Runtime type checking against composable schemas.
import { Types, OneOf, validate } from 'typeshape';
let CardSchema = {
suit: OneOf('Spades', 'Diamonds', 'Clubs', 'Hearts'),
value: Types.number({ '>': 0, '<': 14 })
};
validate(CardSchema, { suit: 'Spaded', value: 10 })
// {
// valid: false,
// reason: 'Expected one of "Spades" or "Diamonds" or "Clubs" or "Hearts" but got "Spaded"',
// path: ['suit']
// }
Check out the demo!
Inspired by Clojure Spec, React PropTypes and JSON Blueprint.
Many applications and tools operate with structured data that the user provides at runtime, such as data sets, configuration files and task descriptions. Normally, these data sources are well beyond the reach of compile-time type checks, so it's the programmer's responsibility to build parsing and validation interfaces that receive and handle the data elegantly.
Typeshape helps you write declarative, composable runtime schemas that programmers can use to validate potentially malformed data, providing the tool needed to let the user know where and why an error occurred.