Skema: Optional Properties
Demo to show how to define an optional Properties
by kaelzhang
HTML
<script src="https://unpkg.com/skema/umd/jsfiddle.js"></script>
<script src="https://unpkg.com/skema/umd/skema.min.js"></script>
JavaScript
const {shape} = Skema
log('example: use a syntactic suger')
const A = shape({
foo: 'number?',
bar: 'number'
})
// foo is optional
success(A.from({bar: 1}).foo === void 0)
// bar is not optional
throws(() => A.from({}))
log('example: inherit a type')
const B = shape({
foo: {
type: 'number',
optional: true
}
})
success(B.from({bar: 1}).foo === void 0)
log('example: a property with a default value is always optional')
const C = shape({
foo: {
default: 1
},
bar: Number
})
success(C.from({
bar: 1
}))
log('example: set optional:false will disable default value')
const D = shape({
foo: {
type: {
default: 1
},
optional: false
}
})
throws(() => D.from({}))