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

universal-scheme-validator

v0.0.1

Published

Validation schemas for class-based data models

Readme

universal-scheme-validator

Robust and easy-to-use validation schemas for class-based data models in TypeScript.

Installation

Install the package using npm:

npm install universal-scheme-validator

Basic Usage

Define your classes or data types, create a validation schema using the ValidationSchema interface, and evaluate your instances using the runSchemaValidation function.

import { ValidationSchema, runSchemaValidation } from 'universal-scheme-validator';

class User {
  name!: string;
  email!: string;
  age?: number;
}

// 1. Define the validation schema
const userSchema: ValidationSchema<User> = {
  name: {
    isRequired: true,
    minLength: 3,
    maxLength: 50,
  },
  email: {
    isRequired: true,
    isEmail: true,
  },
  age: {
    isNumber: { isZero: false },
    isMin: 18,
  },
};

// 2. Create an instance and test validation
try {
  const user = new User();
  user.name = 'Ro'; // Minimum length is 3

  // 3. Execute validation
  // Throws an exception if there are errors by default
  runSchemaValidation(user, userSchema);
} catch (error) {
  console.log(error.errors);
  // Expected output:
  // {
  //   "name": ["The value must be at least 3 characters long"],
  //   "email": ["The property 'email' is required"]
  // }
}

Validation Options

The runSchemaValidation function accepts a third parameter for configuration options:

  • strict (boolean): If true, throws an error when the object contains properties that are not defined in the schema.
  • output ("exception" | "record"): Determines the format in which errors are returned.
    • "exception" (default): Throws a ValidationError exception.
    • "record": Returns an object where the keys are the properties and the values are arrays of the found errors.

Options example:

// Returns errors without throwing an exception and validates in strict mode
const errores = runSchemaValidation(user, userSchema, {
  output: 'record',
  strict: true,
});

Validation of Nested Objects and Arrays

You can validate complex structures, arrays, and nested objects using the nestedSchema rule.

const schemaComplejo: ValidationSchema = {
  direccion: {
    isObject: true,
    nestedSchema: {
      calle: { isRequired: true, isNotEmpty: true },
      codigoPostal: { isNumber: true },
    },
  },
  etiquetas: {
    isArray: { type: 'string' },
    isRequired: true,
  },
};

It is also possible to directly pass an array of instances to runSchemaValidation(arrayOfInstances, schema).

Available Rules (ValidationRule)

The following validation rules are available through the ValidationRule interface and can be used on any property:

Text and Strings

  • isString: boolean: Verifies that the value is a string.
  • minLength: number: Minimum required length of a string.
  • maxLength: number: Maximum required length of a string.
  • isEmail: boolean: Verifies if the string is a valid email.
  • isUrl: boolean: Verifies if the string is a valid URL.
  • isPath: PathValidationOptions: Verifies if the string is a valid path. Allows configuring extra options (e.g., no spaces, not empty).
  • isUUID: boolean | { version?: number, provider?: "standard" | "oracle" }: Validates that it's a valid UUID identifier.
  • isJSON: boolean | { expectedStructure?: ExpectedStructure }: Verifies if the string is a valid JSON.
  • isNotAlpha: boolean | object: Validates that the text does not contain purely alphabetical characters, allowing numbers, accents, or symbols.

Numbers

  • isNumber: boolean | { isZero?: boolean, type?: NumberSchema }: Verifies that the value is numeric.
  • isMin: number: Minimum expected numeric value.
  • isMax: number: Maximum expected numeric value.

Data Types and Structure

  • isType: PrimitiveType: Verifies that the type matches a primitive ("string" | "number" | "boolean").
  • isBoolean: boolean: Validates that the input value is boolean.
  • isArray: boolean | { type: PrimitiveType, strict?: boolean }: Validates that the structure is an array.
  • isObject: boolean | { allowEmpty?: boolean, allowArrays?: boolean }: Verifies that the value is an object.

Required and Equality

  • isRequired: boolean: Strict property, throws an error if the value or property is null, undefined, or non-existent in the instance.
  • isNotEmpty: boolean: The value cannot be empty (works with strings, arrays, objects).
  • isEqualTo: any: Checks if the input value is strictly equal to the provided value.
  • containsValue: any[]: Checks if the tested value includes any of the elements from the options array.

Classes and Nested Schemas

  • isInstance: class: Verifies if the provided value is an instance of a specified class.
  • nestedSchema: An optional object with additional validation schema for embedded/nested structures.

Errors and Exceptions

By default, if a validation fails, the library will throw an instance of ValidationError which can be accessed from @catch.

import { ValidationError } from 'universal-scheme-validator';

try {
  // ...
} catch (error) {
  if (error instanceof ValidationError) {
    console.log(error.errors);
  }
}