@tsed/schema 8.17.3 → 8.18.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (50) hide show
  1. package/functional-api-typing-plan.md +207 -0
  2. package/lib/esm/decorators/common/schema.js +2 -2
  3. package/lib/esm/domain/JsonSchema.js +105 -91
  4. package/lib/esm/domain/types.js +1 -0
  5. package/lib/esm/fn/allOf.js +2 -0
  6. package/lib/esm/fn/any.js +0 -5
  7. package/lib/esm/fn/anyOf.js +2 -1
  8. package/lib/esm/fn/boolean.js +3 -1
  9. package/lib/esm/fn/collection.js +13 -45
  10. package/lib/esm/fn/date.js +6 -0
  11. package/lib/esm/fn/email.js +2 -0
  12. package/lib/esm/fn/enums.js +2 -2
  13. package/lib/esm/fn/from.js +0 -11
  14. package/lib/esm/fn/index.js +10 -1
  15. package/lib/esm/fn/integer.js +2 -2
  16. package/lib/esm/fn/lazyRef.js +0 -3
  17. package/lib/esm/fn/number.js +2 -0
  18. package/lib/esm/fn/object.js +2 -1
  19. package/lib/esm/fn/string.js +2 -0
  20. package/lib/esm/fn/url.js +2 -0
  21. package/lib/esm/index.js +1 -0
  22. package/lib/types/decorators/common/default.d.ts +2 -2
  23. package/lib/types/decorators/common/schema.d.ts +3 -3
  24. package/lib/types/domain/JsonLazyRef.d.ts +1 -1
  25. package/lib/types/domain/JsonMedia.d.ts +1 -1
  26. package/lib/types/domain/JsonParameter.d.ts +1 -1
  27. package/lib/types/domain/JsonParameterStore.d.ts +1 -1
  28. package/lib/types/domain/JsonSchema.d.ts +58 -31
  29. package/lib/types/domain/types.d.ts +7 -0
  30. package/lib/types/fn/allOf.d.ts +5 -2
  31. package/lib/types/fn/any.d.ts +7 -1
  32. package/lib/types/fn/anyOf.d.ts +5 -3
  33. package/lib/types/fn/boolean.d.ts +4 -3
  34. package/lib/types/fn/collection.d.ts +33 -20
  35. package/lib/types/fn/date.d.ts +9 -4
  36. package/lib/types/fn/email.d.ts +3 -2
  37. package/lib/types/fn/enums.d.ts +11 -1
  38. package/lib/types/fn/from.d.ts +3 -1
  39. package/lib/types/fn/index.d.ts +14 -1
  40. package/lib/types/fn/integer.d.ts +3 -4
  41. package/lib/types/fn/lazyRef.d.ts +4 -2
  42. package/lib/types/fn/number.d.ts +3 -1
  43. package/lib/types/fn/object.d.ts +3 -4
  44. package/lib/types/fn/oneOf.d.ts +3 -2
  45. package/lib/types/fn/string.d.ts +3 -2
  46. package/lib/types/fn/uri.d.ts +1 -2
  47. package/lib/types/fn/url.d.ts +3 -2
  48. package/lib/types/index.d.ts +1 -0
  49. package/lib/types/registries/enumRegistries.d.ts +1 -1
  50. package/package.json +7 -7
@@ -2,7 +2,9 @@ import { from } from "./from.js";
2
2
  /**
3
3
  * Declare a new boolean model.
4
4
  *
5
- * * See @@JsonSchema@@ to discover available methods.
5
+ * See @@JsonSchema@@ to discover available methods.
6
+ *
7
+ * @schemaFunctional
6
8
  */
