Skip to content

Update schema generator to better support complex types #60

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 17 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 49 additions & 45 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -1,79 +1,83 @@
version: 2
version: 2.1

refs:
container: &container
orbs:
node: circleci/[email protected]
win: circleci/[email protected]

executors:
linux:
docker:
- image: node:10
- image: node:16
working_directory: ~/repo
windows:
machine:
image: windows-server-2022-gui:current
resource_class: windows.medium
shell: bash.exe

refs:
steps:
- &InstallNode
node/install:
node-version: << parameters.node-version >>
- &PNPM
run:
name: Install PNPM
command: nvm v & node -v && npm -v && npm install -g pnpm
- &Versions
run:
name: Versions
command: node -v && npm -v && yarn -v
command: node -v && npm -v && npx pnpm -v
- &Install
run:
name: Install Dependencies
command: yarn install --pure-lockfile
command: npx pnpm install
- &Build
run:
name: Build
command: yarn build
command: npx pnpm build
- &Test
run:
name: Test
command: yarn ci:test

command: npx pnpm ci:test
- &EnsureNode
run:
name: EnsureNode
command: nvm use << parameters.node-version >> ; npm i -g which-
jobs:
test:
<<: *container
parameters:
os:
type: executor
node-version:
type: string
executor: << parameters.os >>
steps:
- checkout
- when:
condition:
equal:
- 'Linux'
- << parameters.os >>
steps:
- *InstallNode
- *EnsureNode
- *PNPM
- *Versions
- *Install
- *Test
- *Build

publish:
<<: *container
steps:
- checkout
- *Versions
- *Install
- *Test
- *Build
- run:
name: NPM Auth
command: echo "//registry.npmjs.org/:_authToken=${NPM_TOKEN}" > ~/.npmrc
- run:
name: Release
command: |
npx semantic-release && \
npx cross-ci :run \
npx commit-status success Version "'\${PROJECT_VERSION}'"
workflows:
version: 2
all:
jobs:
- test:
matrix:
parameters:
os: [linux, windows]
node-version: [16.15.0]
filters:
branches:
ignore:
- master
- gh-pages
master:
jobs:
- publish:
context: common-env
filters:
branches:
only: master
monthly:
triggers:
- schedule:
cron: '0 0 1 * *'
filters:
branches:
only: master
jobs:
- test:
context: common-env
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,8 @@ lib
environment.toml
.env

src/__tests__/**/*.validator.ts
__tests__/**/*.validator.ts
__tests__/validators/output/
.vscode
tsconfig.tsbuildinfo
.dccache
4 changes: 4 additions & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"

pnpm run -s precommit
2 changes: 2 additions & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
enable-pre-post-script = true
strict-peer-dependencies = false
30 changes: 30 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Logs
logs
*.log

# Runtime data
pids
*.pid
*.seed

# Coverage directory used by tools like istanbul
coverage

# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release

# Dependency directory
node_modules

# Users Environment Variables
.lock-wscript

# Babel build output
lib

# Config files
environment.toml
.env
.vscode
tsconfig.tsbuildinfo
.dccache
6 changes: 1 addition & 5 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -1,5 +1 @@
{
"bracketSpacing": false,
"singleQuote": true,
"trailingComma": "all"
}
{ "semi": true, "trailingComma": "all", "singleQuote": true, "printWidth": 180, "tabWidth": 4, "useTabs": true, "arrowParens": "always", "bracketSameLine": true }
119 changes: 68 additions & 51 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,28 @@ Define a type in `src/Example.ts`, e.g.:

```ts
export default interface ExampleType {
value: string;
/**
* @TJS-format email
*/
email?: string;
/**
* @default 42
*/
answer: number;
value: string;
/**
* @TJS-format email
*/
email?: string;
/**
* @default 42
*/
answer: number;
}
```

To generate a validator, run:

```
npx typescript-json-validator src/Example.ts ExampleType
npx @rkesters/typescript-json-validator src/Example.ts ExampleType
```

This will generate `src/Example.validator.ts`, which you can use:

