npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

schema-validator-lib

v1.0.2

Published

This is the library to validate value by schema

Readme

This is the library to validate value by schema

  1. Schema
  • Schema define structure of validated value

    • To validate by schema, must first compile schema

    • Compile:

      1. Validate schema syntax
      2. Compile to create function that use to validate schema.
      • Each schema compiled once and reuse
        const validator = require('schema-validator');
      
        // compile schema
        validator.compile({
          code:  <code>, // each schema have different code
          schema: <schema>
        })
      
        validator.validate({
          code: <code>, // schema's code
          input: <input>,
          options: <options>
        })
      • :
        • .strict: default: true

          • true: strict validate, overwrite schema strict
        • .remove_additional_field: default: false

          • true: remove object.field that not specific in schema
    • Non-strict validate:

      1. Convert value to field's schema type
      2. Validate type by using converted value
      3. Assign converted value to input => Convert value before validate type
    • Strict validate: not convert value before validate type

  • 2 type schema:

    • Implicit schema:

      • Object exclude 'type': object schema.

      • Array: array schema.

        schema: [
          {
            _id: { type: 'string }
          }
        ]
      
        // => schema: array contain object element which have _id string property
    • Explicit schema: object include 'type'

        schema: { type: 'object', properties: {
          // schema of object
        }}
      • Explicit schema allow properties:

        • 'type':

          • Field type

          • Can accept multiple type which separate by ','. ex: type: 'string,number'

          • List default type:

            • 'number'
            • 'string'
            • 'boolean'
            • 'date'
            • 'array'
            • 'integer'
            • 'function'
            • 'async_function'
            • 'object'
              • if strict validate: if input object contain field not in define in schema, raise error
          • Add custom type

              validator.schema.type.add({ key: 'integer', handler: {
                convert: ({ info, value, schema }) => value, // convert when strict = false
                check: ({ info, value, schema }) => Number.isInteger(value), // check
              }});
            
              // OR raw
                          
              validator.schema.type.add({ key: 'integer', raw: ({ info, value, schema, strict }) => {
                const result = {
                  value: value,
                  errors: [],
                };
                if (strict) {
                  result.value = Math.round(result.value)
                }
                if (!Number.isInteger(value)) {
                  result.errors.push({ invalid: 'type', expect: 'integer', field: info.field })
                }
                return result;
              }});
            
              // schema: field's schema
              // info: info.field:
              // value: field's value
        • 'require': : default: false

          • true: if field's value missing or undefined => invalid
        • 'nullable': : default: false

          • true: default null
          • false: if field's value == null => invalid
        • 'enum':

          • Field's value must in
        • 'default': <default_value>: default: undefine

          • If provide value: assign default value for field
            {
              type: 'boolean',
              default: true,
            }
          • Can specific function: invoke function and assign result to field
            {
              type: 'number',
              default: ({ info }) => {
          
                // info.input: input value
          
                // info.root: parent object contain field
          
                return <default>
              }
            }
        • 'strict': : default: false

          • true: strict validate
        • 'properties':

          • Schema for object
          • Must provide if 'type' = 'object'
            {
              type: 'object',
              properties: {
                // ...object schema
              }
            }
        • 'element':

          • Schema for element in array
          • Must provide if type = 'array'
            {
              type: 'array',
              element: {
                // ...element schema
              }
            }
        • 'check':

          • Use to extra validate field beside type validate

          • Can be object, which key is check:

            • List default check:

              • 'min':
              • 'max':
              • 'min_length':
              • 'max_length':
              • 'set':
                • true: each element in array must unique
              • 'unique':
                • : element's field in array must unique
                // example
                {
                  type: 'number',
                  check: {
                    min: <number>,
                    max: <number>
                  }
                }
              
                {
                  type: 'array',
                  check: {
                    unique: 'unique_key'
                  },
                  element: {
                    ...
                  }
                }
              
                              
            • Add custom check:

              validator.schema.check.add({ key: 'max_length', handler: {
                check: ({ info, value, schema }) => true,
                make_error: ({ info, value, schema }) => ({ field: info.field, invalid: '', check: schema.check })
              }});
            
              validator.schema.check.add({ key: 'set', raw: ({ info, value: array, schema }) => {
                return {
                  errors: [], // not empty => error
                };
              }});
          • Can be function

            {
              type: 'number',
              check: ({ info, value, schema }) => {
                // info.field
          
                // info.input: input value
          
                // info.root: parent object contain field
          
                return { errors: [] } // if error is not empty => fail
              }
            }
        • 'to':

          • Use to convert or format

          • Can be list separate by ','. Will be execute in order

            • List default:

              • 'trim'
              • 'lowercase'
              • 'uppercase'
              • 'round'
              • 'floor'
              • 'ceil'
              • 'iso_datetime'
                {
                  type: 'date',
                  to: 'iso_datetime,trim'
                }
              • Add custom
               validator.schema.to.add({ key: 'iso_datetime', handler: {
                  to: ({ value }) => new Date(value).toISOString(), // assign result to field
                }});
          • Can be function

              {
                type: 'number',
                to: ({ value }) => String(value).toUpperCase(), // assign result to field
              }