json-schema-to-ts
Advanced tools
Comparing version 2.3.0 to 2.4.0
@@ -0,1 +1,2 @@ | ||
import { JSONSchema7Reference } from "../index"; | ||
import { DeserializationPattern } from "./deserializationPattern"; | ||
@@ -5,3 +6,3 @@ export declare type FromSchemaOptions = { | ||
parseIfThenElseKeywords?: boolean; | ||
definitionsPath?: string; | ||
references?: JSONSchema7Reference[] | false; | ||
deserialize?: DeserializationPattern[] | false; | ||
@@ -12,4 +13,4 @@ }; | ||
parseIfThenElseKeywords: false; | ||
definitionsPath: "$defs"; | ||
references: false; | ||
deserialize: false; | ||
}; |
export { DeserializationPattern } from "./deserializationPattern"; | ||
export { JSONSchema7 } from "./jsonSchema7"; | ||
export { JSONSchema7, JSONSchema7Reference } from "./jsonSchema7"; | ||
export { FromSchemaOptions, FromSchemaDefaultOptions, } from "./fromSchemaOptions"; |
@@ -28,1 +28,4 @@ import { JSONSchema7 as $JSONSchema7 } from "json-schema"; | ||
}); | ||
export declare type JSONSchema7Reference = JSONSchema7 & { | ||
$id: string; | ||
}; |
import { M } from "ts-algebra"; | ||
import { O } from "ts-toolbelt"; | ||
import type { JSONSchema7 as $JSONSchema7, FromSchemaOptions, FromSchemaDefaultOptions } from "./definitions"; | ||
import type { JSONSchema7 as $JSONSchema7, JSONSchema7Reference as $JSONSchema7Reference, FromSchemaOptions, FromSchemaDefaultOptions } from "./definitions"; | ||
import type { ParseSchema } from "./parse-schema"; | ||
@@ -9,3 +9,4 @@ import type { ParseOptions } from "./parse-options"; | ||
export declare type JSONSchema7 = $JSONSchema7 | Readonly<$JSONSchema7>; | ||
export declare type JSONSchema7Reference = $JSONSchema7Reference | Readonly<$JSONSchema7Reference>; | ||
export declare type JSONSchema = JSONSchema7; | ||
export declare type FromSchema<S extends JSONSchema, O extends FromSchemaOptions = FromSchemaDefaultOptions, W extends $JSONSchema7 = S extends O.Object ? Writable<S> : S> = M.$Resolve<ParseSchema<W, ParseOptions<W, O>>>; |
@@ -0,10 +1,17 @@ | ||
import { L } from "ts-toolbelt"; | ||
import { Writable } from "./utils"; | ||
import { JSONSchema7, FromSchemaOptions, FromSchemaDefaultOptions, DeserializationPattern } from "./definitions"; | ||
import { ParseSchemaOptions } from "./parse-schema"; | ||
export declare type ParseOptions<S extends JSONSchema7, O extends FromSchemaOptions, N extends Omit<ParseSchemaOptions, "definitions"> = { | ||
import { JSONSchema7Reference } from "./index"; | ||
export declare type ParseReferences<S extends JSONSchema7Reference[], R extends Record<string, JSONSchema7> = {}> = { | ||
continue: ParseReferences<L.Tail<S>, R & { | ||
[key in L.Head<S>["$id"]]: Writable<L.Head<S>>; | ||
}>; | ||
stop: R; | ||
}[S extends [any, ...any[]] ? "continue" : "stop"]; | ||
export declare type ParseOptions<S extends JSONSchema7, O extends FromSchemaOptions> = { | ||
parseNotKeyword: O["parseNotKeyword"] extends boolean ? O["parseNotKeyword"] : FromSchemaDefaultOptions["parseNotKeyword"]; | ||
parseIfThenElseKeywords: O["parseIfThenElseKeywords"] extends boolean ? O["parseIfThenElseKeywords"] : FromSchemaDefaultOptions["parseIfThenElseKeywords"]; | ||
definitionsPath: O["definitionsPath"] extends string ? O["definitionsPath"] : FromSchemaDefaultOptions["definitionsPath"]; | ||
rootSchema: S; | ||
references: O["references"] extends JSONSchema7Reference[] ? ParseReferences<O["references"]> : {}; | ||
deserialize: O["deserialize"] extends DeserializationPattern[] | false ? O["deserialize"] : FromSchemaDefaultOptions["deserialize"]; | ||
}> = N & { | ||
definitions: S extends Record<N["definitionsPath"], Record<string, JSONSchema7>> ? S[N["definitionsPath"]] : {}; | ||
}; |
@@ -15,12 +15,12 @@ import { M } from "ts-algebra"; | ||
import { NullableSchema, ParseNullableSchema } from "./nullable"; | ||
import { DefinitionSchema, ParseDefinitionSchema } from "./definition"; | ||
import { ReferenceSchema, ParseReferenceSchema } from "./references"; | ||
export declare type ParseSchemaOptions = { | ||
parseNotKeyword: boolean; | ||
parseIfThenElseKeywords: boolean; | ||
definitionsPath: string; | ||
definitions: Record<string, JSONSchema7>; | ||
rootSchema: JSONSchema7; | ||
references: Record<string, JSONSchema7>; | ||
deserialize: DeserializationPattern[] | false; | ||
}; | ||
export declare type ParseSchema<S extends JSONSchema7, O extends ParseSchemaOptions, P = JSONSchema7 extends S ? M.Any : S extends true | string ? M.Any : S extends false ? M.Never : S extends NullableSchema ? ParseNullableSchema<S, O> : S extends DefinitionSchema<O> ? ParseDefinitionSchema<S, O> : And<DoesExtend<O["parseIfThenElseKeywords"], true>, DoesExtend<S, IfThenElseSchema>> extends true ? S extends IfThenElseSchema ? ParseIfThenElseSchema<S, O> : never : And<DoesExtend<O["parseNotKeyword"], true>, DoesExtend<S, NotSchema>> extends true ? S extends NotSchema ? ParseNotSchema<S, O> : never : S extends AllOfSchema ? ParseAllOfSchema<S, O> : S extends OneOfSchema ? ParseOneOfSchema<S, O> : S extends AnyOfSchema ? ParseAnyOfSchema<S, O> : S extends EnumSchema ? ParseEnumSchema<S, O> : S extends ConstSchema ? ParseConstSchema<S, O> : S extends MultipleTypesSchema ? ParseMultipleTypesSchema<S, O> : S extends SingleTypeSchema ? ParseSingleTypeSchema<S, O> : M.Any> = O extends { | ||
export declare type ParseSchema<S extends JSONSchema7, O extends ParseSchemaOptions, P = JSONSchema7 extends S ? M.Any : S extends true | string ? M.Any : S extends false ? M.Never : S extends NullableSchema ? ParseNullableSchema<S, O> : S extends ReferenceSchema ? ParseReferenceSchema<S, O> : And<DoesExtend<O["parseIfThenElseKeywords"], true>, DoesExtend<S, IfThenElseSchema>> extends true ? S extends IfThenElseSchema ? ParseIfThenElseSchema<S, O> : never : And<DoesExtend<O["parseNotKeyword"], true>, DoesExtend<S, NotSchema>> extends true ? S extends NotSchema ? ParseNotSchema<S, O> : never : S extends AllOfSchema ? ParseAllOfSchema<S, O> : S extends OneOfSchema ? ParseOneOfSchema<S, O> : S extends AnyOfSchema ? ParseAnyOfSchema<S, O> : S extends EnumSchema ? ParseEnumSchema<S, O> : S extends ConstSchema ? ParseConstSchema<S, O> : S extends MultipleTypesSchema ? ParseMultipleTypesSchema<S, O> : S extends SingleTypeSchema ? ParseSingleTypeSchema<S, O> : M.Any> = O extends { | ||
deserialize: DeserializationPattern[]; | ||
} ? M.$Intersect<DeserializeSchema<S, O>, P> : P; |
export { And } from "./and"; | ||
export { DoesExtend, IsObject } from "./extends"; | ||
export { DeepGet } from "./get"; | ||
export { HasKeyIn } from "./hasKeyIn"; | ||
export { Merge } from "./merge"; | ||
export { Readonly } from "./readonly"; | ||
export { Writable } from "./writable"; | ||
export { DeepReadonly as Readonly } from "./readonly"; | ||
export { DeepWritable as Writable } from "./writable"; |
import { O } from "ts-toolbelt"; | ||
export declare type Readonly<T> = T extends O.Object ? { | ||
readonly [P in keyof T]: Readonly<T[P]>; | ||
export declare type DeepReadonly<T> = T extends O.Object ? { | ||
readonly [P in keyof T]: DeepReadonly<T[P]>; | ||
} : T; |
@@ -1,3 +0,3 @@ | ||
export declare type Writable<T> = { | ||
-readonly [P in keyof T]: Writable<T[P]>; | ||
export declare type DeepWritable<T> = { | ||
-readonly [P in keyof T]: DeepWritable<T[P]>; | ||
}; |
{ | ||
"name": "json-schema-to-ts", | ||
"version": "2.3.0", | ||
"version": "2.4.0", | ||
"description": "Infer typescript types from your JSON schemas!", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -85,3 +85,3 @@ <img src="assets/header-round-medium.png" width="100%" align="center" /> | ||
- 🍸 **DRYness**: Less code means less embarrassing typos | ||
- 🤝 **Consistency**: See that `string` that you used instead of an `enum`? Or this `additionalProperties` you confused with `additionalItems`? Or forgot entirely? Well, `json-schema-to-ts` does! | ||
- 🤝 **Real-time consistency**: See that `string` that you used instead of an `enum`? Or this `additionalProperties` you confused with `additionalItems`? Or forgot entirely? Well, `json-schema-to-ts` does! | ||
- 🔧 **Reliability**: `FromSchema` is extensively tested against [AJV](https://github.com/ajv-validator/ajv), and covers all the use cases that can be handled by TS for now\* | ||
@@ -139,2 +139,3 @@ - 🏋️♂️ **Help on complex schemas**: Get complex schemas right first time with instantaneous typing feedbacks! For instance, it's not obvious the following schema can never be validated: | ||
- [Definitions](#definitions) | ||
- [References](#references) | ||
- [Deserialization](#deserialization) | ||
@@ -401,3 +402,3 @@ - [FAQ](#frequently-asked-questions) | ||
For the moment, `FromSchema` will use the `oneOf` keyword in the same way as `anyOf`: | ||
`FromSchema` will parse the `oneOf` keyword in the same way as `anyOf`: | ||
@@ -616,3 +617,3 @@ ```typescript | ||
> `FromSchema` computes the resulting type as `(If ∩ Then) ∪ (¬If ∩ Else)`. While correct in theory, remember that the `not` keyword is not perfectly assimilated, which may become an issue in some complex schemas. | ||
> ☝️ `FromSchema` computes the resulting type as `(If ∩ Then) ∪ (¬If ∩ Else)`. While correct in theory, remember that the `not` keyword is not perfectly assimilated, which may become an issue in some complex schemas. | ||
@@ -622,43 +623,65 @@ ### Definitions | ||
```typescript | ||
const personSchema = { | ||
const userSchema = { | ||
type: "object", | ||
properties: { | ||
firstName: { $ref: "#/$defs/name" }, | ||
lastName: { $ref: "#/$defs/name" }, | ||
name: { $ref: "#/$defs/name" }, | ||
age: { $ref: "#/$defs/age" }, | ||
}, | ||
required: ["firstName", "lastName"], | ||
required: ["name", "age"], | ||
additionalProperties: false, | ||
$defs: { | ||
name: { type: "string" }, | ||
age: { type: "integer" }, | ||
}, | ||
} as const; | ||
type Person = FromSchema<typeof personSchema>; | ||
type User = FromSchema<typeof userSchema>; | ||
// => { | ||
// firstName: string; | ||
// lastName: string; | ||
// name: string; | ||
// age: number; | ||
// } | ||
``` | ||
You can specify a different definitions path with the `definitionsPath` option: | ||
> ☝️ Wether in definitions or references, `FromSchema` will not work on recursive schemas for now. | ||
### References | ||
Unlink run-time validator classes like [AJV](https://github.com/ajv-validator/ajv), TS types cannot withhold internal states. Thus, they cannot keep any identified schemas in memory. | ||
But you can hydrate them via the `references` option: | ||
```typescript | ||
const personSchema = { | ||
const userSchema = { | ||
$id: "http://example.com/schemas/user.json", | ||
type: "object", | ||
properties: { | ||
firstName: { $ref: "#/definitions/name" }, | ||
lastName: { $ref: "#/definitions/name" }, | ||
name: { type: "string" }, | ||
age: { type: "integer" }, | ||
}, | ||
required: ["firstName", "lastName"], | ||
required: ["name", "age"], | ||
additionalProperties: false, | ||
definitions: { | ||
name: { type: "string" }, | ||
} as const; | ||
const usersSchema = { | ||
type: "array", | ||
items: { | ||
$ref: "http://example.com/schemas/user.json", | ||
}, | ||
} as const; | ||
type Person = FromSchema< | ||
typeof personSchema, | ||
{ definitionsPath: "definitions" } | ||
type Users = FromSchema< | ||
typeof usersSchema, | ||
{ references: [typeof userSchema] } | ||
>; | ||
// => Will work 🙌 | ||
// => { | ||
// name: string; | ||
// age: string; | ||
// }[] | ||
const anotherUsersSchema = { | ||
$id: "http://example.com/schemas/users.json", | ||
type: "array", | ||
items: { $ref: "user.json" }, | ||
} as const; | ||
// => Will work as well 🙌 | ||
``` | ||
@@ -690,3 +713,3 @@ | ||
type ReceivedUser = FromSchema< | ||
type User = FromSchema< | ||
typeof userSchema, | ||
@@ -693,0 +716,0 @@ { |
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
45756
69
464
742