项目作者: nwtgck

项目描述 :
JSON Validator for TypeScript - Safer JSON.parse() validating by TypeScript types
高级语言: TypeScript
项目地址: git://github.com/nwtgck/ts-json-validator.git
创建时间: 2019-05-13T08:57:41Z
项目社区:https://github.com/nwtgck/ts-json-validator

开源协议:MIT License

下载


ts-json-validator

CircleCI

Safer JSON.parse() validating by TypeScript types

Write a format of JSON once, Derive the type automatically at compile-time.

Installation

  1. npm install -S git+https://github.com/nwtgck/ts-json-validator#v0.1.2

Basic Usage

  1. import {nul, bool, num, str, literal, opt, arr, tuple, obj, union, TsType, validatingParse} from 'ts-json-validator';
  2. // Create a format
  3. const personFormat = obj({
  4. name: str,
  5. age: num
  6. });
  7. // Generate Person type
  8. // IMPORTANT: Type is derived at compile-time. Just a write format once!
  9. type Person = TsType<typeof personFormat>;
  10. // Safer parse than JSON.parse()
  11. const p1: Person | undefined = validatingParse(personFormat, '{"name": "jack", "age": 8}');
  1. const myObj: {[key: string]: any} = {name: "jack", age: 8};
  2. const p2: Person | undefined = validate(personFormat, myObj);
  3. // => not undefined
  1. const myObj2: any = {name: "jack", age: "this is a text"};
  2. isValid(personFormat.runtimeType, myObj2);
  3. // => false (because type of age should be number)

Core feature

The main feature is that type Person is automatically derived. So you just define the format/structure object of JSON such as personFormat. Then you can get a type for free.

Usage of Array, nested objects, Literal Types, Union Types and Tuples

  1. import {nul, bool, num, str, literal, opt, arr, tuple, obj, union, TsType, validatingParse} from 'ts-json-validator';
  2. // Create a format
  3. const myObj1Format = obj({
  4. // String array - string[]
  5. myStrs: arr(str),
  6. // Nested object - (myFlag: boolean)
  7. myObj: obj({
  8. myFlag: bool
  9. }),
  10. // Union Type - string | number | bool
  11. strOrNumOrBool: union(str, num, bool),
  12. // Object array - {prop1: number, prop2: string}[]
  13. myObjs: arr(obj({
  14. prop1: num,
  15. prop2: str
  16. })),
  17. // Optional property(myOptional?: number)
  18. myOptional: opt(num),
  19. // Literal Type - (myLiteral: 'MY_LITERAL')
  20. myLiteral: literal('MY_LITERAL' as const),
  21. // Literal Union Type - ('red' | 'blue')
  22. myLitUnion: union(literal('red' as const), literal('blue' as const)),
  23. // Nullable - (string | null)
  24. myNullable: union(str, nul),
  25. // Tuple - [string, boolean, number]
  26. myTuple: tuple(str, bool, num),
  27. });
  28. // Generate MyObj1 type
  29. // IMPORTANT: Type is derived at compile-time. Just write a format once!
  30. type MyObj1 = TsType<typeof myObj1Format>;

In Union Type, T1 | T2 | ... | T64 is supported. In tuple, [T1, T2, ..., T64] is supported.

Type-safety

You can find errors at compile-time, not runtime!

Wrong type in the array

myStrs should be an string array, but 128 is included.

Missing property

myStrs is required, but missing.

Wrong Literal Type

myLiteral should be 'MY_LITERAL' type, but found 'YOUR LITERAL'

Unknown property

Property somethingElse is not defined in myObj1Format.