Highly composable decoding and validation library

栏目: IT技术 · 发布时间: 6年前

内容简介:A lightweight decoding and validation library.This library provides a set of composable functions for typesafe schema validation and data decoding. Because typescript is staticaly typed this can be particularly useful for message validation in, for example

TS Prove

A lightweight decoding and validation library.

About

This library provides a set of composable functions for typesafe schema validation and data decoding. Because typescript is staticaly typed this can be particularly useful for message validation in, for example, a HTTP route handler by ensuring that the correct data is available.

Install

npm install ts-prove
yarn add ts-prove

Documentation

Example

In the example below, we construct a proof for the type Person<{ name: { first: string, last: string }, age: number }> . This proof is a callback which accepts some unknown data and returns either Failure<[string, unknown]> or Success<[null, Person]>

import P, { ProofType } from 'ts-prove'

const proveName = P.shape({
  first: P.string,
  last: P.optional(P.string),
})

const provePerson = P.shape({
  name: proveName,
  age: P.number,
})

type Person = ProofType<typeof provePerson>
// { name: { first: string, last?: string }, age: number }

provePerson({ name: { first: 'Bob' }, age: 'Incorect string value' }) // [string, unknown]
provePerson({ name: { first: 'Bob' }, age: 10 }) // [null, Person]

This could then be used to validate a payload.

import { isProved } from 'ts-prove'

export const createPerson = (req) => {
  const data = person(req.body)
  if (!isProved(data)) return req.send({ status: 500, body: data[0] })
  return db.createPerson(data[1])
}

db.createPerson = (p: Person) => void

Validation

Every proof is curried and accepts either a function of type (x: unknown) => true | string or some other value. When supplied with a function, a proof returns an instance of itself narrowed by the constraints of the callback. In the example bellow we use this feature to further narrow the number type to numbers betwen 10 and 19.

const teenage = P.number((x) => x > 10 || 'To young')((x) => x < 19 || 'To old')

teenage(9) // ['To young', unknown]
teenage(20) // ['To old', unknown]
teenage(13) // [null, number]

// This could then be used in a structured proof
const teenager = P.shape({ name: nameProof, age: teenage })

Proofs

All the proofs provided by this library are constructed using the function P<ValueType>(): Proof<ValueType> . We can use P to construct are own proofs by providing a return type as the type argument.

P.string = P<string>((x) => typeof x === 'string' || 'Expected string')

Here is a generic proof which does a deep equal comparison using lodash.isequal function.

import P from 'ts-prove'
import isEqual from 'lodash.isequal'

const equalProof = <T extends any>(type: T) => P<T>((input) => isEqual(type, input) | `Not equal`)

API

Type definitions

Type Definition
Sucess<T> [ null , T ]
Failure [ string , unknown ]
Check< T = unknown > (value: T ) => true | string
Proof< R > <T *extends Check<R> | R* >(x: T ): T extends Check<R> ? Proof<R> : Success<R> | Failure

P: Proof

Accepts a return type argument and a validation function that returns a failure of type string or boolean true.

  • string: Proof< string >
  • number: Proof< number >
  • boolean: Proof< boolean >
  • symbol: Proof< symbol >
  • null: Proof< null >
  • undefined: Proof< undefined >

Two utility proofs that always match but that resolve to different types.

  • any: Proof< any >
  • unknown: Proof< unknown >
  • array:< T extends Proof<any> >(proof: T ): Proof <*ProofType< T > []* >

    • Accepts a proof T as an argument and returns a proof for arrays containing type T.

    • P.array(P.string)
      // string[]
  • shape:< T extends { [x: string]: Proof<any> } >(proof: T ): Proof < { [Key in keyof T ]: ProofType< T[Key] > } >

    • Accepts a key value object where every value is a proof and returns a proof for objects of that shape.

    • P.shape({ name: P.string, age: P.number })
      // { name: string, age: number }
  • optional:< T extends Proof<any> >(proof: T ): Proof <*ProofType< T > | undefined* >

    • For use in shapes when you want the return type to indicate and optional value. Optional accepts a proof as an argument and returns union with undefined. For any other cases use or .

    • P.shape({ name: P.string, age: P.optional(P.number) })
      // { name: string, age?: number }
  • or: < T extends Proof<any>[] >(...proofs: T ): Proof < ProofType< T[number] > >

    • Accepts any number of proofs as arguments and returns a union of thoses proofs if any one of them match a given value.

    • P.or(P.string, P.number, P.symbol)
      // string | number | symbol

Helper functions

  • isProved: < T extends any >( x : Failure | Success< T >): x is Success< T >

    • Type guard for the return type of a proof
  • check:( proof : Proof<any>): Check< unknown >

    • Used to convert a proof to a check

Type guard library coming soon...

This project follows the all-contributors specification. Contributions of any kind are welcome!


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

淘宝、天猫网上开店速查速用一本通

淘宝、天猫网上开店速查速用一本通

刘珂 / 北京时代华文书局 / 2015-6 / 39.8

为了帮助众多的新手卖家掌握淘宝天猫网上开店、货源准备、店铺装修、商品拍摄、交易方法、营销推广以及售后服务等知识,本书作者根据自己多年网上开店心得,并结合了多名淘宝五皇冠店主和天猫旗舰店卖家的经验,精心策划编写了本书。 《淘宝、天猫网上开店速查速用一本通:开店、装修、运营、推广完全攻略》将目前最前沿、最流行的营销理念运用到淘宝天猫网上平台,所有技术都在实际应用获得显著效果,并且还在持续创造着惊......一起来看看 《淘宝、天猫网上开店速查速用一本通》 这本书的介绍吧!

在线进制转换器
在线进制转换器

各进制数互转换器

MD5 加密
MD5 加密

MD5 加密工具

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具