@topl/toy
v0.8.54
Published
thornian joi clone
Downloads
32
Readme
🦜 toy
Toy is a language for validating user input. Like JSON, its syntax is a subset of JavaScript.
array().items(string(), number()).validate([1, 1, 2, 3, 5, "eureka!"])
Toy is based upon (a partial clone but not a fork of) Joi
installation
npm install @topl/toy
usage
import { string } from "@topl/toy";
// Validate you a string:
const clean = string().validate(dirty);
If you prefer, you can import the namespace:
import * as toy from "@topl/toy";
const clean = toy.string().validate(dirty);
It is also possible to use toy with CommonJS:
const toy = require("@topl/toy"); // and so on
const { string } = require("@topl/toy"); // also possible
note
This is a clone of joi that is designed to be slightly lower-level. It lacks some of the higher-level validations joi has, notably ones for email and uuid. Instead, it adds a pair of general purpose operators, .must()
and .derive()
, to which the user can pass along a lambda. For instance, email validation can be accomplished like this:
const { string } = require("@topl/toy");
const isEmail = require('isemail');
const validEmail = string().must(isEmail).validate(input);
The intent is to keep toy small, but provide a common api to support all the use-cases.
api
See API
license
See LICENSE