Skema: Built-in Types
Example to declare a type alias
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 {
defaults
} = Skema
const {
shape,
declare
} = defaults() // Creates methods with no builtin types.
throws(() => {
shape({
foo: 'number'
})
})
const isNaN = typeof Number.isNaN === 'function'
? Number.isNaN
: n => n === n
// declare a type alias
declare('number', {
validate (v) {
if (typeof v === 'number' && !isNaN(v)) {
return true
}
throw 'not a number'
}
})
// Now it is ok to use 'number' as a type
const Shape = shape({
foo: 'number'
})
throws(() => {
Shape.from({
foo: '1'
})
})