Wondering what’s next for npm?Check out our public roadmap! »

    validatable-record

    1.2.0 • Public • Published

    validatable-record

    CircleCI standard-readme compliant npm version MIT License

    Immutable.js Record powered with validate.js

    Table of Contents

    Install

    $ npm install --save validatable-record

    Usage

    ValidatableRecord returns Record in Immutable.js for extending your own class. Usage is almost the same as Record in Immutable.js, but it has the power of validate.js. With ValidatableRecord, you can define models with built-in validation logic.

    const ManRecord = ValidatableRecord({
      name: null,
      age: null
    }, {
      name: {
        presence: true
      },
      age: {
        presence: {
          message: "is invalid"
        }
      }
    });
     
    class Man extends ManRecord {
      ...
    }
     
    const man = new Man({
      name: "Justine";
      age: 25
    });
     
    man.validate() // == true
     
    // Of course you can use `Immutable.Record` methods
    man.size        // 2
    man.get('name') // "Justine"
    man.get('age')  // 25
     
    const agelessMan = new Man({
      name: "Michael"
    });
     
    agelessMan.validate() // == false
     
    agelessMan.getErrors() // == [ "Age is invalid" ]
     
    // You can set your own error to model
    agelessMan.setError("Unknown error")
     
    agelessMan.getErrors() // = [ "Unknown error" ]

    Test

    $ npm test

    Contribute

    PRs accepted.

    License

    The gem is available as open source under the terms of the MIT License.

    Install

    npm i validatable-record

    DownloadsWeekly Downloads

    3

    Version

    1.2.0

    License

    MIT

    Unpacked Size

    5.52 kB

    Total Files

    5

    Last publish

    Collaborators

    • avatar