json-schema-to-ts
Advanced tools
Comparing version 3.0.1 to 3.1.0
@@ -33,2 +33,3 @@ export declare const $JSONSchema: unique symbol; | ||
additionalProperties?: JSONSchema; | ||
unevaluatedProperties?: JSONSchema; | ||
dependencies?: Readonly<Record<string, JSONSchema | readonly string[]>>; | ||
@@ -35,0 +36,0 @@ propertyNames?: JSONSchema; |
@@ -11,3 +11,3 @@ import type { M } from "ts-algebra"; | ||
[KEY in keyof OBJECT_SCHEMA["properties"]]: ParseSchema<OBJECT_SCHEMA["properties"][KEY], OPTIONS>; | ||
}, GetRequired<OBJECT_SCHEMA, OPTIONS>, GetOpenProps<OBJECT_SCHEMA, OPTIONS>> : M.$Object<{}, GetRequired<OBJECT_SCHEMA, OPTIONS>, GetOpenProps<OBJECT_SCHEMA, OPTIONS>>; | ||
}, GetRequired<OBJECT_SCHEMA, OPTIONS>, GetOpenProps<OBJECT_SCHEMA, OPTIONS>, GetClosedOnResolve<OBJECT_SCHEMA>> : M.$Object<{}, GetRequired<OBJECT_SCHEMA, OPTIONS>, GetOpenProps<OBJECT_SCHEMA, OPTIONS>, GetClosedOnResolve<OBJECT_SCHEMA>>; | ||
type GetRequired<OBJECT_SCHEMA extends ObjectSchema, OPTIONS extends ParseSchemaOptions> = (OBJECT_SCHEMA extends Readonly<{ | ||
@@ -29,2 +29,5 @@ required: ReadonlyArray<string>; | ||
}> ? PatternProps<OBJECT_SCHEMA["patternProperties"], OPTIONS> : M.Any; | ||
type GetClosedOnResolve<OBJECT_SCHEMA extends ObjectSchema> = OBJECT_SCHEMA extends Readonly<{ | ||
unevaluatedProperties: false; | ||
}> ? true : false; | ||
type PatternProps<PATTERN_PROPERTY_SCHEMAS extends Readonly<Record<string, JSONSchema>>, OPTIONS extends ParseSchemaOptions> = M.$Union<{ | ||
@@ -31,0 +34,0 @@ [KEY in keyof PATTERN_PROPERTY_SCHEMAS]: ParseSchema<PATTERN_PROPERTY_SCHEMAS[KEY], OPTIONS>; |
@@ -9,8 +9,10 @@ import { JSONSchema } from "../definitions"; | ||
}> : SCHEMA extends boolean ? SCHEMA : Omit<SCHEMA, "additionalItems">; | ||
type ParentSchemaOverrides = { | ||
type RemoveInvalidAdditionalProperties<SCHEMA extends JSONSchema> = SCHEMA extends Readonly<{ | ||
additionalProperties: JSONSchema; | ||
}> ? SCHEMA extends Readonly<{ | ||
properties: Readonly<Record<string, JSONSchema>>; | ||
}> ? SCHEMA : SCHEMA & Readonly<{ | ||
properties: {}; | ||
additionalProperties: true; | ||
required: []; | ||
}; | ||
export type MergeSubSchema<PARENT_SCHEMA extends JSONSchema, SUB_SCHEMA extends JSONSchema, CLEANED_SUB_SCHEMA extends JSONSchema = RemoveInvalidAdditionalItems<SUB_SCHEMA>, DEFAULTED_SUB_SCHEMA extends JSONSchema = Omit<ParentSchemaOverrides, keyof CLEANED_SUB_SCHEMA> & CLEANED_SUB_SCHEMA> = Omit<PARENT_SCHEMA, keyof DEFAULTED_SUB_SCHEMA> & DEFAULTED_SUB_SCHEMA; | ||
}> : SCHEMA extends boolean ? SCHEMA : Omit<SCHEMA, "additionalProperties">; | ||
export type MergeSubSchema<PARENT_SCHEMA extends JSONSchema, SUB_SCHEMA extends JSONSchema, CLEANED_SUB_SCHEMA extends JSONSchema = RemoveInvalidAdditionalProperties<RemoveInvalidAdditionalItems<SUB_SCHEMA>>> = Omit<PARENT_SCHEMA, keyof CLEANED_SUB_SCHEMA | "additionalProperties" | "patternProperties" | "unevaluatedProperties" | "required" | "additionalItems"> & CLEANED_SUB_SCHEMA; | ||
export {}; |
{ | ||
"name": "json-schema-to-ts", | ||
"version": "3.0.1", | ||
"version": "3.1.0", | ||
"description": "Infer typescript types from your JSON schemas!", | ||
@@ -29,3 +29,3 @@ "files": [ | ||
"@babel/runtime": "^7.18.3", | ||
"ts-algebra": "^1.2.2" | ||
"ts-algebra": "^2.0.0" | ||
}, | ||
@@ -45,3 +45,3 @@ "devDependencies": { | ||
"@zerollup/ts-transform-paths": "^1.7.18", | ||
"ajv": "^8.10.0", | ||
"ajv": "^8.13.0", | ||
"babel-plugin-module-resolver": "^4.1.0", | ||
@@ -48,0 +48,0 @@ "dependency-cruiser": "^11.18.0", |
@@ -376,5 +376,5 @@ <img src="assets/header-round-medium.png" width="100%" align="center" /> | ||
`FromSchema` partially supports the `additionalProperties` and `patternProperties` keywords: | ||
`FromSchema` partially supports the `additionalProperties`, `patternProperties` and `unevaluatedProperties` keywords: | ||
- `additionalProperties` can be used to deny additional properties. | ||
- `additionalProperties` and `unevaluatedProperties` can be used to deny additional properties. | ||
@@ -391,2 +391,25 @@ ```typescript | ||
```typescript | ||
const closedObjectSchema = { | ||
type: "object", | ||
allOf: [ | ||
{ | ||
properties: { | ||
foo: { type: "string" }, | ||
}, | ||
required: ["foo"], | ||
}, | ||
{ | ||
properties: { | ||
bar: { type: "number" }, | ||
}, | ||
}, | ||
], | ||
unevaluatedProperties: false, | ||
} as const; | ||
type Object = FromSchema<typeof closedObjectSchema>; | ||
// => { foo: string; bar?: number; } | ||
``` | ||
- Used on their own, `additionalProperties` and/or `patternProperties` can be used to type unnamed properties. | ||
@@ -410,4 +433,33 @@ | ||
- However, when used in combination with the `properties` keyword, extra properties will always be typed as `unknown` to avoid conflicts. | ||
However: | ||
- When used in combination with the `properties` keyword, extra properties will always be typed as `unknown` to avoid conflicts. | ||
```typescript | ||
const mixedObjectSchema = { | ||
type: "object", | ||
properties: { | ||
foo: { enum: ["bar", "baz"] }, | ||
}, | ||
additionalProperties: { type: "string" }, | ||
} as const; | ||
type Object = FromSchema<typeof mixedObjectSchema>; | ||
// => { [x: string]: unknown; foo?: "bar" | "baz"; } | ||
``` | ||
- Due to its context-dependent nature, `unevaluatedProperties` does not type extra-properties when used on its own. Use `additionalProperties` instead. | ||
```typescript | ||
const openObjectSchema = { | ||
type: "object", | ||
unevaluatedProperties: { | ||
type: "boolean", | ||
}, | ||
} as const; | ||
type Object = FromSchema<typeof openObjectSchema>; | ||
// => { [x: string]: unknown } | ||
``` | ||
## Combining schemas | ||
@@ -692,8 +744,8 @@ | ||
properties: { | ||
name: { $ref: "#/$defs/name" }, | ||
age: { $ref: "#/$defs/age" }, | ||
name: { $ref: "#/definitions/name" }, | ||
age: { $ref: "#/definitions/age" }, | ||
}, | ||
required: ["name", "age"], | ||
additionalProperties: false, | ||
$defs: { | ||
definitions: { | ||
name: { type: "string" }, | ||
@@ -700,0 +752,0 @@ age: { type: "integer" }, |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
121095
1106
1047
+ Addedts-algebra@2.0.0(transitive)
- Removedts-algebra@1.2.2(transitive)
Updatedts-algebra@^2.0.0