New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

gateschema

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

gateschema

GateSchema javascript implementation

latest
Source
npmnpm
Version
0.2.1
Version published
Maintainers
1
Created
Source

Build Status Coverage Status

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

Features

  • simple and expressive syntax and keywords
  • conditionally constraints
  • supports async validation
  • form generation: gateschema-form-vue, gateschema-form-react
  • serialization

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

Keywords

gateschema

FAQs

Package last updated on 07 Nov 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts