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

runtime-validator

v4.2.1

Published

Runtime type checking and validation for TypeScript and JavaScript

Downloads

1,482

Readme

MIT license npm version Build Status Coverage Status

Runtime type checking & validation

A light weight library to perform run-time type checking and field validation for TypeScript and JavaScript.

Features

  • Easy to learn and read. Simple and fast to extend.
  • Small lib. Optimised for tree shaking.
  • Use with either TypeScript OR JavaScript.
  • Detailed error reporting.
  • Creates Validator objects that are concise and composable.
    • Type check against compile-time types and interfaces.
    • Validate both run-time types and field value constraints.
    • Advanced: Apply transforms, data version upgrades ... to all or parts of your data.
  • Can validate a value and then emit the result as:
    • A valid value OR An exception
    • A Promise (rejects if invalid)
    • A success/fail boolean result (logs errors).
    • Create your own result type!

Applications

  • Validate function call arguments
  • Unit test assertions
  • Validate client or server side request/response payloads.
    • Can either filter out (tObject) or prevent (tObjectStrict) undefined fields.
  • Validate component properties (Vue)
  • Create a TypeScript Type Guard

Within an application, build a local library of composable validators that consistently check the types and values of all internal and external data structures.

Installation

npm i runtime-validator

OR

yarn add runtime-validator

Projects using < [email protected] will need a polyfill for the unknown type, such as unknown-ts.

Examples

Typescript Example #1 - Runtime type checking

TypeScript only checks types at compile time not at run-time. The TypeScript project have excluded this from their stated goals, Type guards work, but are limited in that they circumvent type inference instead of working with it, and can be cumbersome to write.

This example demonstrates how to create a validator and then use it to check the schema of a parsed JSON object.


import {
  Validator, tObject, tString, tNumber, tBoolean, optional
} from 'runtime-validator'

interface Pet {
  name: string;
  species: string;
  age?: number;
  isCute?: boolean;
}

const petValidator: Validator<Pet> = tObject({
  name: tString(),
  species: tString(),
  age: optional(tNumber()),
  isCute: optional(tBoolean())
})

// Since `JSON.parse()` returns `any` we need to type check the result at run-time.
const json: any = JSON.parse('{"name":"Lyle", "age":15, "isCute":true}');

// Returns value if valid, throws exception if invalid
// Exeception example
// `Input: {"name":"Lyle","age":15,"isCute":true}
// Failed at input: the key 'species' is required but was not present`
const pet: Pet = petValidator.asException(json);

// resolve() if valid, reject() if invalid
const petPromise: Promse<Pet> = petValidator.asPromise(json);

// true if valid, false if invalid and logs error.
const isValid: boolean = petValidator.asSuccess(json);

JavaScript Example #2 - VueJS Object Field Validation

VueJS supports custom validation of component properties via a validator field defined on the property. The asSuccess modifier matches the call signature so we can use it to validate a Pet property on a component:

import { tObject, tString, tNumber, tBoolean, optional } from 'runtime-validator';

// Create a logger using NPM 'debug' library
import debug from 'debug';
const logger = debug('MyPet');

const vPet = tObject({
  name: tString(),
  species: tString(),
  age: optional(tNumber()),
  isCute: optional(tBoolean())
});

// VueJs component
export default {
  name: 'MyPet',
  props: {
    pet: {
      type: Object,
      required: true,
      validator: v => vPet.asSuccess(v, logger)
      // OR validator: vPet.asSuccessL(logger)
    }
  },
  ...
}

Example #3: Data Validation

  • oneOf() acts like an enum that restricts a field to limited set of values of equal type.
  • Validator.where() can specify custom data validation conditions.
const range = (min: number, max: number) =>
  tNumber().where(
    n => n >= min && n <= max,
    `expected a number between ${min} and ${max}`
  );

const species = oneOf("dog", "cat", "bird", "snake");

const petValidator = tObject({
  name: tString(),
  species,
  age: optional(range(0, 100)),
  isCute: optional(tBoolean())
});

Documentation

This library uses the combinator pattern from functional programming to build a Validator.

Validators can transform data objects with unknown structure into known and verified type and values.

