New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

@vs-org/schema-validator

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@vs-org/schema-validator

Basic JSON schema validator

latest
Source
npmnpm
Version
0.0.1
Version published
Weekly downloads
0
-100%
Maintainers
1
Weekly downloads
 
Created
Source

@vs-org/schema-validator

This is basic JSON schema validator.

Usage

  • 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: "asd@asd.com",
    password: "PassS@123"
  }
);

  • 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 === 'test@test.com'){
    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: "test@test.com"
  }
);

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

Schema validator options

optionrequiredtypeDescription
allErrorsfalsebooleanIf 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

optionrequiredtypeDescription
typefalsePropetyType ("Undefined", "Null", "String", "Boolean", "Number", "Array", "Email", "Object", "Url", "RegExp", "Function")This option indicate what type of value is accepted for the property
nullablefalsebooleanThis option indicate if value canb be null for the property
itemsfalsePropertyType[] 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
itemsFromfalseany[] This option is used to validate value array items should be from itemsFrom array
oneOffalseany[] This option is used to validate value should be one of item from oneOf array
requiredfalsebooleanThis option indicates if value should be truthy
minLengthfalsenumberThis option indicates value should be of at least minimum length. Value can be array, string, number
maxLengthfalsenumberThis option indicates value can be of maximum length. Value can be array, string, number
minLowerCasefalsenumberThis option indicates value should atleast contain minimun lowercase chars
minUpperCasefalsenumberThis option indicates value should atleast contain minimun uppercase chars
minNumbersfalsenumberThis option indicates value should atleast contain minimun numbers (digits)
minSymbolsfalsenumberThis option indicates value should atleast contain minimun symbols
maxConsecutiveCharsfalsenumberThis option indicates value can contain maximum consecutive chars
validatorfalseFunction: (value: any) => stringIf 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
errorMessagefalsestringThis 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.

Keywords

schema validators

FAQs

Package last updated on 18 Sep 2022

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