Socket
Book a DemoInstallSign in
Socket

validate-types

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

validate-types

Validate object properties against type schema

2.3.0
latest
Source
npmnpm
Version published
Weekly downloads
0
Maintainers
1
Weekly downloads
 
Created
Source

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.

Keywords

validate

FAQs

Package last updated on 21 Jul 2020

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

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.