Abbreviations:

  • v = a Validator instance.
  • v1 ... vN is a Validator sequence (array or argument list).
  • value = a constant or a value being validated.

Full API:

Primitive validators

Primitive validators check a single value matches a primitive type.

When executed they return a validation result (value or error) where the value will match the original primitive type.

Primitive | Description | Return Type | -------------- | --------------------- | --------------------- | tString() | Matches a string | Validator<string> | tNumber() | Matches a number | Validator<number> | tBoolean() | Matches a boolean | Validator<boolean> | tFunction() | Matches a function | Validator<function> | tUndefined() | Matches a undefined | Validator<any> | tAny() | Matches any type. | Validator<any> | tUnknown() | Matches any type. | Validator<unknown> |

Modifiers

Modifiers adapt a value or validator to match/ignore or allow nullable/optional values.

Modifiers | Description | ---------------------- | ----------------------- | constant(value) | Matches a constant string, number or boolean value | nullable(v) | Matches null or a value matching v | optional(v) | Matches undefined or a value matching v | succeed(value) | Ignores input and always returns value. | fails(error) | Ignores input and always fails with the error. | valueAt(path, value) | Returns a specific field from within the value data structure. |

Combinators

Combinators combine or wrapper validators to validate a complex type.

Combinator | Description | -------------------------- | ----------------------- | tArray(v) | Matches an array containing elements that all match validator v | tDict(v) | Matches a dictionary having strings as keys and values that all match v | tuple([v1, ... vN]) | Matches an array containing elements that match validators v1 ... vN in sequence | oneOf(v1, ... vN) | Matches one of the validators v1 .. vN which must all return the same type. | union(v1, ... vN) | Matches one of the validators v1 .. vN. Return type is a union of return types. | intersection(v1, ... vN) | Matches all of the validators v1 .. vN. Return type is the intersection of return types. | withDefault(value, v) | If v fails to match input, return a default value. | lazy(v) | Supports validation of recursive types. See example below. |

Example: Use lazy(v) to validate a recursive type:

const validator: Validator<Comment> = tObject({
  msg: tString(),
  replies: lazy(() => tArray(validator)) // self reference
});

Constraints

Constraints are just wrapper validators to add additional value conditions. Internally they use the Validator.where() method to add these constraints.

These are just a few common examples we've included in the library. You'll likely want to create your own, so check out src/constraints.ts for ideas.

Constraints | Description | ---------------------- | ----------------------- | truthy() | Matches any truthy value | falsy() | Matches any falsy value | range(min, max) | Matches a number value between min and max | chars(n) | Matches a string of length n | charsMin(min) | Matches a string of at least min characters | charsMax(max) | Matches a string of no more than max characters | charsRange(min, max) | Matches a string of between min and max characters | matches(pattern) | Matches a string that matches the pattern | httpUrl() | Matches a HTTP or HTTPS URL |

Validation Execution

The following functions all internally call v.check(value) to check if a value is valid. v.asException(), v.asPromise(), v.asSuccess() adapt the output from v.check() for use in different programming contexts. If you need a validator function with a different behaviour or call signature, follow the same coding pattern and create your own!

Example: v.asSuccess is useful in a Vue property validator or in a TypeScript Type Guard.

Execute Validation | Description | --------------------------- | ----------------------- | v.asException(value) | Returns validated value. Throws an exception if invalid. | v.asString(value) | Returns null if valid or error if invalid. | v.asPromise(value) | Returns a Promise that is resolved to the validated value or rejected with the error. | v.asSuccess(value,logger) | If valid returns true, if invalid returns false and logs error. logger defaults to console.error | v.asSuccessL(logger) | Injects the logger early, returning a new asSuccess(value) validator function. | v.check(value) | Returns a CheckResult. Use to create a custom asXXX(). |

Validation Adaptors

Adaptors can transform a Validator to into a new Validator that when executed will adapt the validation Result. See the documentation on these methods in Validator classes for examples.

Adaptors | Description | -------------------------- | ----------------------- | v.where(f(value),error) | If f(value) is false, emits error. Used to create Constraints. | v.map(value => f(value)) | Transforms a validated value to a new value. | v1.andThen(f(value) => v2) | andThen() can conditionally chain together validator sequences. |

Acknowledgements

This library owes thanks to: