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

@vs-org/schema-validator

v0.0.1

Published

Basic JSON schema validator

Downloads

3

Readme

@vs-org/schema-validator

This is basic JSON schema validator.

Usage

  1. Basic schema validation
// CJS
import { SchemaValidationError, SchemaValidator } from "@vs-org/schema-validator";
import { Schema } from "@vs-org/schema-validator/SchemaValidator";

// Module
const SchemaValidator = require("@vs-org/schema-validator").default;


const schemaValidator = SchemaValidator.getInstance({
  allErrors: true
});

const validationSchema: Schema = {
  properties: {
    firstName: {
      type: "String",
      required: true,
      maxLength: 30
    },
    lastName: {
      type: "String",
      required: false,
      minLength: 30
    },
    email: {
      type: "Email",
      required: true
    },
    password: {
      type: "String",
      required: true,
      minLength: 8,
      maxLength: 48,
      maxConsecutiveChars: 2,
      minUpperCase: 1,
      minNumbers: 1,
      minSymbols: 1,
      errorMessage:
        "Password is invalid. Should contain min 8 character, max 48 characters, no consecutive characters, atleast one number, atleast one symbol, atleast one uppercase character"
    }
  }
};

schemaValidator.addSchema("test", validationSchema);
schemaValidator.compile("test");

const validationErrors: SchemaValidationError = schemaValidator.validate(
  "test",
  {
    firstName: "asd",
    email: "[email protected]",
    password: "PassS@123"
  }
);
  1. Custom validation function
// CJS
import { SchemaValidationError, SchemaValidator } from "@vs-org/schema-validator";
import { Schema } from "@vs-org/schema-validator/SchemaValidator";

// Module
const SchemaValidator = require("@vs-org/schema-validator").default;

const schemaValidator = SchemaValidator.getInstance({
  allErrors: true
});

const isValidEmail = (email) => {
  if(email === '[email protected]'){
    return `Provided email is invalid`
  }
  return '';
}

const validationSchema: Schema = {
  properties: {
    firstName: {
      type: "String",
      required: true,
      maxLength: 30
    },
    lastName: {
      type: "String",
      required: false,
      minLength: 30
    },
    email: {
      type: "Email",
      required: true,
      validator: isValidEmail
    }
  }
};

schemaValidator.addSchema("test", validationSchema);
schemaValidator.compile("test");

const validationErrors: SchemaValidationError = schemaValidator.validate(
  "test",
  {
    firstName: "asd",
    email: "[email protected]"
  }
);

// Result
SchemaValidationError {
      errors: [ { field: 'email', error: 'Provided email is invalid' } ]
    }

Schema validator options

| option | required | type | Description | | ----------- | -------- | --------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | allErrors | false | boolean | If all schema validator errors are required, then provide this option for getInstance function. If this is not specified, Vs Schema validator will first occurance of validation error. Ideally it is recommeded to use this as user will get all errors together to correct. |

Property options

| option | required | type | Description | | --------------------- | -------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | type | false | PropetyType ("Undefined", "Null", "String", "Boolean", "Number", "Array", "Email", "Object", "Url", "RegExp", "Function") | This option indicate what type of value is accepted for the property | | nullable | false | boolean | This option indicate if value canb be null for the property | | items | false | PropertyType[] Array can contain values only from ("Undefined", "Null", "String", "Boolean", "Number", "Array", "Email", "Object", "Url", "RegExp", "Function") | This option is used to validate array of types | | itemsFrom | false | any[] | This option is used to validate value array items should be from itemsFrom array | | oneOf | false | any[] | This option is used to validate value should be one of item from oneOf array | | required | false | boolean | This option indicates if value should be truthy | | minLength | false | number | This option indicates value should be of at least minimum length. Value can be array, string, number | | maxLength | false | number | This option indicates value can be of maximum length. Value can be array, string, number | | minLowerCase | false | number | This option indicates value should atleast contain minimun lowercase chars | | minUpperCase | false | number | This option indicates value should atleast contain minimun uppercase chars | | minNumbers | false | number | This option indicates value should atleast contain minimun numbers (digits) | | minSymbols | false | number | This option indicates value should atleast contain minimun symbols | | maxConsecutiveChars | false | number | This option indicates value can contain maximum consecutive chars | | validator | false | Function: (value: any) => string | If application needs some custom validation, then option can be provided. It should be function which can accept any type of value and should always return string (error message) which will be used as validation error message. If provided value is valid then return empty string | | errorMessage | false | string | This option can be provided if custom error messages are required. |

License

MIT (see LICENSE)

Note

This is experimental package and not actively maintained. Please don't raise issues or feature requests. Only use for development and POC's.