Giter Site home page Giter Site logo

ts-validator's Introduction

Typescript Validator

A type-safe runtime validator for parsing unsafe data.

This library provides type-checked validator functions that help you take unsafe data (e.g. data from an API request) and validate it against your Typescript interfaces.

When used correctly, Typescript will throw and error if your validator function does not match the type that you are trying to validate.

Getting Started

Install from npm:

npm install --save typescript-validator

Given a type interface:

interface User {
	id: number,
	name?: string,
	email: string,
	workspaces: Array<string>
}

You can create a runtime validator function like this:

import * as validate from "typescript-validator"

const validator = validate.object<User>({
	id: validate.number(),
	name?: validate.optional(validate.string()),
	email: validate.string(),
	workspaces: validate.array(validate.string())
})

const valid = validator({id: 1, email: "hello", workspaces: []})
// true

The nice thing about this abstraction is that the type-checker will ensure that you've validated all fields of the type interface with the correct validator.

For example, here are some errors the type checker will help you with:

const validator = validate.object<User>({
	id: validate.number(),
	// name: validate.optional(validate.string()),  <-- "Property 'name' is missing in type"
	email: validate.string(),
	workspaces: validate.array(validate.string())
})

const validator = validate.object<User>({
	id: validate.number(),
	name: validate.optional(validate.string()),
	email: validate.number(), //  <-- Type 'string' is not assignable to type 'number'.
	workspaces: validate.array(validate.string())
})

You can easily create your own validators as well -- its just a function that returns a boolean. As you can see, we haven't verified that the email string is actually an email or the number of workspaces is not empty. We can easily write these functions ourselves.

const validator = validate.object<User>({
	id: validate.number(),
	name?: validate.optional(validate.string()),
	email: (email: string) => {
		return validate.string()(email) && /[^@]+@[^\.]+\.[a-zA-Z]+/.test(value),
	workspaces: (value: Array<string>) => {
		return validate.array((validate.string()))(value) && value.length > 0
	}
})

Happy hacking ๐Ÿ‰

ts-validator's People

Contributors

ccorcos avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.