4 Forms

4 Forms

  • Getting Started
  • API
  • Github
  • Help

›API Reference

Introduction

  • Getting Started
  • Installation
  • Motivation
  • Examples

Guides

  • Intro
  • Field
  • Schema
  • Nested schema
  • Related Field

API Reference

  • API Reference
  • Field
  • Schema
  • When
  • Rules

    • Generic
    • String
    • Number
    • Boolean
    • Array
    • Object
    • Iterator

Schema(rules, [options])

Schema let you validate a javascript object that holds your data, it enables you to build complex validation rules that will apply to your data objects.

Arguments :

  1. Rules (Object): A plain object describing the validation rules of your schema.

  2. Options (Object): A optional plain object describing the schema behavior, by default :

    • verbose : false, Get messages errors object rather than boolean. Read more
    • strict : true, The inverse of the rules is checked in related fields. Read more
    • bool : false, Get Boolean indicator result. Read more
    • async : false, asynchronous validation get promise. Read more

Returns :

  • Schema (Class): A class that hold your schema with the folowing method and attributes:

    • validate(data, [options]) (function): Run validation on your data object.
    • attributes (Field) : Your data fields.

Example

const User = Schema(
    {
        email: Field()
            .string({ message: "%{field} must be string" })
            .email({ message: "%{field} must be email" })
            .required({ message: "%{field} is required" }),
        password: Field()
            .string({ message: "%{field} must be string" })
            .required({ message: "%{field} is required" })
    },
    { verbose: true }
);

let errors = User.validate({});
/**
errors object contains :
 {
  email : "email is reqruied",
  password: "password must be string"
 }
**/
errors = User.validate({ email: "valid@mail.com", password: "mypass" }); // null

// One field Validation

const isValidEmail = User.email.validate("bad@mail", { verbose: false }); // false
← FieldWhen →
  • Arguments :
  • Returns :
  • Example
4 Forms
Docs
Getting StartedGuidesAPI Reference
Community
Stack OverflowProject ChatTwitter
More
GitHub
Copyright © 2019 Soufiane Nassih