/ts-json-schema

Use https://github.com/YousefED/typescript-json-schema 1. TypeScript -> Json Schema and 2. use ajv (master branch) / python jsonschema to import schema and invalidate json data.

Primary LanguageTypeScript

TS-JSON-SCHEMA

Prerequisite

  1. Install yarn. https://yarnpkg.com/zh-Hans/
  2. Install TypeScript globally [recommended way]. yarn add global typescript
  3. Execute yarn or yarn install in project root.

In command line to use typescript-json-schema lib to generate json schema

yarn run typescript-json-schema myString.ts MyString

result in stdout, add -o json.schema to redirect to a file

Test example.ts (master branch) to generate json scheman, and use AJV to validate (in js/ts), interface: myString.ts

  1. Compile TypeScript to JavaScript, yarn run build example.ts
  2. Test, node example.js, result in json.schema

Test example.ts (bdd branch) to generate json schema, and use python to validate, interface: myObject.ts

  1. yarn run build example.ts
  2. node example.js
  3. pip install jsonschema, it not installed
  4. python test.py to validate