It's a very simple validation library. No dependencies. Written in TypeScript. Useful for counstructing well-typed validation functions using a declarative interface. The validation functions are generally supposed to accept any input, ensure its shape during runtime and return typed values.
Validation result success and error types are inferred from the validators used.
const personValidator = shape({
name: shape({
first: string,
last: string
}),
age: number
})
const validation = personValidator(<input>)
if (isOk(validation)) {
// Handle success with validation.value
// validation.value is typed to the validator successful result
} else {
// Handle failure with validation.value
// validation.value is typed to the validator erroneous result
}
npm install --save valid-ts
I want to try out an unusual approach to documentation. I've created a sandbox project in the awesome codesandbox.io:
https://codesandbox.io/s/valid-ts-sandbox-9xxjj
All you need to know to use the library lives in the comments of src/index.ts
there.
There are two simple example projects to look at. Please check their README.md
s to build and test them:
-
Server-side validation. This project shows an example of creating a union type "command" validator and using it in an Express endpoint. In README you can find some more use case examples.
-
Client-side validation. This project shows a very simple ValidTs-Formik integration - translating ValidTs errors to a form digestable by Formik.
npm install
npm test
You're welcome to open a GitHub issue about any problem/question you have. Even a typo in documentation is a good thing to report!
Copyright (c) 2018-present, Michał Podwórny