json-schema-to-ts
Advanced tools
Comparing version 1.1.1 to 1.2.0
{ | ||
"name": "json-schema-to-ts", | ||
"version": "1.1.1", | ||
"version": "1.2.0", | ||
"description": "Infer typescript types from your JSON schemas!", | ||
@@ -5,0 +5,0 @@ "main": "src/index.ts", |
108
README.md
@@ -76,55 +76,2 @@ # Stop typing twice 🙅♂️ | ||
# Why use json-schema-to-ts ? | ||
There are many librearies out there like [zod]() (type first validation) our . Feel free to check them, they may suit your needs better! | ||
However, if you prefer to stick to JSON schemas (and there are good reasons to do so) AND if you can define your schemas in TS (the `as const` statement doesn't work on JSON imports... for now !), then `json-schema-to-ts` is made for you ! | ||
Doesn't impact, what's lighter than a dev-only library ? | ||
Let's face it, making small errors in JSON schemas is easy! | ||
Which has the benefits: | ||
- 0 impact on compiled code: What's lighter than a dev dependancy ? | ||
- 📝 Reduce redundancy: Writing less code is always nice! | ||
- 🤝 Enforce consistency: Spot typos, string instead of enum or confusing additionalItems and additionalProperties... | ||
- ⚡️ Be right first-time: With instantaneous feedback given by TS ! | ||
- Types are easier to read than shemas. Help you write better schemas | ||
Helps you write complex schemas ! | ||
For instance, is it obvious that the following schema can never be validated ? | ||
```typescript | ||
const addressSchema = { | ||
type: "object", | ||
allOf: [ | ||
{ | ||
properties: { | ||
street: { type: "string" }, | ||
city: { type: "string" }, | ||
state: { type: "string" }, | ||
}, | ||
required: ["street", "city", "state"], | ||
}, | ||
{ | ||
properties: { | ||
type: { enum: ["residential", "business"] }, | ||
}, | ||
}, | ||
], | ||
additionalProperties: false, | ||
} as const; | ||
``` | ||
Well, it is with `FromSchema` ! | ||
```typescript | ||
type Address = FromSchema<typeof addressSchema>; | ||
// => never 🙌 | ||
``` | ||
Extensively tested against the [ajv library](https://github.com/ajv-validator/ajv). Trust! | ||
# Docs | ||
@@ -203,3 +150,3 @@ | ||
(For `object` and `array` types, properties like `required` or `additionalItems` will work 🙌) | ||
> For `object` and `array` types, properties like `required` or `additionalItems` will apply 🙌 | ||
@@ -375,3 +322,3 @@ ### Arrays | ||
Because TypeScript doesn't have [refinment types](https://en.wikipedia.org/wiki/Refinement_type), `oneOf` will behave strictly the same as `anyOf`. | ||
Because TypeScript misses [refinment types](https://en.wikipedia.org/wiki/Refinement_type), `FromSchema` will use the `oneOf` keyword in the same way as `anyOf`: | ||
@@ -382,4 +329,13 @@ ```typescript | ||
oneOf: [ | ||
{ properties: { name: { type: "string" } }, required: ["name"] }, | ||
{ properties: { color: { enum: ["black", "brown", "white"] } } }, | ||
{ | ||
properties: { | ||
name: { type: "string" }, | ||
}, | ||
required: ["name"], | ||
}, | ||
{ | ||
properties: { | ||
color: { enum: ["black", "brown", "white"] }, | ||
}, | ||
}, | ||
], | ||
@@ -397,3 +353,3 @@ } as const; | ||
// => FromSchema cannot detect the following invalid obj 😱 | ||
// => FromSchema will not detect the following invalid obj 😱 | ||
const invalidCat: Cat = { name: "Garfield" }; | ||
@@ -404,6 +360,36 @@ ``` | ||
...Coming soon 😃 | ||
```typescript | ||
const addressSchema = { | ||
type: "object", | ||
allOf: [ | ||
{ | ||
properties: { | ||
address: { type: "string" }, | ||
city: { type: "string" }, | ||
state: { type: "string" }, | ||
}, | ||
required: ["address", "city", "state"], | ||
}, | ||
{ | ||
properties: { | ||
type: { enum: ["residential", "business"] }, | ||
}, | ||
}, | ||
], | ||
} as const; | ||
### If/Else | ||
type Address = FromSchema<typeof addressSchema>; | ||
// => { | ||
// [x: string]: unknown; | ||
// address: string; | ||
// city: string; | ||
// state: string; | ||
// type?: "residential" | "business"; | ||
// } | ||
``` | ||
...Coming soon 😃 | ||
### Not & If/Then/Else | ||
For the same reason as `oneOf` (missing refinment types), I feel like implementing the `not` and the `if/then/else` keywords in `FromSchema` would lead into a rabbit hole... | ||
But I may be wrong ! If you have a use case and you think that it can be implemented, feel free to submit a PR 🤗 |
@@ -10,2 +10,3 @@ import { Litteral, Any, Never } from "../meta-types"; | ||
import { ParseOneOfSchema } from "./oneOf"; | ||
import { ParseAllOfSchema } from "./allOf"; | ||
@@ -26,2 +27,3 @@ export type ParseSchema<S> = { | ||
oneOf: ParseOneOfSchema<S>; | ||
allOf: ParseAllOfSchema<S>; | ||
}[InferSchemaType<S>]; | ||
@@ -33,2 +35,4 @@ | ||
? "never" | ||
: "allOf" extends keyof S | ||
? "allOf" | ||
: "oneOf" extends keyof S | ||
@@ -35,0 +39,0 @@ ? "oneOf" |
@@ -27,5 +27,3 @@ import { Object, Any, Never, Union, Error } from "../meta-types"; | ||
type GetOpenProps<S> = "properties" extends keyof S | ||
? Any | ||
: "additionalProperties" extends keyof S | ||
type GetOpenProps<S> = "additionalProperties" extends keyof S | ||
? "patternProperties" extends keyof S | ||
@@ -32,0 +30,0 @@ ? AdditionalAndPatternProps< |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
45373
45
1172
390