Skip to content
/ valid-ts Public

Simple TypeScript validation library with a declarative interface aimed at providing correct type inference of both successful and erroneous validations.

License

Notifications You must be signed in to change notification settings

mskv/valid-ts

Repository files navigation

Valid-ts

npm package

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.

The gist of it

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
}

Installation:

npm install --save valid-ts

Documentation

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.

Example projects

There are two simple example projects to look at. Please check their README.mds to build and test them:

Development

npm install

npm test

Contributing

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!

License

MIT

Copyright (c) 2018-present, Michał Podwórny

About

Simple TypeScript validation library with a declarative interface aimed at providing correct type inference of both successful and erroneous validations.

Resources

License

Stars

Watchers

Forks

Packages

No packages published