JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 510
  • Score
    100M100P100Q94507F
  • License MIT

Validate object properties against type schema

Package Exports

  • validate-types
  • validate-types/tests/all

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (validate-types) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

Validate types

Build Status NPM Status

Validate object fields using simple schema. Works in browser or server side. Packs no dependencies and weighs less than 2KB.

Documentation and examples.

Install

npm install validate-types;

Usage example

// require or import library
const validateTypes = require('validate-types');
// import validateTypes from 'validate-types';

// define validation schema
const schema = {
    firstName: String,
    lastName: {
        type: String,
        required: true
    },
    address: String,
    zipCode: [String, Number],
    age: {
        type: Number,
        validator: age => age > 17
    },
    acceptsCookies: {
        type: Boolean,
        default: false
    }
};

// call with schema and object to validate
var result = validateTypes(schema, {
    firstName: 42,
    age: 15
});

console.log(result);
// will output
{
   hasErrors: true,
   hasUndeclaredFields: false,
   errors: [
      {field: 'firstName', test: 'type', message: 'Field "firstName" is of invalid type'},
      {field: 'lastName', test: 'required', message: 'Field "lastName" is required'},
      {field: 'age', test: 'validator', message: 'Field "age" failed validation'}
   ],
   data: {
       firstName: 42,
       age: 15,
       acceptsCookies: false
   },
   undeclaredFields: {}
}

Documentation and examples.