Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

model-validate

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

model-validate

Data validation framework for MVC

  • 0.1.1
  • npm
  • Socket score

Version published
Weekly downloads
1
Maintainers
1
Weekly downloads
 
Created
Source

ModelValidate

MVC validation for JavaScript

SyncValidator

src/index.js:20-71

Class for synchronous validation.

Parameters

  • validators (optional, default {})

validate

src/index.js:39-70

Validate a model according to the validators set up in mask

Parameters

  • model Object Data to be validated Ex: { phone : "123-456-7890" }

  • mask Object Validators to be applied to model. Ex: { phone : ['required', 'phone']}

    The following validators are provided by default

    • required checks for non-null, non-whitespace, non-empty strings or Numbers
    • phone validates 10 or 11 digit phone numbers reguardless of formatting characters
    • credit-card validates credit card numbers according to a LUHN-10 check
    • zip-code validates American 5-digit and 9-digit zip codes

Example

import {SyncValidator} from 'model-validate';

// Get a validator with just the built-in validators
let validator = new SyncValidator();

// sample data including some errors
let model = {
  address : {
    firstName : 'John',
    address1  : '123 SomeRoad',
    address2  : 'apt 4',
    city      : 'New York',
    state     : '    ',
    zip       : '123456',
  },
  phone : '1-(123)-123-1234',

}

// get validation results
let results = validatator.validate(model, {
  // the mask object mirrors the structure of the model
  address : {
    firstName : ['required'],
    lastName  : ['required'],
    address1  : ['required'],
    city      : ['required'],
    state     : ['required'],
    zip       : ['required', 'zip-code'],
  },
  phone : ['required', 'phone'],
})

// This returns the following structure
{
  "address": {
    "firstName": {
      "required": {
        "_valid": true
      },
      "_valid": true
    },
    "lastName": {
      "required": {
        "_valid": false,
        "reason": "\'lastName\' is null or undefined"
      },
      "_valid": false
    },
    "address1": {
      "required": {
        "_valid": true
      },
      "_valid": true
    },
    "city": {
      "required": {
        "_valid": true
      },
      "_valid": true
    },
    "state": {
      "required": {
        "value": false,
        "reason": "String is empty"
      },
      "_valid": false
    },
    "zip": {
      "required": {
        "_valid": true
      },
      "zip-code": {
        "_valid": false,
        "reason": "Zip must be 5 digits or have the full extended syntax"
      },
      "_valid": false
    },
    "_valid": false
  },
  "phone": {
    "required": {
      "_valid": true
    },
    "phone": {
      "_valid": true
    },
    "_valid": true
  },
  "_valid": false
}

There is a _valid property at every level that describes the validity of everything at that level and lower. This way you can decide to use the data or not simply based on results._valid, but you can show feedback to the user based on the valididy of a specific validation.

For example model.address.zip passes the required validator, but not the zip-code validator because it has 6 digits instead of 5. So results.address.zip.required._valid == true, but results.address.zip.zip-code._valid == false

FAQs

Package last updated on 22 Sep 2016

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc