All Projects → nwtgck → ts-json-validator

nwtgck / ts-json-validator

Licence: MIT License
JSON Validator for TypeScript - Safer JSON.parse() validating by TypeScript types

Programming Languages

typescript
32286 projects

Projects that are alternatives of or similar to ts-json-validator

efm-certvalidator
Certificate validator for X.509 certificates.
Stars: ✭ 25 (+8.7%)
Mutual labels:  validator
checker
Golang parameter validation, which can replace go-playground/validator, includes ncluding Cross Field, Map, Slice and Array diving, provides readable,flexible, configurable validation.
Stars: ✭ 62 (+169.57%)
Mutual labels:  validator
apispec
A Common Lisp library for handling Web API requests and responses.
Stars: ✭ 26 (+13.04%)
Mutual labels:  validator
strcode
Structure your code better.
Stars: ✭ 42 (+82.61%)
Mutual labels:  structure
focus-single
Single repo demo project using GoFrame.
Stars: ✭ 26 (+13.04%)
Mutual labels:  structure
ThinCreditCard
💳 Simple way to add a credit card
Stars: ✭ 40 (+73.91%)
Mutual labels:  validator
openapi-lint-vscode
OpenAPI 2.0/3.0.x intellisense, validator, linter, converter and resolver extension for Visual Studio Code
Stars: ✭ 47 (+104.35%)
Mutual labels:  validator
PlaneTR3D
[ICCV'21] PlaneTR: Structure-Guided Transformers for 3D Plane Recovery
Stars: ✭ 58 (+152.17%)
Mutual labels:  structure
directory-structure
📦 Print a directory tree structure in your Python code.
Stars: ✭ 40 (+73.91%)
Mutual labels:  structure
Version3-1
Version 2020 (3.1) of Chem4Word - A Chemistry Add-In for Microsoft Word
Stars: ✭ 14 (-39.13%)
Mutual labels:  structure
verum-php
Server-Side Validation Library for PHP
Stars: ✭ 17 (-26.09%)
Mutual labels:  validator
NZ-Bank-Account-Validator
A small, zero dependency NZ bank account validation library that runs everywhere.
Stars: ✭ 15 (-34.78%)
Mutual labels:  validator
validation
Validation on Laravel 5.X|6.X|7.X|8.X
Stars: ✭ 26 (+13.04%)
Mutual labels:  validator
morpheus
Red pill or blue pill? - A collection of Desmos testnets
Stars: ✭ 22 (-4.35%)
Mutual labels:  validator
cti-pattern-validator
OASIS TC Open Repository: Validate patterns used to express cyber observable content in STIX Indicators
Stars: ✭ 18 (-21.74%)
Mutual labels:  validator
dockerfile-utils
A library and command line interface for formatting and linting Dockerfiles.
Stars: ✭ 17 (-26.09%)
Mutual labels:  validator
thai-citizen-id-validator
🦉 Validate Thai Citizen ID with 0 dependencies 🇹🇭
Stars: ✭ 35 (+52.17%)
Mutual labels:  validator
jsonlint
Lightweight command-line tool for validating JSON
Stars: ✭ 27 (+17.39%)
Mutual labels:  validator
js-form-validator
Javascript form validation. Pure JS. No jQuery
Stars: ✭ 38 (+65.22%)
Mutual labels:  validator
excel validator
Python script to validate data in Excel files
Stars: ✭ 14 (-39.13%)
Mutual labels:  validator

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

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

Basic Usage

import {nul, bool, num, str, literal, opt, arr, tuple, obj, union, TsType, validatingParse} from 'ts-json-validator';

// Create a format
const personFormat = obj({
  name: str,
  age: num
});

// Generate Person type
// IMPORTANT: Type is derived at compile-time. Just a write format once!
type Person = TsType<typeof personFormat>;


// Safer parse than JSON.parse()
const p1: Person | undefined = validatingParse(personFormat, '{"name": "jack", "age": 8}');
const myObj: {[key: string]: any} = {name: "jack", age: 8};
const p2: Person | undefined = validate(personFormat, myObj);
// => not undefined
const myObj2: any = {name: "jack", age: "this is a text"};
isValid(personFormat.runtimeType, myObj2);
// => 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

import {nul, bool, num, str, literal, opt, arr, tuple, obj, union, TsType, validatingParse} from 'ts-json-validator';

// Create a format
const myObj1Format = obj({
  // String array - string[]
  myStrs: arr(str),
  // Nested object - (myFlag: boolean)
  myObj: obj({
    myFlag: bool
  }),
  // Union Type - string | number | bool
  strOrNumOrBool: union(str, num, bool),
  // Object array - {prop1: number, prop2: string}[]
  myObjs: arr(obj({
    prop1: num,
    prop2: str
  })),
  // Optional property(myOptional?: number)
  myOptional: opt(num),
  // Literal Type - (myLiteral: 'MY_LITERAL')
  myLiteral: literal('MY_LITERAL' as const),
  // Literal Union Type - ('red' | 'blue')
  myLitUnion: union(literal('red' as const), literal('blue' as const)),
  // Nullable - (string | null)
  myNullable: union(str, nul),
  // Tuple - [string, boolean, number]
  myTuple: tuple(str, bool, num),
});


// Generate MyObj1 type
// IMPORTANT: Type is derived at compile-time. Just write a format once!
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.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].