GithubHelp home page GithubHelp logo

kya's Introduction

Kya - Javascript Object Validation

Kya is written to make object validation easier. It support JS native type, required field and easy customized validate function, all in single object as descriptor. The validation is asynchronous.

Install

npm i kya --save

Support type

  • String
  • Number
  • Date
  • Boolean
  • Array
  • Array validation
  • Nested object validation

Examples

  const usernameValidator = {
    required: true,
    type: String,
  }

  const schema = kya(
    {
      username: usernameValidator,
      telephone: Number,
    },
    {
      username: {
        required: 'username is required',
        type: 'Type error!'
      },
      telephone: {
        type: 'Type error!'
      },
    }
  )

  schema
    .validate({ username: 'kya', age: 19 })
    .then(result => {
      console.log('result', result) // { username: { valid: true }, telephone: { valid: true } }​​​​​
    })

API

kya

  function kya(schema?: Schema, messages?: Messages): {
    validate: (target?: Object) => Promise<Object>;
    validateOn: (target?: Object, ...onfields: string[]) => Promise<Object>;
  };
interface Schema {
  [field: string]: FieldSchema;
}
type SupportType = String | Number | Boolean | Array<any> | Date | Object;
type Type = { type: string; } | SupportType;
type FieldSchema =
  (Type & {required?: boolean; }) &
  {
    [customrule: string]: any
  };
export interface Messages {
  [field: string]: Object;
}

There are two arguments, schema and message, in kya. Schema is an object that specify fields you want to check, and in each field you wanna specify field schema, where your validation rule goes here. Message is the error message it gonna show whenever your field validation is not valid.

kya's People

Contributors

groupystinks avatar

Watchers

James Cloos avatar  avatar

Forkers

xxd4rkc0d3rxx

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.