Validate object properties against type schema

Usage no npm install needed!

<script type="module">
  import validateTypes from '';


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.


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

// 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.