Built-in types are actually SkemaAlias
es which declared by declare()
import {
defaults
} from 'skema'
const {
shape,
declare
} = defaults() // Creates methods with no builtin types.
shape({
foo: 'number'
})
// throw Error
// - code: 'UNKNOWN_TYPE'
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'
}
})
const Shape = shape({
foo: 'number'
})
Shape.from({foo: '1'})
// throw Error
// - code: 'CUSTOM_ERROR'
// - message: 'not a number'
The default built-in types are loose types, which means they try to convert properties of input values into the right ones if possible.
see @skema/basic for details.