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 🙏

© 2024 – Pkg Stats / Ryan Hefner

5no-schema

v1.2.3

Published

JSON Schema Filter/Validator

Downloads

115

Readme

Schema

JSON Schema Filter/Validator

This package has moved to @5no/schema

Install

@5no/schema requires Node version 8 or above.

npm install --save @5no/schema

Doc


const schemaJson = {
    [name]: {
      type: Number, // Number, String, Boolean, Date, Array, Object
      schema: {}, // Can describe object schema
      prefilled: false, // This value is optionally, indicate filling or not by default field
      defaultValue: null, // This value is optionally
      format: null, // This value only for Date and Number types
      required: true, // true or false
      filters: [], // Can be 'trim', 'upperCase', 'lowerCase' or custom functions
      validators: [], // Can be custom functions
    }
}

const customFilertOrValidator = ({name, type, value, defaultValue, previousResult, allValues, options}) {
  //name - name of field
  //type - type of field
  //value - current value
  //defaultValue - default value of field
  //previousResult - result previous validator
  //allValues - all data
  //options custom data
}
   

Examples

const Schema = require('@5no/schema')

const schemaJson = {
    id: {
      type: Number,
      defaultValue: null,
      required: true,
      format: '0',
      filters: [
                    ({value}) => {
                        return value + 1000
                    },
                    {
                        fn: ({value, options}) => {
                            return value + options.step
                        },
                        options: {
                            step: 100
                        }
                    }
                ],
        validators: [
                    ({value, previousResult}) => {
                        if (previousResult === true) {
                          if (value > 100) {
                            return 'Test Custom Error'
                          }
                          return true
                        }
                    },
                    async ({value}) => {
                        if (value > 100) {
                            return 'async Test Custom Error'
                        }
                        return true
                    },
                    {
                        fn: ({value, options}) => {
                            if (value > 100) {
                                return options.message
                            }
                            return true
                        },
                        options: {
                            message: 'Test Custom Error Options'
                        }
                    }
                ]
    },
    email: {
      type: String,
      defaultValue: null,
      required: true,
      filters: [
        'trim',
        'lowerCase'
      ]
    },
    active: {
      type: Boolean,
      prefilled: true,
      defaultValue: false
    },
    createdAt: {
      type: Date,
      defaultValue: null
    },
    updatedAt: {
      type: Date,
      defaultValue: null,
      format: 'YYYY-MM-DD'
    },
    informations: {
      type: Object,
      schema: {
        firstName: {
          type: String,
          required: true
        },
        lastName: {
          type: String,
          required: true
        }
      }
    },
    parameters: {
      type: Array,
      required: true,
      defaultValue: [],
      schema: {
        name: {
          type: String,
          required: true,
          filters: [
            'lowerCase',
          ],
        },
        value: {
          type: String,
          required: true,
        },
      },
    },
    address: {
      type: Object,
      defaultValue: null,
    },
    roles: {
      type: Array,
      defaultValue: null
    }
    status: {
      type: String,
      defaultValue: 'active',
      allowedValues: [
        'active',
        'banned'
      ],
    }
}

const schemaJsonData = {
    id: 123,
    email: '[email protected]',
    active: true,
    createdAt: '2018-12-12 12:12:12',
    updatedAt: '2018-12-12 12:12:12',
    informations: {
        firstName: 'FirstName',
        lastName: 'LastNname'
    },
    parameters: [
      {
        name: 'FirstName',
        value: 'LastNname',
      },
      {
        name: 'FirstName1',
        value: 'LastNname1',
      },
    ],
    roles: [
      'customer',
      'admin'
    ],
    status: 'active'
}

const SchemaData = new Schema(schemaJson, false) // prefilledSchema - prefill object by default values of schema

const SchemaDataFiltered = await SchemaData.filter(schemaJsonData)

/*
{ 
  id: 1223,
  email: '[email protected]',
  active: true,
  createdAt: 2018-12-12T10:12:12.000Z,
  updatedAt: '2018-12-12',
  informations: {
    firstName: 'FirstName',
    lastName: 'LastNname',
  },
  parameters: [
    {
      name: 'firstname',
      value: 'LastNname',
    },
    {
      name: 'firstname1',
      value: 'LastNname1',
    },
  ],
  roles: [ 'customer', 'admin' ],
  status: 'active'
}
*/

SchemaData.validate(SchemaDataFiltered).then(function(data) {
    //data = SchemaDataFiltered
    console.log(data)
}).catch(function(errors) {
    //errors = The object consist of fields which have errors

    /*
    { 
      email: [ 'email is required' ],
      informations:
      { 
        firstName: [ 'firstName is required' ],
        lastName: [ 'lastName is required' ] 
      },
      parameters: [
          'parameters is required',
        ],
    }
    */

    Object.keys(errors).forEach((name) => {
        for (let error of errors[name]) {
            console.log(error)
        }
    })
})


//prefilledSchema = true
const schemaJsonData = {
    id: 123,
    email: '[email protected]',
    createdAt: '2018-12-12 12:12:12',
    updatedAt: '2018-12-12 12:12:12',
    informations: {
        firstName: 'FirstName',
        lastName: 'LastNname'
    },
    parameters: [
      {
        name: 'FirstName',
        value: 'LastNname',
      },
      {
        name: 'FirstName1',
        value: 'LastNname1',
      },
    ],
    roles: [
      'customer',
      'admin'
    ],
    status: 'active'
}

const SchemaData = new Schema(schemaJson, true) // prefilledSchema - prefill object by default values of schema

const SchemaDataFiltered = await SchemaData.filter(schemaJsonData)

/*
{ 
  id: 1223,
  email: '[email protected]',
  active: false,
  createdAt: 2018-12-12T10:12:12.000Z,
  updatedAt: '2018-12-12',
  address: null,
  informations: { 
    firstName: 'FirstName', 
    lastName: 'LastNname' 
  },
  parameters: [
    {
      name: 'firstname',
      value: 'LastNname',
    },
    {
      name: 'firstname1',
      value: 'LastNname1',
    },
  ],
  roles: [ 'customer', 'admin' ],
  status: 'active'
}
*/


const SchemaDataJsonOptions = SchemaData.json()

/*
{
    id: {
      type: "number",
      defaultValue: null,
      required: true
    },
    email: {
      type: "string",
      defaultValue: null,
      required: true
    },
    active: {
      type: "boolean",
      defaultValue: false,
      required: false
    },
    createdAt: {
      type: "date",
      defaultValue: null,
      required: false
    },
    updatedAt: {
      type: "date",
      defaultValue: null,
      format: "YYYY-MM-DD",
      required: false
    },
    informations: {
      type: "object",
      required: false,
      schema: {
        firstName: {
          type: "string",
          required: true
        },
        lastName: {
          type: "string",
          required: true
        }
      }
    },
    parameters: {
      type: 'array',
      defaultValue: [],
      required: true,
      schema: {
        name: {
          type: 'string',
          required: true,
        },
        value: {
          type: 'string',
          required: true,
        },
      },
    },
    address: {
      type: "object",
      defaultValue: null,
      required: false
    },
    roles: {
      type: "array",
      defaultValue: null,
      required: false
    },
    status: {
      type: "string",
      defaultValue: null,
      required: false,
      allowedValues: [
        'active',
        'banned'
      ]
    }
}
*/
   

License

MIT Licensed, Copyright (c) 2018 Aleksandr Sokol