typanion
Last updated 3 years ago by arcanis .
MIT · Repository · Original npm · Tarball · package.json
$ gnpm install typanion 
SYNC missed versions from official npm registry.

Typanion

Static and runtime type assertion library with no dependencies

Installation

yarn add typanion

Why

  • Typanion can validate nested arbitrary data structures
  • Typanion is type-safe; it uses type predicates
  • Typanion allows you to derive types from your schemas
  • Typanion can report detailed error reports

Compared to yup, Typanion has a better inference support for TypeScript + supports isOneOf. Its functional API makes it very easy to tree shake, which is another bonus (although the library isn't very large in itself).

Documentation

Check the website for our documentation: mael.dev/typanion.

Usage

First define a schema using the builtin operators:

import * as t from 'typanion';

const isMovie = t.isObject({
    title: t.isString(),
    description: t.isString(),
});

Then just call the schema to validate any unknown value:

const userData = JSON.parse(input);

if (isMovie(userData)) {
    console.log(userData.title);
}

Passing a second parameter allows you to retrieve detailed errors:

const userData = JSON.parse(input);
const errors: string[] = [];

if (!isMovie(userData, {errors})) {
    console.log(errors);
}

You can also apply coercion over the user input:

const userData = JSON.parse(input);
const coercions: Coercion[] = [];

if (isMovie(userData, {coercions})) {
    // Coercions aren't flushed by default
    for (const [p, op] of coercions) op();

    // All relevant fields have now been coerced
    // ...
}

You can derive the type from the schema and use it in other functions:

import * as t from 'typanion';

const isMovie = t.isObject({
    title: t.isString(),
    description: t.isString(),
});

type Movie = t.InferType<typeof isMovie>;

// Then just use your alias:
const printMovie = (movie: Movie) => {
    // ...
};

Schemas can be stored in multiple variables if needed:

import * as t from 'typanion';

const isActor = t.isObject({
    name: t.isString();
});

const isMovie = t.isObject({
    title: t.isString(),
    description: t.isString(),
    actors: t.isArray(isActor),
});

License (MIT)

Copyright © 2020 Mael Nison

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Current Tags

  • 3.14.0                                ...           latest (a year ago)

32 Versions

  • 3.14.0                                ...           a year ago
  • 3.13.0                                ...           a year ago
  • 3.12.1                                ...           2 years ago
  • 3.12.0                                ...           2 years ago
  • 3.11.0                                ...           2 years ago
  • 3.10.0                                ...           2 years ago
  • 3.9.0                                ...           3 years ago
  • 3.8.0                                ...           3 years ago
  • 3.7.2                                ...           3 years ago
  • 3.7.1                                ...           3 years ago
  • 3.7.0                                ...           3 years ago
  • 3.6.1                                ...           3 years ago
  • 3.6.0                                ...           3 years ago
  • 3.5.0                                ...           3 years ago
  • 3.4.1                                ...           3 years ago
  • 3.4.0                                ...           3 years ago
  • 3.3.2                                ...           3 years ago
  • 3.3.1                                ...           4 years ago
  • 3.3.0                                ...           4 years ago
  • 3.2.1                                ...           4 years ago
  • 3.2.0                                ...           4 years ago
  • 3.1.0                                ...           4 years ago
  • 3.0.1                                ...           4 years ago
  • 3.0.0                                ...           4 years ago
  • 2.2.0                                ...           4 years ago
  • 2.1.1                                ...           4 years ago
  • 2.1.0                                ...           4 years ago
  • 2.0.2                                ...           4 years ago
  • 2.0.1                                ...           4 years ago
  • 2.0.0                                ...           4 years ago
  • 1.0.1                                ...           4 years ago
  • 1.0.0                                ...           4 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 12
Last Day 0
Last Week 12
Last Month 0
Dependencies (0)
None
Dev Dependencies (9)
Dependents (1)

Copyright 2013 - present © cnpmjs.org | Home |