```ts
import {readFileSync} from 'fs';
import { readFileSync } from 'fs';
import validate from './Example.validator.ts';

const value: unknown = JSON.parse(readFileSync(process.argv[2], 'utf8'));
Expand All @@ -51,60 +51,77 @@ Note that types will be validated automatically, but you can also use annotation
Usage: typescript-json-schema <path-to-typescript-file> <type>

Options:
--help Show help [boolean]
--version Show version number [boolean]
--refs Create shared ref definitions. [boolean] [default: true]
--aliasRefs Create shared ref definitions for the type aliases.
--help Show help [boolean]
--version Show version number [boolean]
--refs Create shared ref definitions.
[boolean] [default: true]
--aliasRefs Create shared ref definitions for the type aliases.
[boolean] [default: false]
--topRef Create a top-level ref definition.
--topRef Create a top-level ref definition.
[boolean] [default: false]
--titles Creates titles in the output schema.
--titles Creates titles in the output schema.
[boolean] [default: false]
--defaultProps Create default properties definitions.
--defaultProps Create default properties definitions.
[boolean] [default: true]
--noExtraProps Disable additional properties in objects by default.
--noExtraProps Disable additional properties in objects by default.
[boolean] [default: false]
--propOrder Create property order definitions.
--propOrder Create property order definitions.
[boolean] [default: false]
--typeOfKeyword Use typeOf keyword (https://goo.gl/DC6sni) for
functions. [boolean] [default: false]
--required Create required array for non-optional properties.
--typeOfKeyword Use typeOf keyword (https://goo.gl/DC6sni) for
functions. [boolean] [default: false]
--required Create required array for non-optional properties.
[boolean] [default: true]
--strictNullChecks Make values non-nullable by default.
--strictNullChecks Make values non-nullable by default.
[boolean] [default: true]
--ignoreErrors Generate even if the program has errors.
--ignoreErrors Generate even if the program has errors.
[boolean] [default: false]
--validationKeywords Provide additional validation keywords to include.
--validationKeywords Provide additional validation keywords to include.
[array] [default: []]
--excludePrivate Exclude private members from the schema.
--excludePrivate Exclude private members from the schema.
[boolean] [default: false]
--uniqueNames Use unique names for type symbols.
--uniqueNames Use unique names for type symbols.
[boolean] [default: false]
--include Further limit tsconfig to include only matching files.
[array]
--rejectDateType Rejects Date fields in type definitions.
--include Further limit tsconfig to include only matching
files. [array]
--rejectDateType Rejects Date fields in type definitions.
[boolean] [default: false]
--id ID of schema. [string] [default: ""]
--uniqueItems Validate `uniqueItems` keyword [boolean] [default: true]
--unicode calculate correct length of strings with unicode pairs
(true by default). Pass false to use .length of strings
that is faster, but gives "incorrect" lengths of strings
with unicode pairs - each unicode pair is counted as two
characters. [boolean] [default: true]
--nullable support keyword "nullable" from Open API 3
specification. [boolean] [default: true]
--format formats validation mode ('fast' by default). Pass 'full'
for more correct and slow validation or false not to
validate formats at all. E.g., 25:00:00 and 2015/14/33
will be invalid time and date in 'full' mode but it will
be valid in 'fast' mode.
--id ID of schema. [string] [default: ""]
--format formats validation mode ('fast' by default). Pass
'full' for more correct and slow validation or false
not to validate formats at all. E.g., 25:00:00 and
2015/14/33 will be invalid time and date in 'full'
mode but it will be valid in 'fast' mode.
[choices: "fast", "full"] [default: "fast"]
--coerceTypes Change data type of data to match type keyword. e.g.
parse numbers in strings [boolean] [default: false]
--collection Process the file as a collection of types, instead of
one single type. [boolean] [default: false]
--useNamedExport Type name is a named export, rather than the default
export of the file [boolean] [default: false]
--coerceTypes Change data type of data to match type keyword. e.g.
parse numbers in strings [boolean] [default: false]
--strictAll Value for all ajv strict options, unless overriden
by other options (https://bit.ly/3flXX6z)
[choices: "log", true, false] [default: false]
--strict Value for all ajv strict options, unless overriden
by other options (https://bit.ly/3flXX6z)
[choices: "log", true, false]
--strictSchema Prevent unknown keywords, (https://bit.ly/3flXX6z)
[choices: "log", true, false]
--strictNumbers Whether to accept NaN and Infinity as number types
during validation, (https://bit.ly/3flXX6z)
[choices: "log", true, false]
--strictTypes Imposes additional restrictions on how type keyword
is used, (https://bit.ly/3rfsetf)
[choices: "log", true, false]
--strictTuples https://bit.ly/3FruEKe [choices: "log", true, false]
--strictRequired Logs warning or throws exception if the property
used in "required" keyword is not defined in
"properties" keyword in the same or some parent
schema relating to the same object (
(https://bit.ly/3rd5Bpf)
[choices: "log", true, false]
--collection Process the file as a collection of types, instead
of one single type. [boolean] [default: false]
--useNamedExport Type name is a named export, rather than the default
export of the file [boolean] [default: false]
--output overrides filename [string]
--separateSchemaFile save json schema to a separate .json file
[boolean] [default: false]
-* [default: []]

```
Expand Down
Loading