Skip to content

Latest commit

History

History
60 lines (44 loc) 路 1.11 KB

builtins.md

File metadata and controls

60 lines (44 loc) 路 1.11 KB

Built-in Types and Type Aliases

Built-in types are actually SkemaAliases which declared by declare()

Declare Custom Type Aliases

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'

Live Demo馃敩

Built-in Types

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.

Uses Skema as Type Checker

Live Demo馃敩