Nitrogen Peroxide Monoxide

    gateschema
    TypeScript icon, indicating that this package has built-in type declarations

    0.2.1 • Public • Published

    Build Status Coverage Status

    A small, simple and expressive schema builder and data validator.

    Features

    Quick Start

    import _ from 'gateschema' 
    // Schema creation   
    const schema = _
      .required
      .map({
        name: _
          .required
          .string
          .notEmpty,
        password: _
          .required
          .string
          .notEmpty,
        isRemember: _
          .optional
          .boolean
      })
     
    const userInput = {
      // ....
    } 
     
    // Data Validation
    // callback style
    schema.validate(userInput, (err) => {
      if (err) {
        // ...
      }
      // ...
    })
     
    // or promise style 
    schema
      .validate(userInput)
      .then(() => {
        // ...
      })
      .catch((err) => {
        // ...
      })
     
     
    // Serialization
    console.log(schema.toJSON()) // or JSON.stringify(schema)

    Install

    npm install gateschema --save  
    

    API

    see API

    Q&A

    Custom messages and i18n

    see $msg and addMsgs

    Require fields conditionally

    const schema = _
      .map({
        employed: _
          .optional
          .boolean
      })
      .switch('/employed', [
        {
          // if `employed` is true
          case: _
            .value(true), 
          // then the input shoud be a map containing `employee` key
          schema: _ 
            .map({
              employee: _
                .required
                .string
            })
        }
      ])

    One of a field is required

    const schema = _
      .required
      .map({
        email: _
          .switch('/phone', [
            {
              case: _.required, // if `phone` satisfy `_.required`
              schema: _.optional // then `email` is optional
            },
            {
              case: _.any, // else 
              schema: _.required.$msg('Please input email or phone') // `email` is required
            }
          ])
          .string
          .format('email'),
        phone: _
          .switch('/email', [
            {
              case: _.required,
              schema: _.optional
            },
            {
              case: _.any,
              schema: _.required.$msg('Please input email or phone')
            }
          ])
          .string
      })

    Another way

    const schema = _
      .required
      .map({
        email: _
          .optional
          .string
          .format('email'),
        phone: _
          .optional
          .string
      })
      .oneOf([
        _.map({
          email: _.required
        }),
        _.map({
          phone: _.required
        })
      ])
      .$msg('Please input email or phone')

    Changelog

    See CHANGELOG

    License

    MIT

    Install

    npm i gateschema

    DownloadsWeekly Downloads

    0

    Version

    0.2.1

    License

    MIT

    Unpacked Size

    347 kB

    Total Files

    245

    Last publish

    Collaborators

    • william17