Skip to content

Latest commit

History

History
84 lines (68 loc) 路 1.31 KB

working-mode.md

File metadata and controls

84 lines (68 loc) 路 1.31 KB

Working Mode

By default, skema works synchronously.

Woking Asychronously

There are TWO ways to make Skema run asynchronously.

1. Specify options parameter of .from(raw, options)

import {type} from 'skema'
const Type = type({
  set: v => v + 1
})

Type.from(1)  // 2

Type.from(1, {async: true})
.then(v => {
  console.log(v)  // 2
})

2. Change the default settings.

import {defaults} from 'skema'
import {LOOSE} from '@skema/basic'

const {
  type,
  shape
} = defaults({
  async: true,
  types: LOOSE
})

const UserName = type({
  type: String,
  validate (name) {
    if (name.length < 4) {
      throw 'name is too short'
    }

    return new Promise((resolve, reject) => {
      isUniqueFromRemoteHTTPServer(name, (err, unique) => {
        if (err) {
          return reject(err)
        }
        resolve(unique)
      })
    })
  },
  set (name) {
    // Oh, he is the admin, and make him different!
    return name === 'isaac'
      ? 'Isaac Z. Schlueter'
      : name
  }
})

const User = shape({
  name: UserName
})

User.from({
  name: 'izs'
})
.catch(console.error)
// Error
// - code: 'CUSTOM_ERROR',
// - message: 'name is too short'
// - path: ['name'],
// - key: 'name',
// - input: 'izs',

User.from({
  name: 'isaac'
})
.then(console.log)
// {name: 'Isaac Z. Schlueter'}