7
9
  export function boolean() {
8
10
  return from(Boolean);
@@ -1,49 +1,17 @@
1
1
  import { from } from "./from.js";
2
- /**
3
- * Declare a new object model with `additionalProperties: true`.
4
- *
5
- * ```json
6
- * {
7
- * "type": "array"
8
- * }
9
- * ```
10
- *
11
- * * See @@JsonSchema@@ to discover available methods.
12
- *
13
- * @schemaFunctional
14
- */
15
- export function array() {
16
- return from(Array);
2
+ export function array(item) {
3
+ const schema = from(Array);
4
+ return (item ? schema.items(item) : schema);
17
5
  }
18
- /**
19
- * Declare a new object model with `additionalProperties: true`.
20
- *
21
- * ```json
22
- * {
23
- * "type": "object",
24
- * "additionalProperties": true
25
- * }
26
- * ```
27
- *
28
- * * See @@JsonSchema@@ to discover available methods.
29
- *
30
- * @schemaFunctional
31
- */
32
- export function map() {
33
- return from(Map).unknown(true);
6
+ export function map(value) {
7
+ const schema = from(Map).unknown(true);
8
+ return (value ? schema.additionalProperties(value) : schema);
34
9
  }
35
- /**
36
- * Declare a new array model with `uniqueItems: true`.
37
- *
38
- * ```json
39
- * {
40
- * "type": "array",
41
- * "uniqueItems": true
42
- * }
43
- * ```
44
- *
45
- * * See @@JsonSchema@@ to discover available methods.
46
- */
47
- export function set() {
48
- return from(Array).uniqueItems(true);
10
+ export function set(item) {
11
+ const schema = from(Array).uniqueItems(true);
12
+ return (item ? schema.items(item) : schema);
13
+ }
14
+ export function record(value) {
15
+ const schema = from(Object).unknown(true);
16
+ return (value ? schema.additionalProperties(value) : schema);
49
17
  }
@@ -4,6 +4,8 @@ import { from } from "./from.js";
4
4
  * Declare a new string model with `format: date`.
5
5
  *
6
6
  * See @@JsonSchema@@ to discover available methods.
7
+ *
8
+ * @schemaFunctional
7
9
  */
8
10
  export function date() {
9
11
  return from(Date).format(JsonFormatTypes.DATE);
@@ -12,6 +14,8 @@ export function date() {
12
14
  * Declare a new string model with `format: datetime`.
13
15
  *
14
16
  * See @@JsonSchema@@ to discover available methods.
17
+ *
18
+ * @schemaFunctional
15
19
  */
16
20
  export function datetime() {
17
21
  return from(Date).format(JsonFormatTypes.DATE_TIME);
@@ -20,6 +24,8 @@ export function datetime() {
20
24
  * Declare a new string model with `format: time`.
21
25
  *
22
26
  * See @@JsonSchema@@ to discover available methods.
27
+ *
28
+ * @schemaFunctional
23
29
  */
24
30
  export function time() {
25
31
  return from(Date).format(JsonFormatTypes.TIME);
@@ -11,6 +11,8 @@ import { string } from "./string.js";
11
11
  * const schema = email();
12
12
  * // Results in: { type: "string", format: "email" }
13
13
  * ```
14
+ *
15
+ * @schemaFunctional
14
16
  */
15
17
  export function email() {
16
18
  return string().format(JsonFormatTypes.EMAIL);
@@ -1,7 +1,7 @@
1
1
  import { enumsRegistry } from "../registries/enumRegistries.js";
2
- import { string } from "./string.js";
2
+ import { from } from "./from.js";
3
3
  export function enums(e) {
4
- const schema = string().enum(e);
4
+ const schema = from().enum(e);
5
5
  enumsRegistry.set(e, schema);
6
6
  return schema;
7
7
  }
@@ -1,17 +1,6 @@
1
1
  import { isClass } from "@tsed/core";
2
2
  import { JsonSchema } from "../domain/JsonSchema.js";
3
3
  import { getJsonEntityStore } from "../utils/getJsonEntityStore.js";
4
- /**
5
- * Create a new model from the given type.
6
- *
7
- * ```typescript
8
- * from(String)
9
- * ```
10
- *
11
- * * See @@JsonSchema@@ to discover available methods.
12
- *
13
- * @schemaFunctional
14
- */
15
4
  export function from(type = Object) {
16
5
  if (isClass(type)) {
17
6
  const { schema } = getJsonEntityStore(type);
@@ -1,10 +1,12 @@
1
1
  import { allOf } from "./allOf.js";
2
+ import { any } from "./any.js";
2
3
  import { anyOf } from "./anyOf.js";
3
4
  import { boolean } from "./boolean.js";
4
- import { array, map, set } from "./collection.js";
5
+ import { array, map, record, set } from "./collection.js";
5
6
  import { date, datetime, time } from "./date.js";
6
7
  import { email } from "./email.js";
7
8
  import { enums } from "./enums.js";
9
+ import { from } from "./from.js";
8
10
  import { integer } from "./integer.js";
9
11
  import { lazyRef } from "./lazyRef.js";
10
12
  import { number } from "./number.js";
@@ -13,13 +15,20 @@ import { oneOf } from "./oneOf.js";
13
15
  import { string } from "./string.js";
14
16
  import { uri } from "./uri.js";
15
17
  import { url } from "./url.js";
18
+ /**
19
+ * A namespace of all schema builder functions.
20
+ * @schemaFunctional
21
+ */
16
22
  export const s = {
23
+ from,
17
24
  allOf,
18
25
  anyOf,
26
+ any,
19
27
  boolean,
20
28
  array,
21
29
  map,
22
30
  set,
31
+ record,
23
32
  date,
24
33
  datetime,
25
34
  time,
@@ -4,8 +4,8 @@ import { from } from "./from.js";
4
4
  *
5
5
  * See @@JsonSchema@@ to discover available methods.
6
6
  *
7
- * @returns {JsonSchema} A schema configured for integer values
8
- * @function
7
+ * @returns A schema configured for integer values
8
+ * @schemaFunctional
9
9
  */
10
10
  export function integer() {
11
11
  return from(Number).integer();
@@ -1,8 +1,5 @@
1
1
  import { JsonEntityStore } from "../domain/JsonEntityStore.js";
2
2
  import { JsonLazyRef } from "../domain/JsonLazyRef.js";
3
- /**
4
- * Declare a sub schema which will be resolved later. Use this function when you have a circular reference between two schemes.
5
- */
6
3
  export function lazyRef(cb) {
7
4
  try {
8
5
  // solve issue with a self referenced model
@@ -3,6 +3,8 @@ import { from } from "./from.js";
3
3
  * Declare a new number model.
4
4
  *
5
5
  * * See @@JsonSchema@@ to discover available methods.
6
+ *
7
+ * @schemaFunctional
6
8
  */
7
9
  export function number() {
8
10
  return from(Number);
@@ -14,7 +14,8 @@ import { from } from "./from.js";
14
14
  * ```
15
15
  *
16
16
  * @param properties - An object containing property definitions where each value is a JsonSchema
17
- * @returns {JsonSchema} A new object model with the specified properties
17
+ * @returns A new object model with the specified properties
18
+ * @schemaFunctional
18
19
  */
19
20
  export function object(properties = {}) {
20
21
  return from(Object).properties(properties);
@@ -3,6 +3,8 @@ import { from } from "./from.js";
3
3
  * Declare a new string model.
4
4
  *
5
5
  * See @@JsonSchema@@ to discover available methods.
6
+ *
7
+ * @schemaFunctional
6
8
  */
7
9
  export function string() {
8
10
  return from(String);
package/lib/esm/fn/url.js CHANGED
@@ -4,6 +4,8 @@ import { string } from "./string.js";
4
4
  * Declare a new string model with `format: url`.
5
5
  *
6
6
  * See @@JsonSchema@@ to discover available methods.
7
+ *
8
+ * @schemaFunctional
7
9
  */
8
10
  export function url() {
9
11
  return string().format(JsonFormatTypes.URL);
package/lib/esm/index.js CHANGED
@@ -134,6 +134,7 @@ export * from "./domain/JsonRequestBody.js";
134
134
  export * from "./domain/JsonResponse.js";
135
135
  export * from "./domain/JsonSchema.js";
136
136
  export * from "./domain/SpecTypes.js";
137
+ export * from "./domain/types.js";
137
138
  export * from "./fn/allOf.js";
138
139
  export * from "./fn/any.js";
139
140
  export * from "./fn/anyOf.js";
@@ -1,4 +1,4 @@
1
- import type { JSONSchema6Type } from "json-schema";
1
+ import type { JSONSchema7Type } from "json-schema";
2
2
  /**
3
3
  * There are no restrictions placed on the value of this keyword.
4
4
  *
@@ -39,4 +39,4 @@ import type { JSONSchema6Type } from "json-schema";
39
39
  * @schema
40
40
  * @input
41
41
  */
42
- export declare function Default(defaultValue: JSONSchema6Type | undefined | (() => JSONSchema6Type)): (...args: any[]) => any;
42
+ export declare function Default(defaultValue: JSONSchema7Type | undefined | (() => JSONSchema7Type)): (...args: any[]) => any;
@@ -5,7 +5,7 @@ import { SpecTypes } from "../../domain/SpecTypes.js";
5
5
  *
6
6
  * ## Example
7
7
  *
8
- * ```typescript
8
+ * ```ts
9
9
  * @Schema({title: "test"})
10
10
  * class Model {
11
11
  * @Schema({formatMinimum: "1987-10-24"})
@@ -38,9 +38,9 @@ import { SpecTypes } from "../../domain/SpecTypes.js";
38
38
  * @classDecorator
39
39
  * @input
40
40
  */
41
- export declare function Schema(partialSchema: Partial<JsonSchemaObject> | JsonSchema): (...args: any[]) => any;
41
+ export declare function Schema<T>(partialSchema: Partial<JsonSchemaObject> | JsonSchema<T>): (...args: any[]) => any;
42
42
  /**
43
- * Apply specific schema depending on the spec version
43
+ * Apply a specific schema depending on the spec version
44
44
  * @param specType
45
45
  * @param schema
46
46
  * @decorator
@@ -4,6 +4,6 @@ export declare class JsonLazyRef {
4
4
  readonly isLazyRef = true;
5
5
  constructor(getType: () => Type<any>);
6
6
  get target(): Type<any>;
7
- get schema(): import("./JsonSchema.js").JsonSchema;
7
+ get schema(): import("./JsonSchema.js").JsonSchema<import("json-schema").JSONSchema7Type>;
8
8
  get name(): string;
9
9
  }
@@ -4,7 +4,7 @@ import { JsonSchema } from "./JsonSchema.js";
4
4
  export declare class JsonMedia extends JsonMap<any> {
5
5
  $kind: string;
6
6
  constructor(obj?: Partial<OS3MediaType<JsonSchema>>);
7
- schema(schema?: JsonSchema): JsonSchema;
7
+ schema(schema?: JsonSchema): JsonSchema<import("json-schema").JSONSchema7Type>;
8
8
  examples(examples?: any): this;
9
9
  type(type: any): this;
10
10
  }
@@ -14,6 +14,6 @@ export declare class JsonParameter extends JsonMap<OS3Parameter<JsonSchema>> {
14
14
  required(required: boolean): this;
15
15
  schema(): JsonSchema;
16
16
  schema(schema: JsonSchema): this;
17
- itemSchema(schema?: JsonSchema): JsonSchema;
17
+ itemSchema(schema?: JsonSchema): JsonSchema<import("json-schema").JSONSchema7Type>;
18
18
  toJSON(options?: JsonSchemaOptions): any;
19
19
  }
@@ -31,7 +31,7 @@ export declare class JsonParameterStore extends JsonEntityStore {
31
31
  get required(): boolean;
32
32
  set required(value: boolean);
33
33
  get allowedRequiredValues(): any[];
34
- get schema(): import("./JsonSchema.js").JsonSchema;
34
+ get schema(): import("./JsonSchema.js").JsonSchema<import("json-schema").JSONSchema7Type>;
35
35
  static getParams<T extends JsonParameterStore = JsonParameterStore>(target: Type<any>, propertyKey: string | symbol): T[];
36
36
  static get(target: Type<any>, propertyKey: string | symbol, index: number): JsonParameterStore;
37
37
  /**
@@ -9,16 +9,17 @@ import { AliasMap, AliasType } from "./JsonAliasMap.js";
9
9
  import { Discriminator } from "./JsonDiscriminator.js";
10
10
  import { JsonFormatTypes } from "./JsonFormatTypes.js";
11
11
  import { JsonLazyRef } from "./JsonLazyRef.js";
12
+ import type { Infer, PropsToShape, UnionToIntersection } from "./types.js";
12
13
  export interface JsonSchemaObject extends Omit<JSONSchema7, "type" | "additionalProperties" | "items" | "pattern"> {
13
14
  type?: JSONSchema7["type"] | Type | null | (String | null | Date | Number | Object | Boolean)[];
14
15
  additionalProperties?: JsonSchemaObject | boolean | Type | JsonSchema;
15
16
  items?: JsonSchemaObject | boolean | Type | JsonSchema | (JsonSchemaObject | Type | JsonSchema)[];
16
17
  pattern?: string | RegExp;
17
18
  }
18
- export type AnyJsonSchema = JsonSchemaObject | JSONSchema7 | JsonSchema | JsonLazyRef | {
19
+ export type AnyJsonSchema<T = any> = JsonSchemaObject | JSONSchema7 | JsonSchema<T> | JsonLazyRef | {
19
20
  label?: string;
20
21
  } | Type;
21
- export declare class JsonSchema extends Map<string, any> {
22
+ export declare class JsonSchema<T = JSONSchema7Type> extends Map<string, any> {
22
23
  #private;
23
24
  readonly $kind: string;
24
25
  readonly $isJsonDocument = true;
@@ -28,7 +29,7 @@ export declare class JsonSchema extends Map<string, any> {
28
29
  $ignore: boolean | IgnoreCallback;
29
30
  isDiscriminatorKey: boolean;
30
31
  isDiscriminator: boolean;
31
- constructor(obj?: JsonSchema | Partial<JsonSchemaObject>);
32
+ constructor(obj?: Partial<JSONSchema7> | JsonSchema | Record<string, unknown>);
32
33
  get alias(): AliasMap;
33
34
  get isClass(): boolean;
34
35
  /**
@@ -60,7 +61,7 @@ export declare class JsonSchema extends Map<string, any> {
60
61
  get isReadOnly(): any;
61
62
  get isWriteOnly(): any;
62
63
  get hasDiscriminator(): boolean;
63
- static from(item?: Partial<JsonSchemaObject> | Type<any> | JsonSchema): JsonSchema;
64
+ static from(item: Partial<JsonSchemaObject> | Type<any> | JsonSchema | undefined): JsonSchema<JSONSchema7Type>;
64
65
  /**
65
66
  * Check if the schema as key, #key or x-key
66
67
  * @param key
@@ -88,8 +89,10 @@ export declare class JsonSchema extends Map<string, any> {
88
89
  * @param generics
89
90
  */
90
91
  genericOf(...generics: GenericValue[][]): this;
91
- nullable(value: boolean): this;
92
- itemSchema(obj?: AnyJsonSchema): JsonSchema;
92
+ nullable(): JsonSchema<T | null>;
93
+ nullable(value: true): JsonSchema<T | null>;
94
+ nullable(value: false): JsonSchema<Exclude<T, null>>;
95
+ itemSchema(obj?: AnyJsonSchema): JsonSchema<JSONSchema7Type>;
93
96
  getAliasOf(property: AliasType): AliasType | undefined;
94
97
  addAlias(property: AliasType, alias: AliasType): this;
95
98
  removeAlias(property: AliasType): this;
@@ -108,14 +111,14 @@ export declare class JsonSchema extends Map<string, any> {
108
111
  * It is RECOMMENDED that a default value be valid against the associated schema.
109
112
  * @see https://tools.ietf.org/html/draft-wright-json-schema-validation-01#section-7.3
110
113
  */
111
- default(value: JSONSchema7Type | undefined | (() => JSONSchema7Type)): this;
114
+ default(value: T | undefined | (() => T)): JsonSchema<T>;
112
115
  /**
113
- * More readible form of a one-element "enum"
116
+ * More readable form of a one-element "enum"
114
117
  * @see https://tools.ietf.org/html/draft-wright-json-schema-validation-01#section-6.24
115
118
  */
116
- const(value: JSONSchema7Type): this;
119
+ const<V extends T>(value: V): JsonSchema<V>;
117
120
  /**
118
- * This attribute is a string that provides a full description of the of purpose the instance property.
121
+ * This attribute is a string that provides a full description of the purpose of the instance property.
119
122
  *
120
123
  * @see https://tools.ietf.org/html/draft-wright-json-schema-validation-01#section-7.2
121
124
  */
@@ -124,7 +127,7 @@ export declare class JsonSchema extends Map<string, any> {
124
127
  discriminatorKey(propertyName: string): this;
125
128
  discriminatorValue(...values: string[]): this;
126
129
  /**
127
- * This keyword determines how child instances validate for arrays, and does not directly validate the immediate instance itself.
130
+ * This keyword determines how child instances validate for arrays and does not directly validate the immediate instance itself.
128
131
  * If "items" is an array of schemas, validation succeeds if every instance element
129
132
  * at a position greater than the size of "items" validates against "additionalItems".
130
133
  * Otherwise, "additionalItems" MUST be ignored, as the "items" schema
@@ -149,11 +152,15 @@ export declare class JsonSchema extends Map<string, any> {
149
152
  */
150
153
  example(...examples: JSONSchema7Type[]): this;
151
154
  /**
152
- * This keyword determines how child instances validate for arrays, and does not directly validate the immediate instance itself.
155
+ * This keyword determines how child instances validate for arrays and does not directly validate the immediate instance itself.
153
156
  * Omitting this keyword has the same behavior as an empty schema.
154
157
  * @see https://tools.ietf.org/html/draft-wright-json-schema-validation-01#section-6.9
155
158
  */
156
- items(items: AnyJsonSchema | AnyJsonSchema[]): this;
159
+ /**
160
+ * Set the items' schema for array/set collections
161
+ * @param items The schema for array/set items
162
+ */
163
+ items<I = JSONSchema7Type>(items: JsonSchema<I> | AnyJsonSchema | AnyJsonSchema[]): JsonSchema<T extends Array<any> ? I[] : T extends Set<any> ? Set<I> : I>;
157
164
  $comment(comment: string): this;
158
165
  /**
159
166
  * Must be a non-negative integer.
@@ -169,7 +176,7 @@ export declare class JsonSchema extends Map<string, any> {
169
176
  */
170
177
  minItems(minItems: number): this;
171
178
  /**
172
- * If this keyword has boolean value false, the instance validates successfully.
179
+ * If this keyword has a boolean value false, the instance validates successfully.
173
180
  * If it has boolean value true, the instance validates successfully if all of its elements are unique.
174
181
  * Omitting this keyword has the same behavior as a value of false.
175
182
  * @see https://tools.ietf.org/html/draft-wright-json-schema-validation-01#section-6.13
@@ -190,6 +197,7 @@ export declare class JsonSchema extends Map<string, any> {
190
197
  */
191
198
  minProperties(minProperties: number): this;
192
199
  allow(...allow: any[]): this;
200
+ optional(): JsonSchema<T | undefined>;
193
201
  /**
194
202
  * Elements of this array must be unique.
195
203
  * An object instance is valid against this keyword if every item in the array is the name of a property in the instance.
@@ -197,19 +205,21 @@ export declare class JsonSchema extends Map<string, any> {
197
205
  *
198
206
  * @see https://tools.ietf.org/html/draft-wright-json-schema-validation-01#section-6.17
199
207
  */
200
- required(required?: boolean | string[]): JsonSchema;
208
+ required(): JsonSchema<Exclude<T, undefined>>;
209
+ required(required: true): JsonSchema<Exclude<T, undefined>>;
210
+ required(required: false): JsonSchema<T | undefined>;
201
211
  addRequired(property: string): this;
202
212
  removeRequired(property: string): this;
203
213
  isRequired(property: string): boolean;
204
214
  getRequiredFields(): string[];
205
215
  /**
206
- * This keyword determines how child instances validate for objects, and does not directly validate the immediate instance itself.
216
+ * This keyword determines how child instances validate for objects and does not directly validate the immediate instance itself.
207
217
  * Validation succeeds if, for each name that appears in both the instance and as a name within this keyword's value,
208
218
  * the child instance for that name successfully validates against the corresponding schema.
209
219
  * Omitting this keyword has the same behavior as an empty object.
210
220
  * @see https://tools.ietf.org/html/draft-wright-json-schema-validation-01#section-6.18
211
221
  */
212
- properties(properties: AnyJsonSchema | Record<string, AnyJsonSchema>): this;
222
+ properties<P extends Record<string, JsonSchema<any>> = Record<string, JsonSchema<any>>>(properties: P): JsonSchema<PropsToShape<P>>;
213
223
  addProperty(key: string, schema: AnyJsonSchema): this;
214
224
  /**
215
225
  * This attribute is an object that defines the schema for a set of property names of an object instance.
@@ -219,25 +229,25 @@ export declare class JsonSchema extends Map<string, any> {
219
229
  * Omitting this keyword has the same behavior as an empty object.
220
230
  * @see https://tools.ietf.org/html/draft-wright-json-schema-validation-01#section-6.19
221
231
  */
222
- patternProperties(patternProperties: Record<string, AnyJsonSchema>): this;
232
+ patternProperties<P extends Record<string, JsonSchema<any>> = Record<string, JsonSchema<any>>>(patternProperties: P): JsonSchema<PropsToShape<P>>;
223
233
  /**
224
234
  * This attribute defines a schema for all properties that are not explicitly defined in an object type definition.
225
235
  * If specified, the value MUST be a schema or a boolean.
226
236
  * If false is provided, no additional properties are allowed beyond the properties defined in the schema.
227
- * The default value is an empty schema which allows any value for additional properties.
237
+ * The default value is an empty schema that allows any value for additional properties.
228
238
  * @see https://tools.ietf.org/html/draft-wright-json-schema-validation-01#section-6.20
229
239
  */
230
- additionalProperties(additionalProperties: boolean | AnyJsonSchema): this;
240
+ additionalProperties<V>(additionalProperties: boolean | AnyJsonSchema | JsonSchema<V>): JsonSchema<Map<string, V>>;
231
241
  /**
232
242
  * This attribute defines a schema for all properties that are not explicitly defined in an object type definition.
233
243
  * If specified, the value MUST be a schema or a boolean.
234
244
  * If false is provided, no additional properties are allowed beyond the properties defined in the schema.
235
- * The default value is an empty schema which allows any value for additional properties.
245
+ * The default value is an empty schema that allows any value for additional properties.
236
246
  * @see https://tools.ietf.org/html/draft-wright-json-schema-validation-01#section-6.20
237
247
  * @alias additionalProperties
238
248
  * @param unknown
239
249
  */
240
- unknown(unknown?: boolean): this;
250
+ unknown(unknown?: boolean): JsonSchema<T & Record<string, unknown>>;
241
251
  /**
242
252
  * This keyword specifies rules that are evaluated if the instance is an object and contains a certain property.
243
253
  * Each property specifies a dependency.
@@ -264,8 +274,9 @@ export declare class JsonSchema extends Map<string, any> {
264
274
  *
265
275
  * @see https://tools.ietf.org/html/draft-wright-json-schema-validation-01#section-6.23
266
276
  */
267
- enum(...enumValues: any[]): this;
268
- enum(enumSchema: JsonSchema): this;
277
+ enum<E extends Record<string, string | number>>(e: E): JsonSchema<E[keyof E]>;
278
+ enum<E extends readonly T[]>(...e: E): JsonSchema<E[number]>;
279
+ enum<E extends readonly T[]>(e: E): JsonSchema<E[number]>;
269
280
  /**
270
281
  * @see https://tools.ietf.org/html/draft-wright-json-schema-validation-01#section-7.1
271
282
  */
@@ -273,12 +284,12 @@ export declare class JsonSchema extends Map<string, any> {
273
284
  /**
274
285
  * @see https://tools.ietf.org/html/draft-wright-json-schema-validation-01#section-6.26
275
286
  */
276
- allOf(allOf: (AnyJsonSchema | null)[]): this;
287
+ allOf<S extends Array<AnyJsonSchema | null>>(allOf: S): JsonSchema<UnionToIntersection<Infer<S[number]>>>;
277
288
  /**
278
289
  * @see https://tools.ietf.org/html/draft-wright-json-schema-validation-01#section-6.27
279
290
  */
280
- anyOf(anyOf: (AnyJsonSchema | null)[]): this;
281
- oneOf(oneOf: (AnyJsonSchema | null)[]): this;
291
+ anyOf<S extends Array<AnyJsonSchema | null>>(anyOf: S): JsonSchema<Infer<S[number]>>;
292
+ oneOf<S extends Array<AnyJsonSchema | null>>(oneOf: S): JsonSchema<Infer<S[number]>>;
282
293
  /**
283
294
  * @see https://tools.ietf.org/html/draft-wright-json-schema-validation-01#section-6.29
284
295
  */
@@ -289,6 +300,12 @@ export declare class JsonSchema extends Map<string, any> {
289
300
  * @see https://tools.ietf.org/html/draft-wright-json-schema-validation-01#section-6.1
290
301
  */
291
302
  multipleOf(multipleOf: number): this;
303
+ /**
304
+ * Representing an inclusive upper limit for a numeric instance.
305
+ * This keyword validates only if the instance is less than or exactly equal to "maximum".
306
+ * @see https://tools.ietf.org/html/draft-wright-json-schema-validation-01#section-6.2
307
+ */
308
+ max(maximum: number): this;
292
309
  /**
293
310
  * Representing an inclusive upper limit for a numeric instance.
294
311
  * This keyword validates only if the instance is less than or exactly equal to "maximum".
@@ -301,6 +318,12 @@ export declare class JsonSchema extends Map<string, any> {
301
318
  * @see https://tools.ietf.org/html/draft-wright-json-schema-validation-01#section-6.3
302
319
  */
303
320
  exclusiveMaximum(exclusiveMaximum: number): this;
321
+ /**
322
+ * Representing an inclusive lower limit for a numeric instance.
323
+ * This keyword validates only if the instance is greater than or exactly equal to "minimum".
324
+ * @see https://tools.ietf.org/html/draft-wright-json-schema-validation-01#section-6.4
325
+ */
326
+ min(minimum: number): this;
304
327
  /**
305
328
  * Representing an inclusive lower limit for a numeric instance.
306
329
  * This keyword validates only if the instance is greater than or exactly equal to "minimum".
@@ -354,10 +377,10 @@ export declare class JsonSchema extends Map<string, any> {
354
377
  vendorKey(key: VendorKeys, value: any): this;
355
378
  toObject(options?: JsonSchemaOptions): any;
356
379
  toJSON(options?: JsonSchemaOptions): any;
357
- assign(obj?: AnyJsonSchema): this;
380
+ assign(obj: Partial<JSONSchema7> | JsonSchema | Record<string, unknown>): this;
358
381
  set(key: string, value: any): this;
359
382
  /**
360
- * Return the Json type as string
383
+ * Return the JSON type as string
361
384
  */
362
385
  getJsonType(): string | string[];
363
386
  getTarget(): Type<any>;
@@ -369,7 +392,7 @@ export declare class JsonSchema extends Map<string, any> {
369
392
  * Get the symbolic name of the entity
370
393
  */
371
394
  getName(): any;
372
- clone(): JsonSchema;
395
+ clone(): JsonSchema<JSONSchema7Type>;
373
396
  getGenericLabels(): string[] | undefined;
374
397
  getGenericOf(): GenericsMap | undefined;
375
398
  /**
@@ -377,10 +400,14 @@ export declare class JsonSchema extends Map<string, any> {
377
400
  * Used to get the original schema definition when this schema is a local reference.
378
401
  * @returns The reference schema or undefined if not applicable
379
402
  */
380
- getRefSchema(): JsonSchema | undefined;
403
+ getRefSchema(): JsonSchema<JSONSchema7Type> | undefined;
381
404
  propertyKey(s: string): this;
382
405
  getPropertyKey(): string | symbol | undefined;
383
406
  target(target: any): void;
384
407
  isRequiredValue(property: string, value: any): boolean;
385
408
  protected setManyOf(keyword: "oneOf" | "anyOf" | "allOf", value: (AnyJsonSchema | null)[]): this | undefined;
409
+ protected mapToJsonSchema(item: any[]): JsonSchema[];
410
+ protected mapToJsonSchema(item: any): JsonSchema;
411
+ protected mapGenerics(jsonSchema: JsonSchema, generics: GenericValue[][]): GenericsMap;
412
+ protected mapProperties(properties: Record<string, any>): any;
386
413
  }
@@ -0,0 +1,7 @@
1
+ import type { JsonSchema } from "./JsonSchema.js";
2
+ export type Infer<S> = S extends JsonSchema<infer T> ? T : never;
3
+ export type PropsToShape<P extends Record<string, JsonSchema<any>>> = {
4
+ [K in keyof P]: Infer<P[K]>;
5
+ };
6
+ export type CtorToType<C> = C extends StringConstructor ? string : C extends NumberConstructor ? number : C extends BooleanConstructor ? boolean : C extends DateConstructor ? Date : C extends ArrayConstructor ? any[] : C extends MapConstructor ? Record<string, any> : C extends SetConstructor ? Set<any> : C extends ObjectConstructor ? Record<string, any> : C extends abstract new (...args: any) => any ? InstanceType<C> : any;
7
+ export type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends (k: infer I) => void ? I : never;
@@ -1,9 +1,12 @@
1
- import type { JsonSchema } from "../domain/JsonSchema.js";
1
+ import type { AnyJsonSchema, JsonSchema } from "../domain/JsonSchema.js";
2
+ import type { Infer, UnionToIntersection } from "../domain/types.js";
2
3
  /**
3
4
  * See https://tools.ietf.org/html/draft-wright-json-schema-validation-01#section-6.26
4
5
  *
5
6
  * * See @@JsonSchema@@ to discover available methods.
6
7
  *
7
8
  * @see https://tools.ietf.org/html/draft-wright-json-schema-validation-01#section-6.26
9
+ *
10
+ * @schemaFunctional
8
11
  */
9
- export declare function allOf(...allOf: Parameters<JsonSchema["allOf"]>[0]): JsonSchema;
12
+ export declare function allOf<S extends Array<AnyJsonSchema | null>>(...allOf: S): JsonSchema<UnionToIntersection<Infer<S[number]>>>;
@@ -1,7 +1,13 @@
1
1
  import type { JsonSchema } from "../domain/JsonSchema.js";
2
+ import type { Infer } from "../domain/types.js";
2
3
  /**
3
4
  * Declare a model with any type (By default: `integer`, `number`, `string`, `boolean`, `array`, `object`, `null`)
4
5
  *
5
6
  * See @@JsonSchema@@ to discover available methods.
7
+ *
8
+ * @schemaFunctional
6
9
  */
7
- export declare function any(...types: Parameters<JsonSchema["oneOf"]>[0]): JsonSchema;
10
+ export declare function any(): JsonSchema<any>;
11
+ export declare function any<S extends Array<JsonSchema<any>>>(...types: S): JsonSchema<{
12
+ [K in keyof S]: Infer<S[K]>;
13
+ }>;
@@ -1,9 +1,11 @@
1
- import type { JsonSchema } from "../domain/JsonSchema.js";
1
+ import type { AnyJsonSchema, JsonSchema } from "../domain/JsonSchema.js";
2
+ import type { Infer } from "../domain/types.js";
2
3
  /**
3
4
  * See https://tools.ietf.org/html/draft-wright-json-schema-validation-01#section-6.27
4
5
  *
5
- * * See @@JsonSchema@@ to discover available methods.
6
+ * See @@JsonSchema@@ to discover available methods.
6
7
  *
7
8
  * @see https://tools.ietf.org/html/draft-wright-json-schema-validation-01#section-6.27
9
+ * @schemaFunctional
8
10
  */
9
- export declare function anyOf(...anyOf: Parameters<JsonSchema["allOf"]>[0]): JsonSchema;
11
+ export declare function anyOf<S extends Array<AnyJsonSchema | null>>(...anyOf: S): JsonSchema<Infer<S[number]>>;