@powerlines/schema 0.11.27 → 0.11.37

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 (82) hide show
  1. package/dist/bundle.cjs +34 -20
  2. package/dist/bundle.d.cts +3 -3
  3. package/dist/bundle.d.cts.map +1 -1
  4. package/dist/bundle.d.mts +3 -3
  5. package/dist/bundle.d.mts.map +1 -1
  6. package/dist/bundle.mjs +35 -21
  7. package/dist/bundle.mjs.map +1 -1
  8. package/dist/codegen.cjs +40 -36
  9. package/dist/codegen.d.cts +10 -22
  10. package/dist/codegen.d.cts.map +1 -1
  11. package/dist/codegen.d.mts +10 -22
  12. package/dist/codegen.d.mts.map +1 -1
  13. package/dist/codegen.mjs +39 -36
  14. package/dist/codegen.mjs.map +1 -1
  15. package/dist/constants.cjs +34 -11
  16. package/dist/constants.d.cts +9 -11
  17. package/dist/constants.d.cts.map +1 -1
  18. package/dist/constants.d.mts +9 -11
  19. package/dist/constants.d.mts.map +1 -1
  20. package/dist/constants.mjs +32 -11
  21. package/dist/constants.mjs.map +1 -1
  22. package/dist/extract.cjs +122 -92
  23. package/dist/extract.d.cts +34 -69
  24. package/dist/extract.d.cts.map +1 -1
  25. package/dist/extract.d.mts +34 -69
  26. package/dist/extract.d.mts.map +1 -1
  27. package/dist/extract.mjs +123 -93
  28. package/dist/extract.mjs.map +1 -1
  29. package/dist/helpers.cjs +45 -52
  30. package/dist/helpers.d.cts +28 -24
  31. package/dist/helpers.d.cts.map +1 -1
  32. package/dist/helpers.d.mts +28 -24
  33. package/dist/helpers.d.mts.map +1 -1
  34. package/dist/helpers.mjs +46 -52
  35. package/dist/helpers.mjs.map +1 -1
  36. package/dist/index.cjs +14 -7
  37. package/dist/index.d.cts +6 -6
  38. package/dist/index.d.mts +6 -6
  39. package/dist/index.mjs +5 -5
  40. package/dist/metadata.cjs +80 -0
  41. package/dist/metadata.d.cts +52 -0
  42. package/dist/metadata.d.cts.map +1 -0
  43. package/dist/metadata.d.mts +52 -0
  44. package/dist/metadata.d.mts.map +1 -0
  45. package/dist/metadata.mjs +76 -0
  46. package/dist/metadata.mjs.map +1 -0
  47. package/dist/persistence.cjs +1 -2
  48. package/dist/persistence.d.cts +5 -5
  49. package/dist/persistence.d.cts.map +1 -1
  50. package/dist/persistence.d.mts +5 -5
  51. package/dist/persistence.d.mts.map +1 -1
  52. package/dist/persistence.mjs +1 -1
  53. package/dist/persistence.mjs.map +1 -1
  54. package/dist/reflection.cjs +289 -303
  55. package/dist/reflection.d.cts +3 -16
  56. package/dist/reflection.d.cts.map +1 -1
  57. package/dist/reflection.d.mts +3 -16
  58. package/dist/reflection.d.mts.map +1 -1
  59. package/dist/reflection.mjs +290 -304
  60. package/dist/reflection.mjs.map +1 -1
  61. package/dist/resolve.cjs +7 -7
  62. package/dist/resolve.mjs +7 -7
  63. package/dist/resolve.mjs.map +1 -1
  64. package/dist/type-checks.cjs +122 -40
  65. package/dist/type-checks.d.cts +41 -33
  66. package/dist/type-checks.d.cts.map +1 -1
  67. package/dist/type-checks.d.mts +41 -33
  68. package/dist/type-checks.d.mts.map +1 -1
  69. package/dist/type-checks.mjs +120 -37
  70. package/dist/type-checks.mjs.map +1 -1
  71. package/dist/types.d.cts +225 -113
  72. package/dist/types.d.cts.map +1 -1
  73. package/dist/types.d.mts +225 -113
  74. package/dist/types.d.mts.map +1 -1
  75. package/package.json +11 -7
  76. package/dist/jtd.cjs +0 -385
  77. package/dist/jtd.d.cts +0 -15
  78. package/dist/jtd.d.cts.map +0 -1
  79. package/dist/jtd.d.mts +0 -15
  80. package/dist/jtd.d.mts.map +0 -1
  81. package/dist/jtd.mjs +0 -384
  82. package/dist/jtd.mjs.map +0 -1
@@ -1,44 +1,48 @@
1
- import { JTDSchemaObjectType, JTDSchemaType, ObjectSchema, SchemaMetadata } from "./types.cjs";
1
+ import { JsonSchema, JsonSchemaProperty, Schema } from "./types.cjs";
2
2
 
3
3
  //#region src/helpers.d.ts
4
4
  /**
5
- * A helper function to extract the properties from a JTD object schema. This function takes an {@link ObjectSchema} as input and returns a record of its properties, where each key is the property name and the value is the corresponding JTD schema type along with an `optional` flag indicating whether the property is optional (i.e., defined in `optionalProperties`) or required (i.e., defined in `properties`). The function checks both `properties` and `optionalProperties` of the JTD schema to construct the resulting record.
5
+ * Extracts object properties from a JSON Schema object form.
6
6
  *
7
- * @param obj - The {@link ObjectSchema} from which to extract the properties.
8
- * @returns A record of the properties defined in the JTD object schema, where each key is the property name and the value is an object containing the JTD schema type and an `optional` flag.
7
+ * @remarks
8
+ * This function returns an empty object if the schema is not an object form or if it has no properties.
9
+ *
10
+ * @param obj - The JSON Schema object form or a Schema wrapper to extract properties from.
11
+ * @returns An object mapping property names to their corresponding JSON Schema fragments, including metadata.
9
12
  */
10
- declare function getProperties<TMetadata extends SchemaMetadata>(obj: ObjectSchema<TMetadata> | JTDSchemaObjectType<TMetadata>): Record<string, JTDSchemaType<TMetadata> & {
11
- name: string;
12
- optional: boolean;
13
- }>;
13
+ declare function getProperties<T extends Record<string, any> = Record<string, any>>(obj: Schema<T> | JsonSchema<T>): Record<string, JsonSchemaProperty<T>>;
14
14
  /**
15
- * A helper function to get a list of properties from a JTD object schema. This function takes an {@link ObjectSchema} as input and returns an array of its properties, where each element is an object containing the property name, the corresponding JTD schema type, and an `optional` flag indicating whether the property is optional (i.e., defined in `optionalProperties`) or required (i.e., defined in `properties`). The function utilizes the `getProperties` helper function to first retrieve the properties as a record and then converts that record into an array format for easier iteration and access.
15
+ * Returns object properties as an array.
16
+ *
17
+ * @remarks
18
+ * This is a convenience function that extracts properties using `getProperties` and returns them as an array.
16
19
  *
17
- * @param obj - The {@link ObjectSchema} from which to extract the properties.
18
- * @returns An array of properties defined in the JTD object schema, where each element is an object containing the property name, the corresponding JTD schema type, and an `optional` flag.
20
+ * @param obj - The JSON Schema object form or a Schema wrapper to extract properties from.
21
+ * @returns An array of JSON Schema fragments representing the properties, each including metadata.
19
22
  */
20
- declare function getPropertiesList<TMetadata extends SchemaMetadata>(obj: ObjectSchema<TMetadata> | JTDSchemaObjectType<TMetadata>): Array<JTDSchemaType<TMetadata> & {
21
- name: string;
22
- optional: boolean;
23
- }>;
23
+ declare function getPropertiesList<T extends Record<string, any> = Record<string, any>>(obj: Schema<T> | JsonSchema<T>): Array<JsonSchemaProperty<T>>;
24
24
  /**
25
- * A helper function to add a property to a JTD object schema. This function takes a JTD object schema (either {@link JTDSchemaObjectType} or {@link ObjectSchema}), a property name, and a JTD schema type as input. It adds the specified property to the appropriate section of the JTD schema based on whether the property is optional or required. If the property is optional (i.e., has `nullable: true`), it is added to the `optionalProperties` section of the schema; otherwise, it is added to the `properties` section. The function also ensures that the property's metadata includes its name for easier identification and access in future operations.
25
+ * Adds a property to a JSON Schema object form.
26
+ *
27
+ * @remarks
28
+ * This function modifies the provided schema in place by adding a new property with the specified name and schema. It also updates the `required` array based on the `optional` flag of the property. If the property is marked as optional, it will be removed from the `required` array; otherwise, it will be added to it.
26
29
  *
27
- * @param obj - The JTD object schema to which the property should be added.
30
+ * @param obj - The JSON Schema object form or a Schema wrapper to which the property should be added.
28
31
  * @param name - The name of the property to add.
29
- * @param property - The JTD schema type of the property to add, which may include a `nullable` flag indicating whether the property is optional.
32
+ * @param property - The JSON Schema fragment representing the property's schema, including metadata.
33
+ * @throws Will throw an error if the provided schema is not an object form.
30
34
  */
31
- declare function addProperty<TMetadata extends SchemaMetadata>(obj: ObjectSchema<TMetadata> | JTDSchemaObjectType<TMetadata>, name: string, property: JTDSchemaType<TMetadata>): void;
35
+ declare function addProperty<T extends Record<string, any> = Record<string, any>>(obj: Schema<T> | JsonSchema<T>, name: string, property: JsonSchemaProperty<T>): void;
32
36
  /**
33
- * Merges multiple JTD object schemas into a single schema. This function takes an array of JTD object schemas (either {@link JTDSchemaObjectType} or {@link ObjectSchema}) and combines their properties into a single JTD object schema. The resulting schema will contain all properties from the input schemas, with optional properties marked accordingly. If there are overlapping properties between the input schemas, the function will merge them using a deep merge strategy (via `defu`) if they are both JTD object schemas; otherwise, the property from the first schema in the input array will take precedence.
37
+ * Merges multiple JSON Schema object forms into one.
34
38
  *
35
39
  * @remarks
36
- * This function attempts to mimic {@link defu}'s behavior for merging objects, but with special handling for JTD schemas. When merging properties, if a property exists in multiple input schemas and is defined as a JTD object schema in all of them, the function will perform a deep merge of those schemas. If the property is not a JTD object schema in any of the input schemas, the function will simply take the first definition it encounters.
40
+ * This function takes multiple JSON Schema objects or Schema wrappers and merges them into a single JSON Schema object. The merging process combines properties and metadata from all provided schemas, with later schemas in the arguments list taking precedence over earlier ones in case of conflicts. The resulting schema will include all unique properties and metadata from the input schemas.
37
41
  *
38
- * @param schemas - An array of JTD object schemas to merge.
39
- * @returns A single JTD object schema that combines the properties of all input schemas.
42
+ * @param schemas - An array of JSON Schema objects or Schema wrappers to merge.
43
+ * @returns A new JSON Schema object that is the result of merging all input schemas.
40
44
  */
41
- declare function mergeSchemas<TMetadata extends SchemaMetadata>(...schemas: (JTDSchemaObjectType<TMetadata> | ObjectSchema<TMetadata>)[]): JTDSchemaObjectType<TMetadata>;
45
+ declare function mergeSchemas<T extends Record<string, any> = Record<string, any>>(...schemas: (JsonSchema<T> | Schema<T>)[]): JsonSchema<T>;
42
46
  //#endregion
43
47
  export { addProperty, getProperties, getPropertiesList, mergeSchemas };
44
48
  //# sourceMappingURL=helpers.d.cts.map
@@ -1 +1 @@
1
- {"version":3,"file":"helpers.d.cts","names":[],"sources":["../src/helpers.ts"],"mappings":";;;;;AAkCA;;;;iBAAgB,aAAA,mBAAgC,cAAA,CAAA,CAC9C,GAAA,EAAK,YAAA,CAAa,SAAA,IAAa,mBAAA,CAAoB,SAAA,IAClD,MAAA,SAED,aAAA,CAAc,SAAA;EAAe,IAAA;EAAc,QAAA;AAAA;;;;;;;iBAgC7B,iBAAA,mBAAoC,cAAA,CAAA,CAClD,GAAA,EAAK,YAAA,CAAa,SAAA,IAAa,mBAAA,CAAoB,SAAA,IAClD,KAAA,CAAM,aAAA,CAAc,SAAA;EAAe,IAAA;EAAc,QAAA;AAAA;;;;;;;;iBAWpC,WAAA,mBAA8B,cAAA,CAAA,CAC5C,GAAA,EAAK,YAAA,CAAa,SAAA,IAAa,mBAAA,CAAoB,SAAA,GACnD,IAAA,UACA,QAAA,EAAU,aAAA,CAAc,SAAA;AAhD2B;AAgCrD;;;;;;;;AAhCqD,iBA2ErC,YAAA,mBAA+B,cAAA,CAAA,CAAA,GAC1C,OAAA,GAAU,mBAAA,CAAoB,SAAA,IAAa,YAAA,CAAa,SAAA,OAC1D,mBAAA,CAAoB,SAAA"}
1
+ {"version":3,"file":"helpers.d.cts","names":[],"sources":["../src/helpers.ts"],"mappings":";;;;;AAsCA;;;;;;;iBAAgB,aAAA,WACJ,MAAA,gBAAsB,MAAA,cAAA,CAChC,GAAA,EAAK,MAAA,CAAO,CAAA,IAAK,UAAA,CAAW,CAAA,IAAK,MAAA,SAAe,kBAAA,CAAmB,CAAA;;;;;;;;;;iBA2BrD,iBAAA,WACJ,MAAA,gBAAsB,MAAA,cAAA,CAChC,GAAA,EAAK,MAAA,CAAO,CAAA,IAAK,UAAA,CAAW,CAAA,IAAK,KAAA,CAAM,kBAAA,CAAmB,CAAA;;;;;;;;;AA7BU;AA2BtE;;iBAiBgB,WAAA,WACJ,MAAA,gBAAsB,MAAA,cAAA,CAEhC,GAAA,EAAK,MAAA,CAAO,CAAA,IAAK,UAAA,CAAW,CAAA,GAC5B,IAAA,UACA,QAAA,EAAU,kBAAA,CAAmB,CAAA;;;;;;;;;;iBA+Bf,YAAA,WACJ,MAAA,gBAAsB,MAAA,cAAA,CAAA,GAC7B,OAAA,GAAU,UAAA,CAAW,CAAA,IAAK,MAAA,CAAO,CAAA,OAAQ,UAAA,CAAW,CAAA"}
@@ -1,44 +1,48 @@
1
- import { JTDSchemaObjectType, JTDSchemaType, ObjectSchema, SchemaMetadata } from "./types.mjs";
1
+ import { JsonSchema, JsonSchemaProperty, Schema } from "./types.mjs";
2
2
 
3
3
  //#region src/helpers.d.ts
4
4
  /**
5
- * A helper function to extract the properties from a JTD object schema. This function takes an {@link ObjectSchema} as input and returns a record of its properties, where each key is the property name and the value is the corresponding JTD schema type along with an `optional` flag indicating whether the property is optional (i.e., defined in `optionalProperties`) or required (i.e., defined in `properties`). The function checks both `properties` and `optionalProperties` of the JTD schema to construct the resulting record.
5
+ * Extracts object properties from a JSON Schema object form.
6
6
  *
7
- * @param obj - The {@link ObjectSchema} from which to extract the properties.
8
- * @returns A record of the properties defined in the JTD object schema, where each key is the property name and the value is an object containing the JTD schema type and an `optional` flag.
7
+ * @remarks
8
+ * This function returns an empty object if the schema is not an object form or if it has no properties.
9
+ *
10
+ * @param obj - The JSON Schema object form or a Schema wrapper to extract properties from.
11
+ * @returns An object mapping property names to their corresponding JSON Schema fragments, including metadata.
9
12
  */
10
- declare function getProperties<TMetadata extends SchemaMetadata>(obj: ObjectSchema<TMetadata> | JTDSchemaObjectType<TMetadata>): Record<string, JTDSchemaType<TMetadata> & {
11
- name: string;
12
- optional: boolean;
13
- }>;
13
+ declare function getProperties<T extends Record<string, any> = Record<string, any>>(obj: Schema<T> | JsonSchema<T>): Record<string, JsonSchemaProperty<T>>;
14
14
  /**
15
- * A helper function to get a list of properties from a JTD object schema. This function takes an {@link ObjectSchema} as input and returns an array of its properties, where each element is an object containing the property name, the corresponding JTD schema type, and an `optional` flag indicating whether the property is optional (i.e., defined in `optionalProperties`) or required (i.e., defined in `properties`). The function utilizes the `getProperties` helper function to first retrieve the properties as a record and then converts that record into an array format for easier iteration and access.
15
+ * Returns object properties as an array.
16
+ *
17
+ * @remarks
18
+ * This is a convenience function that extracts properties using `getProperties` and returns them as an array.
16
19
  *
17
- * @param obj - The {@link ObjectSchema} from which to extract the properties.
18
- * @returns An array of properties defined in the JTD object schema, where each element is an object containing the property name, the corresponding JTD schema type, and an `optional` flag.
20
+ * @param obj - The JSON Schema object form or a Schema wrapper to extract properties from.
21
+ * @returns An array of JSON Schema fragments representing the properties, each including metadata.
19
22
  */
20
- declare function getPropertiesList<TMetadata extends SchemaMetadata>(obj: ObjectSchema<TMetadata> | JTDSchemaObjectType<TMetadata>): Array<JTDSchemaType<TMetadata> & {
21
- name: string;
22
- optional: boolean;
23
- }>;
23
+ declare function getPropertiesList<T extends Record<string, any> = Record<string, any>>(obj: Schema<T> | JsonSchema<T>): Array<JsonSchemaProperty<T>>;
24
24
  /**
25
- * A helper function to add a property to a JTD object schema. This function takes a JTD object schema (either {@link JTDSchemaObjectType} or {@link ObjectSchema}), a property name, and a JTD schema type as input. It adds the specified property to the appropriate section of the JTD schema based on whether the property is optional or required. If the property is optional (i.e., has `nullable: true`), it is added to the `optionalProperties` section of the schema; otherwise, it is added to the `properties` section. The function also ensures that the property's metadata includes its name for easier identification and access in future operations.
25
+ * Adds a property to a JSON Schema object form.
26
+ *
27
+ * @remarks
28
+ * This function modifies the provided schema in place by adding a new property with the specified name and schema. It also updates the `required` array based on the `optional` flag of the property. If the property is marked as optional, it will be removed from the `required` array; otherwise, it will be added to it.
26
29
  *
27
- * @param obj - The JTD object schema to which the property should be added.
30
+ * @param obj - The JSON Schema object form or a Schema wrapper to which the property should be added.
28
31
  * @param name - The name of the property to add.
29
- * @param property - The JTD schema type of the property to add, which may include a `nullable` flag indicating whether the property is optional.
32
+ * @param property - The JSON Schema fragment representing the property's schema, including metadata.
33
+ * @throws Will throw an error if the provided schema is not an object form.
30
34
  */
31
- declare function addProperty<TMetadata extends SchemaMetadata>(obj: ObjectSchema<TMetadata> | JTDSchemaObjectType<TMetadata>, name: string, property: JTDSchemaType<TMetadata>): void;
35
+ declare function addProperty<T extends Record<string, any> = Record<string, any>>(obj: Schema<T> | JsonSchema<T>, name: string, property: JsonSchemaProperty<T>): void;
32
36
  /**
33
- * Merges multiple JTD object schemas into a single schema. This function takes an array of JTD object schemas (either {@link JTDSchemaObjectType} or {@link ObjectSchema}) and combines their properties into a single JTD object schema. The resulting schema will contain all properties from the input schemas, with optional properties marked accordingly. If there are overlapping properties between the input schemas, the function will merge them using a deep merge strategy (via `defu`) if they are both JTD object schemas; otherwise, the property from the first schema in the input array will take precedence.
37
+ * Merges multiple JSON Schema object forms into one.
34
38
  *
35
39
  * @remarks
36
- * This function attempts to mimic {@link defu}'s behavior for merging objects, but with special handling for JTD schemas. When merging properties, if a property exists in multiple input schemas and is defined as a JTD object schema in all of them, the function will perform a deep merge of those schemas. If the property is not a JTD object schema in any of the input schemas, the function will simply take the first definition it encounters.
40
+ * This function takes multiple JSON Schema objects or Schema wrappers and merges them into a single JSON Schema object. The merging process combines properties and metadata from all provided schemas, with later schemas in the arguments list taking precedence over earlier ones in case of conflicts. The resulting schema will include all unique properties and metadata from the input schemas.
37
41
  *
38
- * @param schemas - An array of JTD object schemas to merge.
39
- * @returns A single JTD object schema that combines the properties of all input schemas.
42
+ * @param schemas - An array of JSON Schema objects or Schema wrappers to merge.
43
+ * @returns A new JSON Schema object that is the result of merging all input schemas.
40
44
  */
41
- declare function mergeSchemas<TMetadata extends SchemaMetadata>(...schemas: (JTDSchemaObjectType<TMetadata> | ObjectSchema<TMetadata>)[]): JTDSchemaObjectType<TMetadata>;
45
+ declare function mergeSchemas<T extends Record<string, any> = Record<string, any>>(...schemas: (JsonSchema<T> | Schema<T>)[]): JsonSchema<T>;
42
46
  //#endregion
43
47
  export { addProperty, getProperties, getPropertiesList, mergeSchemas };
44
48
  //# sourceMappingURL=helpers.d.mts.map
@@ -1 +1 @@
1
- {"version":3,"file":"helpers.d.mts","names":[],"sources":["../src/helpers.ts"],"mappings":";;;;;AAkCA;;;;iBAAgB,aAAA,mBAAgC,cAAA,CAAA,CAC9C,GAAA,EAAK,YAAA,CAAa,SAAA,IAAa,mBAAA,CAAoB,SAAA,IAClD,MAAA,SAED,aAAA,CAAc,SAAA;EAAe,IAAA;EAAc,QAAA;AAAA;;;;;;;iBAgC7B,iBAAA,mBAAoC,cAAA,CAAA,CAClD,GAAA,EAAK,YAAA,CAAa,SAAA,IAAa,mBAAA,CAAoB,SAAA,IAClD,KAAA,CAAM,aAAA,CAAc,SAAA;EAAe,IAAA;EAAc,QAAA;AAAA;;;;;;;;iBAWpC,WAAA,mBAA8B,cAAA,CAAA,CAC5C,GAAA,EAAK,YAAA,CAAa,SAAA,IAAa,mBAAA,CAAoB,SAAA,GACnD,IAAA,UACA,QAAA,EAAU,aAAA,CAAc,SAAA;AAhD2B;AAgCrD;;;;;;;;AAhCqD,iBA2ErC,YAAA,mBAA+B,cAAA,CAAA,CAAA,GAC1C,OAAA,GAAU,mBAAA,CAAoB,SAAA,IAAa,YAAA,CAAa,SAAA,OAC1D,mBAAA,CAAoB,SAAA"}
1
+ {"version":3,"file":"helpers.d.mts","names":[],"sources":["../src/helpers.ts"],"mappings":";;;;;AAsCA;;;;;;;iBAAgB,aAAA,WACJ,MAAA,gBAAsB,MAAA,cAAA,CAChC,GAAA,EAAK,MAAA,CAAO,CAAA,IAAK,UAAA,CAAW,CAAA,IAAK,MAAA,SAAe,kBAAA,CAAmB,CAAA;;;;;;;;;;iBA2BrD,iBAAA,WACJ,MAAA,gBAAsB,MAAA,cAAA,CAChC,GAAA,EAAK,MAAA,CAAO,CAAA,IAAK,UAAA,CAAW,CAAA,IAAK,KAAA,CAAM,kBAAA,CAAmB,CAAA;;;;;;;;;AA7BU;AA2BtE;;iBAiBgB,WAAA,WACJ,MAAA,gBAAsB,MAAA,cAAA,CAEhC,GAAA,EAAK,MAAA,CAAO,CAAA,IAAK,UAAA,CAAW,CAAA,GAC5B,IAAA,UACA,QAAA,EAAU,kBAAA,CAAmB,CAAA;;;;;;;;;;iBA+Bf,YAAA,WACJ,MAAA,gBAAsB,MAAA,cAAA,CAAA,GAC7B,OAAA,GAAU,UAAA,CAAW,CAAA,IAAK,MAAA,CAAO,CAAA,OAAQ,UAAA,CAAW,CAAA"}
package/dist/helpers.mjs CHANGED
@@ -1,88 +1,82 @@
1
- import { isJTDSchemaObject, isObjectSchema } from "./type-checks.mjs";
1
+ import { isPropertyOptional, isSchemaNullable } from "./metadata.mjs";
2
+ import { isJsonSchemaObject, isSchema } from "./type-checks.mjs";
2
3
  import { defu as defu$1 } from "defu";
3
4
  import { isSetObject } from "@stryke/type-checks";
4
5
 
5
6
  //#region src/helpers.ts
6
7
  /**
7
- * A helper function to extract the properties from a JTD object schema. This function takes an {@link ObjectSchema} as input and returns a record of its properties, where each key is the property name and the value is the corresponding JTD schema type along with an `optional` flag indicating whether the property is optional (i.e., defined in `optionalProperties`) or required (i.e., defined in `properties`). The function checks both `properties` and `optionalProperties` of the JTD schema to construct the resulting record.
8
+ * Extracts object properties from a JSON Schema object form.
8
9
  *
9
- * @param obj - The {@link ObjectSchema} from which to extract the properties.
10
- * @returns A record of the properties defined in the JTD object schema, where each key is the property name and the value is an object containing the JTD schema type and an `optional` flag.
10
+ * @remarks
11
+ * This function returns an empty object if the schema is not an object form or if it has no properties.
12
+ *
13
+ * @param obj - The JSON Schema object form or a Schema wrapper to extract properties from.
14
+ * @returns An object mapping property names to their corresponding JSON Schema fragments, including metadata.
11
15
  */
12
16
  function getProperties(obj) {
13
17
  const properties = {};
14
- const schema = isObjectSchema(obj) ? obj.schema : obj;
15
- if ("optionalProperties" in schema && isSetObject(schema.optionalProperties)) for (const [key, value] of Object.entries(schema.optionalProperties)) properties[key] = {
16
- ...value,
17
- name: key,
18
- optional: true
19
- };
20
- if ("properties" in schema && isSetObject(schema.properties)) for (const [key, value] of Object.entries(schema.properties)) properties[key] = {
18
+ const schema = isSchema(obj) ? obj.schema : obj;
19
+ if (!isJsonSchemaObject(schema) || !isSetObject(schema.properties)) return properties;
20
+ for (const [key, value] of Object.entries(schema.properties)) properties[key] = {
21
21
  ...value,
22
22
  name: key,
23
- optional: false
23
+ nullable: isSchemaNullable(value) || isPropertyOptional(schema, key)
24
24
  };
25
25
  return properties;
26
26
  }
27
27
  /**
28
- * A helper function to get a list of properties from a JTD object schema. This function takes an {@link ObjectSchema} as input and returns an array of its properties, where each element is an object containing the property name, the corresponding JTD schema type, and an `optional` flag indicating whether the property is optional (i.e., defined in `optionalProperties`) or required (i.e., defined in `properties`). The function utilizes the `getProperties` helper function to first retrieve the properties as a record and then converts that record into an array format for easier iteration and access.
28
+ * Returns object properties as an array.
29
+ *
30
+ * @remarks
31
+ * This is a convenience function that extracts properties using `getProperties` and returns them as an array.
29
32
  *
30
- * @param obj - The {@link ObjectSchema} from which to extract the properties.
31
- * @returns An array of properties defined in the JTD object schema, where each element is an object containing the property name, the corresponding JTD schema type, and an `optional` flag.
33
+ * @param obj - The JSON Schema object form or a Schema wrapper to extract properties from.
34
+ * @returns An array of JSON Schema fragments representing the properties, each including metadata.
32
35
  */
33
36
  function getPropertiesList(obj) {
34
37
  return Object.values(getProperties(obj));
35
38
  }
36
39
  /**
37
- * A helper function to add a property to a JTD object schema. This function takes a JTD object schema (either {@link JTDSchemaObjectType} or {@link ObjectSchema}), a property name, and a JTD schema type as input. It adds the specified property to the appropriate section of the JTD schema based on whether the property is optional or required. If the property is optional (i.e., has `nullable: true`), it is added to the `optionalProperties` section of the schema; otherwise, it is added to the `properties` section. The function also ensures that the property's metadata includes its name for easier identification and access in future operations.
40
+ * Adds a property to a JSON Schema object form.
41
+ *
42
+ * @remarks
43
+ * This function modifies the provided schema in place by adding a new property with the specified name and schema. It also updates the `required` array based on the `optional` flag of the property. If the property is marked as optional, it will be removed from the `required` array; otherwise, it will be added to it.
38
44
  *
39
- * @param obj - The JTD object schema to which the property should be added.
45
+ * @param obj - The JSON Schema object form or a Schema wrapper to which the property should be added.
40
46
  * @param name - The name of the property to add.
41
- * @param property - The JTD schema type of the property to add, which may include a `nullable` flag indicating whether the property is optional.
47
+ * @param property - The JSON Schema fragment representing the property's schema, including metadata.
48
+ * @throws Will throw an error if the provided schema is not an object form.
42
49
  */
43
50
  function addProperty(obj, name, property) {
44
- const schema = isObjectSchema(obj) ? obj.schema : obj;
45
- if (property.nullable) {
46
- schema.optionalProperties ??= {};
47
- schema.optionalProperties[name] = {
48
- ...property,
49
- metadata: {
50
- ...property.metadata,
51
- name
52
- }
53
- };
54
- } else {
55
- schema.properties ??= {};
56
- schema.properties[name] = {
57
- ...property,
58
- metadata: {
59
- ...property.metadata,
60
- name
61
- }
62
- };
63
- }
51
+ const schema = isSchema(obj) ? obj.schema : obj;
52
+ if (!isJsonSchemaObject(schema)) throw new Error("Cannot add property to non-object schema");
53
+ schema.properties ??= {};
54
+ schema.required ??= [];
55
+ schema.properties[name] = {
56
+ ...property,
57
+ name
58
+ };
59
+ if (property?.optional) schema.required = schema.required.filter((key) => key !== name);
60
+ else if (!schema.required.includes(name)) schema.required.push(name);
61
+ if (schema.required.length === 0) delete schema.required;
64
62
  }
65
63
  /**
66
- * Merges multiple JTD object schemas into a single schema. This function takes an array of JTD object schemas (either {@link JTDSchemaObjectType} or {@link ObjectSchema}) and combines their properties into a single JTD object schema. The resulting schema will contain all properties from the input schemas, with optional properties marked accordingly. If there are overlapping properties between the input schemas, the function will merge them using a deep merge strategy (via `defu`) if they are both JTD object schemas; otherwise, the property from the first schema in the input array will take precedence.
64
+ * Merges multiple JSON Schema object forms into one.
67
65
  *
68
66
  * @remarks
69
- * This function attempts to mimic {@link defu}'s behavior for merging objects, but with special handling for JTD schemas. When merging properties, if a property exists in multiple input schemas and is defined as a JTD object schema in all of them, the function will perform a deep merge of those schemas. If the property is not a JTD object schema in any of the input schemas, the function will simply take the first definition it encounters.
67
+ * This function takes multiple JSON Schema objects or Schema wrappers and merges them into a single JSON Schema object. The merging process combines properties and metadata from all provided schemas, with later schemas in the arguments list taking precedence over earlier ones in case of conflicts. The resulting schema will include all unique properties and metadata from the input schemas.
70
68
  *
71
- * @param schemas - An array of JTD object schemas to merge.
72
- * @returns A single JTD object schema that combines the properties of all input schemas.
69
+ * @param schemas - An array of JSON Schema objects or Schema wrappers to merge.
70
+ * @returns A new JSON Schema object that is the result of merging all input schemas.
73
71
  */
74
72
  function mergeSchemas(...schemas) {
75
- const mergedSchema = {
76
- properties: {},
77
- optionalProperties: {},
78
- additionalProperties: false
79
- };
80
- for (const schema of schemas.reverse()) {
81
- const properties = getProperties(schema);
82
- for (const [key, value] of Object.entries(properties)) if (value.optional) mergedSchema.optionalProperties[key] = mergedSchema.optionalProperties[key] && isJTDSchemaObject(mergedSchema.optionalProperties[key]) && isJTDSchemaObject(value) ? defu$1(mergedSchema.optionalProperties[key], value) : value;
83
- else mergedSchema.properties[key] = mergedSchema.properties[key] && isJTDSchemaObject(mergedSchema.properties[key]) && isJTDSchemaObject(value) ? defu$1(mergedSchema.properties[key], value) : value;
73
+ const result = {};
74
+ for (const schema of schemas) {
75
+ const jsonSchema = isSchema(schema) ? schema.schema : schema;
76
+ if (!isJsonSchemaObject(jsonSchema)) continue;
77
+ defu$1(result, jsonSchema);
84
78
  }
85
- return mergedSchema;
79
+ return result;
86
80
  }
87
81
 
88
82
  //#endregion
@@ -1 +1 @@
1
- {"version":3,"file":"helpers.mjs","names":["defu"],"sources":["../src/helpers.ts"],"sourcesContent":["/* -------------------------------------------------------------------\n\n ⚡ Storm Software - Powerlines\n\n This code was released as part of the Powerlines project. Powerlines\n is maintained by Storm Software under the Apache-2.0 license, and is\n free for commercial and private use. For more information, please visit\n our licensing page at https://stormsoftware.com/licenses/projects/powerlines.\n\n Website: https://stormsoftware.com\n Repository: https://github.com/storm-software/powerlines\n Documentation: https://docs.stormsoftware.com/projects/powerlines\n Contact: https://stormsoftware.com/contact\n\n SPDX-License-Identifier: Apache-2.0\n\n ------------------------------------------------------------------- */\n\nimport { isSetObject } from \"@stryke/type-checks\";\nimport { defu } from \"defu\";\nimport { isJTDSchemaObject, isObjectSchema } from \"./type-checks\";\nimport {\n JTDSchemaObjectType,\n JTDSchemaType,\n ObjectSchema,\n SchemaMetadata\n} from \"./types\";\n\n/**\n * A helper function to extract the properties from a JTD object schema. This function takes an {@link ObjectSchema} as input and returns a record of its properties, where each key is the property name and the value is the corresponding JTD schema type along with an `optional` flag indicating whether the property is optional (i.e., defined in `optionalProperties`) or required (i.e., defined in `properties`). The function checks both `properties` and `optionalProperties` of the JTD schema to construct the resulting record.\n *\n * @param obj - The {@link ObjectSchema} from which to extract the properties.\n * @returns A record of the properties defined in the JTD object schema, where each key is the property name and the value is an object containing the JTD schema type and an `optional` flag.\n */\nexport function getProperties<TMetadata extends SchemaMetadata>(\n obj: ObjectSchema<TMetadata> | JTDSchemaObjectType<TMetadata>\n): Record<\n string,\n JTDSchemaType<TMetadata> & { name: string; optional: boolean }\n> {\n const properties: Record<\n string,\n JTDSchemaType<TMetadata> & { name: string; optional: boolean }\n > = {};\n\n const schema = isObjectSchema(obj) ? obj.schema : obj;\n if (\n \"optionalProperties\" in schema &&\n isSetObject(schema.optionalProperties)\n ) {\n for (const [key, value] of Object.entries(schema.optionalProperties)) {\n properties[key] = { ...value, name: key, optional: true };\n }\n }\n\n if (\"properties\" in schema && isSetObject(schema.properties)) {\n for (const [key, value] of Object.entries(schema.properties)) {\n properties[key] = { ...value, name: key, optional: false };\n }\n }\n\n return properties;\n}\n\n/**\n * A helper function to get a list of properties from a JTD object schema. This function takes an {@link ObjectSchema} as input and returns an array of its properties, where each element is an object containing the property name, the corresponding JTD schema type, and an `optional` flag indicating whether the property is optional (i.e., defined in `optionalProperties`) or required (i.e., defined in `properties`). The function utilizes the `getProperties` helper function to first retrieve the properties as a record and then converts that record into an array format for easier iteration and access.\n *\n * @param obj - The {@link ObjectSchema} from which to extract the properties.\n * @returns An array of properties defined in the JTD object schema, where each element is an object containing the property name, the corresponding JTD schema type, and an `optional` flag.\n */\nexport function getPropertiesList<TMetadata extends SchemaMetadata>(\n obj: ObjectSchema<TMetadata> | JTDSchemaObjectType<TMetadata>\n): Array<JTDSchemaType<TMetadata> & { name: string; optional: boolean }> {\n return Object.values(getProperties(obj));\n}\n\n/**\n * A helper function to add a property to a JTD object schema. This function takes a JTD object schema (either {@link JTDSchemaObjectType} or {@link ObjectSchema}), a property name, and a JTD schema type as input. It adds the specified property to the appropriate section of the JTD schema based on whether the property is optional or required. If the property is optional (i.e., has `nullable: true`), it is added to the `optionalProperties` section of the schema; otherwise, it is added to the `properties` section. The function also ensures that the property's metadata includes its name for easier identification and access in future operations.\n *\n * @param obj - The JTD object schema to which the property should be added.\n * @param name - The name of the property to add.\n * @param property - The JTD schema type of the property to add, which may include a `nullable` flag indicating whether the property is optional.\n */\nexport function addProperty<TMetadata extends SchemaMetadata>(\n obj: ObjectSchema<TMetadata> | JTDSchemaObjectType<TMetadata>,\n name: string,\n property: JTDSchemaType<TMetadata>\n) {\n const schema = isObjectSchema(obj) ? obj.schema : obj;\n if (property.nullable) {\n schema.optionalProperties ??= {};\n schema.optionalProperties[name] = {\n ...property,\n metadata: { ...property.metadata, name } as TMetadata\n };\n } else {\n schema.properties ??= {};\n schema.properties[name] = {\n ...property,\n metadata: { ...property.metadata, name } as TMetadata\n };\n }\n}\n\n/**\n * Merges multiple JTD object schemas into a single schema. This function takes an array of JTD object schemas (either {@link JTDSchemaObjectType} or {@link ObjectSchema}) and combines their properties into a single JTD object schema. The resulting schema will contain all properties from the input schemas, with optional properties marked accordingly. If there are overlapping properties between the input schemas, the function will merge them using a deep merge strategy (via `defu`) if they are both JTD object schemas; otherwise, the property from the first schema in the input array will take precedence.\n *\n * @remarks\n * This function attempts to mimic {@link defu}'s behavior for merging objects, but with special handling for JTD schemas. When merging properties, if a property exists in multiple input schemas and is defined as a JTD object schema in all of them, the function will perform a deep merge of those schemas. If the property is not a JTD object schema in any of the input schemas, the function will simply take the first definition it encounters.\n *\n * @param schemas - An array of JTD object schemas to merge.\n * @returns A single JTD object schema that combines the properties of all input schemas.\n */\nexport function mergeSchemas<TMetadata extends SchemaMetadata>(\n ...schemas: (JTDSchemaObjectType<TMetadata> | ObjectSchema<TMetadata>)[]\n): JTDSchemaObjectType<TMetadata> {\n const mergedSchema = {\n properties: {},\n optionalProperties: {},\n additionalProperties: false\n } as JTDSchemaObjectType<TMetadata>;\n for (const schema of schemas.reverse()) {\n const properties = getProperties(schema);\n for (const [key, value] of Object.entries(properties)) {\n if (value.optional) {\n mergedSchema.optionalProperties![key] = (\n mergedSchema.optionalProperties![key] &&\n isJTDSchemaObject(mergedSchema.optionalProperties![key]) &&\n isJTDSchemaObject(value)\n ? defu(mergedSchema.optionalProperties![key], value)\n : value\n ) as JTDSchemaType<TMetadata>;\n } else {\n mergedSchema.properties![key] = (\n mergedSchema.properties![key] &&\n isJTDSchemaObject(mergedSchema.properties![key]) &&\n isJTDSchemaObject(value)\n ? defu(mergedSchema.properties![key], value)\n : value\n ) as JTDSchemaType<TMetadata>;\n }\n }\n }\n\n return mergedSchema;\n}\n"],"mappings":";;;;;;;;;;;AAkCA,SAAgB,cACd,KAIA;CACA,MAAM,aAGF,CAAC;CAEL,MAAM,SAAS,eAAe,GAAG,IAAI,IAAI,SAAS;CAClD,IACE,wBAAwB,UACxB,YAAY,OAAO,kBAAkB,GAErC,KAAK,MAAM,CAAC,KAAK,UAAU,OAAO,QAAQ,OAAO,kBAAkB,GACjE,WAAW,OAAO;EAAE,GAAG;EAAO,MAAM;EAAK,UAAU;CAAK;CAI5D,IAAI,gBAAgB,UAAU,YAAY,OAAO,UAAU,GACzD,KAAK,MAAM,CAAC,KAAK,UAAU,OAAO,QAAQ,OAAO,UAAU,GACzD,WAAW,OAAO;EAAE,GAAG;EAAO,MAAM;EAAK,UAAU;CAAM;CAI7D,OAAO;AACT;;;;;;;AAQA,SAAgB,kBACd,KACuE;CACvE,OAAO,OAAO,OAAO,cAAc,GAAG,CAAC;AACzC;;;;;;;;AASA,SAAgB,YACd,KACA,MACA,UACA;CACA,MAAM,SAAS,eAAe,GAAG,IAAI,IAAI,SAAS;CAClD,IAAI,SAAS,UAAU;EACrB,OAAO,uBAAuB,CAAC;EAC/B,OAAO,mBAAmB,QAAQ;GAChC,GAAG;GACH,UAAU;IAAE,GAAG,SAAS;IAAU;GAAK;EACzC;CACF,OAAO;EACL,OAAO,eAAe,CAAC;EACvB,OAAO,WAAW,QAAQ;GACxB,GAAG;GACH,UAAU;IAAE,GAAG,SAAS;IAAU;GAAK;EACzC;CACF;AACF;;;;;;;;;;AAWA,SAAgB,aACd,GAAG,SAC6B;CAChC,MAAM,eAAe;EACnB,YAAY,CAAC;EACb,oBAAoB,CAAC;EACrB,sBAAsB;CACxB;CACA,KAAK,MAAM,UAAU,QAAQ,QAAQ,GAAG;EACtC,MAAM,aAAa,cAAc,MAAM;EACvC,KAAK,MAAM,CAAC,KAAK,UAAU,OAAO,QAAQ,UAAU,GAClD,IAAI,MAAM,UACR,aAAa,mBAAoB,OAC/B,aAAa,mBAAoB,QACjC,kBAAkB,aAAa,mBAAoB,IAAI,KACvD,kBAAkB,KAAK,IACnBA,OAAK,aAAa,mBAAoB,MAAM,KAAK,IACjD;OAGN,aAAa,WAAY,OACvB,aAAa,WAAY,QACzB,kBAAkB,aAAa,WAAY,IAAI,KAC/C,kBAAkB,KAAK,IACnBA,OAAK,aAAa,WAAY,MAAM,KAAK,IACzC;CAIZ;CAEA,OAAO;AACT"}
1
+ {"version":3,"file":"helpers.mjs","names":[],"sources":["../src/helpers.ts"],"sourcesContent":["/* -------------------------------------------------------------------\n\n ⚡ Storm Software - Powerlines\n\n This code was released as part of the Powerlines project. Powerlines\n is maintained by Storm Software under the Apache-2.0 license, and is\n free for commercial and private use. For more information, please visit\n our licensing page at https://stormsoftware.com/licenses/projects/powerlines.\n\n Website: https://stormsoftware.com\n Repository: https://github.com/storm-software/powerlines\n Documentation: https://docs.stormsoftware.com/projects/powerlines\n Contact: https://stormsoftware.com/contact\n\n SPDX-License-Identifier: Apache-2.0\n\n ------------------------------------------------------------------- */\n\nimport { isSetObject } from \"@stryke/type-checks\";\nimport { defu } from \"defu\";\nimport { isPropertyOptional, isSchemaNullable } from \"./metadata\";\nimport { isJsonSchemaObject, isSchema } from \"./type-checks\";\nimport {\n JsonSchema,\n JsonSchemaObject,\n JsonSchemaProperty,\n Schema\n} from \"./types\";\n\n/**\n * Extracts object properties from a JSON Schema object form.\n *\n * @remarks\n * This function returns an empty object if the schema is not an object form or if it has no properties.\n *\n * @param obj - The JSON Schema object form or a Schema wrapper to extract properties from.\n * @returns An object mapping property names to their corresponding JSON Schema fragments, including metadata.\n */\nexport function getProperties<\n T extends Record<string, any> = Record<string, any>\n>(obj: Schema<T> | JsonSchema<T>): Record<string, JsonSchemaProperty<T>> {\n const properties: Record<string, JsonSchemaProperty<T>> = {};\n const schema: JsonSchema<T> = isSchema<T>(obj) ? obj.schema : obj;\n if (!isJsonSchemaObject<T>(schema) || !isSetObject(schema.properties)) {\n return properties;\n }\n\n for (const [key, value] of Object.entries(schema.properties)) {\n properties[key] = {\n ...value,\n name: key,\n nullable: isSchemaNullable<T>(value) || isPropertyOptional(schema, key)\n };\n }\n\n return properties;\n}\n\n/**\n * Returns object properties as an array.\n *\n * @remarks\n * This is a convenience function that extracts properties using `getProperties` and returns them as an array.\n *\n * @param obj - The JSON Schema object form or a Schema wrapper to extract properties from.\n * @returns An array of JSON Schema fragments representing the properties, each including metadata.\n */\nexport function getPropertiesList<\n T extends Record<string, any> = Record<string, any>\n>(obj: Schema<T> | JsonSchema<T>): Array<JsonSchemaProperty<T>> {\n return Object.values(getProperties<T>(obj));\n}\n\n/**\n * Adds a property to a JSON Schema object form.\n *\n * @remarks\n * This function modifies the provided schema in place by adding a new property with the specified name and schema. It also updates the `required` array based on the `optional` flag of the property. If the property is marked as optional, it will be removed from the `required` array; otherwise, it will be added to it.\n *\n * @param obj - The JSON Schema object form or a Schema wrapper to which the property should be added.\n * @param name - The name of the property to add.\n * @param property - The JSON Schema fragment representing the property's schema, including metadata.\n * @throws Will throw an error if the provided schema is not an object form.\n */\nexport function addProperty<\n T extends Record<string, any> = Record<string, any>\n>(\n obj: Schema<T> | JsonSchema<T>,\n name: string,\n property: JsonSchemaProperty<T>\n): void {\n const schema = (isSchema<T>(obj) ? obj.schema : obj) as JsonSchemaObject<T>;\n if (!isJsonSchemaObject<T>(schema)) {\n throw new Error(\"Cannot add property to non-object schema\");\n }\n\n schema.properties ??= {};\n schema.required ??= [];\n\n schema.properties[name] = { ...property, name };\n if (property?.optional) {\n schema.required = schema.required.filter(key => key !== name);\n } else if (!schema.required.includes(name)) {\n schema.required.push(name);\n }\n\n if (schema.required.length === 0) {\n delete schema.required;\n }\n}\n\n/**\n * Merges multiple JSON Schema object forms into one.\n *\n * @remarks\n * This function takes multiple JSON Schema objects or Schema wrappers and merges them into a single JSON Schema object. The merging process combines properties and metadata from all provided schemas, with later schemas in the arguments list taking precedence over earlier ones in case of conflicts. The resulting schema will include all unique properties and metadata from the input schemas.\n *\n * @param schemas - An array of JSON Schema objects or Schema wrappers to merge.\n * @returns A new JSON Schema object that is the result of merging all input schemas.\n */\nexport function mergeSchemas<\n T extends Record<string, any> = Record<string, any>\n>(...schemas: (JsonSchema<T> | Schema<T>)[]): JsonSchema<T> {\n const result: JsonSchema<T> = {} as JsonSchema<T>;\n for (const schema of schemas) {\n const jsonSchema: JsonSchema<T> = isSchema<T>(schema)\n ? schema.schema\n : schema;\n if (!isJsonSchemaObject<T>(jsonSchema)) {\n continue;\n }\n\n defu(result, jsonSchema);\n }\n\n return result;\n}\n"],"mappings":";;;;;;;;;;;;;;;AAsCA,SAAgB,cAEd,KAAuE;CACvE,MAAM,aAAoD,CAAC;CAC3D,MAAM,SAAwB,SAAY,GAAG,IAAI,IAAI,SAAS;CAC9D,IAAI,CAAC,mBAAsB,MAAM,KAAK,CAAC,YAAY,OAAO,UAAU,GAClE,OAAO;CAGT,KAAK,MAAM,CAAC,KAAK,UAAU,OAAO,QAAQ,OAAO,UAAU,GACzD,WAAW,OAAO;EAChB,GAAG;EACH,MAAM;EACN,UAAU,iBAAoB,KAAK,KAAK,mBAAmB,QAAQ,GAAG;CACxE;CAGF,OAAO;AACT;;;;;;;;;;AAWA,SAAgB,kBAEd,KAA8D;CAC9D,OAAO,OAAO,OAAO,cAAiB,GAAG,CAAC;AAC5C;;;;;;;;;;;;AAaA,SAAgB,YAGd,KACA,MACA,UACM;CACN,MAAM,SAAU,SAAY,GAAG,IAAI,IAAI,SAAS;CAChD,IAAI,CAAC,mBAAsB,MAAM,GAC/B,MAAM,IAAI,MAAM,0CAA0C;CAG5D,OAAO,eAAe,CAAC;CACvB,OAAO,aAAa,CAAC;CAErB,OAAO,WAAW,QAAQ;EAAE,GAAG;EAAU;CAAK;CAC9C,IAAI,UAAU,UACZ,OAAO,WAAW,OAAO,SAAS,QAAO,QAAO,QAAQ,IAAI;MACvD,IAAI,CAAC,OAAO,SAAS,SAAS,IAAI,GACvC,OAAO,SAAS,KAAK,IAAI;CAG3B,IAAI,OAAO,SAAS,WAAW,GAC7B,OAAO,OAAO;AAElB;;;;;;;;;;AAWA,SAAgB,aAEd,GAAG,SAAuD;CAC1D,MAAM,SAAwB,CAAC;CAC/B,KAAK,MAAM,UAAU,SAAS;EAC5B,MAAM,aAA4B,SAAY,MAAM,IAChD,OAAO,SACP;EACJ,IAAI,CAAC,mBAAsB,UAAU,GACnC;EAGF,OAAK,QAAQ,UAAU;CACzB;CAEA,OAAO;AACT"}
package/dist/index.cjs CHANGED
@@ -1,17 +1,20 @@
1
1
  Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
2
2
  const require_bundle = require('./bundle.cjs');
3
+ const require_constants = require('./constants.cjs');
4
+ const require_metadata = require('./metadata.cjs');
3
5
  const require_type_checks = require('./type-checks.cjs');
4
6
  const require_helpers = require('./helpers.cjs');
5
7
  const require_codegen = require('./codegen.cjs');
6
- const require_constants = require('./constants.cjs');
7
- const require_jtd = require('./jtd.cjs');
8
8
  const require_persistence = require('./persistence.cjs');
9
9
  const require_reflection = require('./reflection.cjs');
10
10
  const require_resolve = require('./resolve.cjs');
11
11
  const require_extract = require('./extract.cjs');
12
12
 
13
- exports.JTDTypes = require_constants.JTDTypes;
13
+ exports.JSON_SCHEMA_DATA_TYPES = require_constants.JSON_SCHEMA_DATA_TYPES;
14
+ exports.JSON_SCHEMA_METADATA_KEYS = require_constants.JSON_SCHEMA_METADATA_KEYS;
15
+ exports.JsonSchemaTypes = require_constants.JsonSchemaTypes;
14
16
  exports.addProperty = require_helpers.addProperty;
17
+ exports.applySchemaMetadata = require_metadata.applySchemaMetadata;
15
18
  exports.bundle = require_bundle.bundle;
16
19
  exports.extract = require_extract.extract;
17
20
  exports.extractHash = require_extract.extractHash;
@@ -25,19 +28,23 @@ exports.extractVariant = require_extract.extractVariant;
25
28
  exports.generateCode = require_codegen.generateCode;
26
29
  exports.getCacheDirectory = require_persistence.getCacheDirectory;
27
30
  exports.getCacheFilePath = require_persistence.getCacheFilePath;
31
+ exports.getPrimarySchemaType = require_metadata.getPrimarySchemaType;
28
32
  exports.getProperties = require_helpers.getProperties;
29
33
  exports.getPropertiesList = require_helpers.getPropertiesList;
30
34
  exports.isExtractedSchema = require_type_checks.isExtractedSchema;
31
- exports.isJTDSchema = require_type_checks.isJTDSchema;
32
- exports.isJTDSchemaObject = require_type_checks.isJTDSchemaObject;
33
- exports.isObjectSchema = require_type_checks.isObjectSchema;
35
+ exports.isJsonSchema = require_type_checks.isJsonSchema;
36
+ exports.isJsonSchemaObject = require_type_checks.isJsonSchemaObject;
37
+ exports.isNullOnlyJsonSchema = require_type_checks.isNullOnlyJsonSchema;
38
+ exports.isNullableSchema = require_codegen.isNullableSchema;
39
+ exports.isPropertyOptional = require_metadata.isPropertyOptional;
34
40
  exports.isSchema = require_type_checks.isSchema;
41
+ exports.isSchemaNullable = require_metadata.isSchemaNullable;
35
42
  exports.isUntypedInput = require_type_checks.isUntypedInput;
36
43
  exports.isUntypedSchema = require_type_checks.isUntypedSchema;
37
- exports.jsonSchemaToJtd = require_jtd.jsonSchemaToJtd;
38
44
  exports.mergeSchemas = require_helpers.mergeSchemas;
39
45
  exports.readSchema = require_persistence.readSchema;
40
46
  exports.readSchemaSafe = require_persistence.readSchemaSafe;
47
+ exports.readSchemaTypes = require_metadata.readSchemaTypes;
41
48
  exports.reflectionToJsonSchema = require_reflection.reflectionToJsonSchema;
42
49
  exports.resolve = require_resolve.resolve;
43
50
  exports.resolveModule = require_resolve.resolveModule;
package/dist/index.d.cts CHANGED
@@ -1,12 +1,12 @@
1
1
  import { BundleOptions, bundle } from "./bundle.cjs";
2
- import { BaseSchemaSource, ExtractedSchema, JTDNumberType, JTDSchemaObjectForm, JTDSchemaObjectType, JTDSchemaSchemaSource, JTDSchemaType, JTDStringType, JTDType, JsonSchemaLike, JsonSchemaSchemaSource, ObjectSchema, ReflectionSchemaSource, Schema, SchemaInput, SchemaInputVariant, SchemaMetadata, SchemaSource, SchemaSourceInput, SchemaSourceVariant, StandardSchemaSchemaSource, TypeDefinitionReference, UntypedInputObject, UntypedSchema, UntypedSchemaSource, Zod3SchemaSource } from "./types.cjs";
3
- import { generateCode, stringifyType, stringifyValue } from "./codegen.cjs";
4
- import { JTDTypes } from "./constants.cjs";
2
+ import { BaseSchemaSource, ExtractedSchema, JsonSchema, JsonSchemaLike, JsonSchemaObject, JsonSchemaPrimitiveType, JsonSchemaProperty, JsonSchemaSchemaSource, JsonType, NumberFormat, NumberKeywords, ReflectionSchemaSource, Schema, SchemaInput, SchemaInputVariant, SchemaMetadata, SchemaSource, SchemaSourceInput, SchemaSourceVariant, StandardSchemaSchemaSource, StringKeywords, TypeDefinitionReference, UntypedInputObject, UntypedSchema, UntypedSchemaSource, Zod3SchemaSource } from "./types.cjs";
3
+ import { generateCode, isNullableSchema, stringifyType, stringifyValue } from "./codegen.cjs";
4
+ import { JSON_SCHEMA_DATA_TYPES, JSON_SCHEMA_METADATA_KEYS, JsonSchemaTypes } from "./constants.cjs";
5
5
  import { extract, extractHash, extractJsonSchema, extractReflection, extractResolvedVariant, extractSchema, extractSchemaSchema, extractSource, extractVariant } from "./extract.cjs";
6
6
  import { addProperty, getProperties, getPropertiesList, mergeSchemas } from "./helpers.cjs";
7
- import { jsonSchemaToJtd } from "./jtd.cjs";
7
+ import { applySchemaMetadata, getPrimarySchemaType, isPropertyOptional, isSchemaNullable, readSchemaTypes } from "./metadata.cjs";
8
8
  import { getCacheDirectory, getCacheFilePath, readSchema, readSchemaSafe, writeSchema } from "./persistence.cjs";
9
9
  import { reflectionToJsonSchema } from "./reflection.cjs";
10
10
  import { resolve, resolveModule, resolveReflection } from "./resolve.cjs";
11
- import { isExtractedSchema, isJTDSchema, isJTDSchemaObject, isObjectSchema, isSchema, isUntypedInput, isUntypedSchema } from "./type-checks.cjs";
12
- export { BaseSchemaSource, BundleOptions, ExtractedSchema, JTDNumberType, JTDSchemaObjectForm, JTDSchemaObjectType, JTDSchemaSchemaSource, JTDSchemaType, JTDStringType, JTDType, JTDTypes, JsonSchemaLike, JsonSchemaSchemaSource, ObjectSchema, ReflectionSchemaSource, Schema, SchemaInput, SchemaInputVariant, SchemaMetadata, SchemaSource, SchemaSourceInput, SchemaSourceVariant, StandardSchemaSchemaSource, TypeDefinitionReference, UntypedInputObject, UntypedSchema, UntypedSchemaSource, Zod3SchemaSource, addProperty, bundle, extract, extractHash, extractJsonSchema, extractReflection, extractResolvedVariant, extractSchema, extractSchemaSchema, extractSource, extractVariant, generateCode, getCacheDirectory, getCacheFilePath, getProperties, getPropertiesList, isExtractedSchema, isJTDSchema, isJTDSchemaObject, isObjectSchema, isSchema, isUntypedInput, isUntypedSchema, jsonSchemaToJtd, mergeSchemas, readSchema, readSchemaSafe, reflectionToJsonSchema, resolve, resolveModule, resolveReflection, stringifyType, stringifyValue, writeSchema };
11
+ import { isExtractedSchema, isJsonSchema, isJsonSchemaObject, isNullOnlyJsonSchema, isSchema, isUntypedInput, isUntypedSchema } from "./type-checks.cjs";
12
+ export { BaseSchemaSource, BundleOptions, ExtractedSchema, JSON_SCHEMA_DATA_TYPES, JSON_SCHEMA_METADATA_KEYS, JsonSchema, JsonSchemaLike, JsonSchemaObject, JsonSchemaPrimitiveType, JsonSchemaProperty, JsonSchemaSchemaSource, JsonSchemaTypes, JsonType, NumberFormat, NumberKeywords, ReflectionSchemaSource, Schema, SchemaInput, SchemaInputVariant, SchemaMetadata, SchemaSource, SchemaSourceInput, SchemaSourceVariant, StandardSchemaSchemaSource, StringKeywords, TypeDefinitionReference, UntypedInputObject, UntypedSchema, UntypedSchemaSource, Zod3SchemaSource, addProperty, applySchemaMetadata, bundle, extract, extractHash, extractJsonSchema, extractReflection, extractResolvedVariant, extractSchema, extractSchemaSchema, extractSource, extractVariant, generateCode, getCacheDirectory, getCacheFilePath, getPrimarySchemaType, getProperties, getPropertiesList, isExtractedSchema, isJsonSchema, isJsonSchemaObject, isNullOnlyJsonSchema, isNullableSchema, isPropertyOptional, isSchema, isSchemaNullable, isUntypedInput, isUntypedSchema, mergeSchemas, readSchema, readSchemaSafe, readSchemaTypes, reflectionToJsonSchema, resolve, resolveModule, resolveReflection, stringifyType, stringifyValue, writeSchema };
package/dist/index.d.mts CHANGED
@@ -1,12 +1,12 @@
1
1
  import { BundleOptions, bundle } from "./bundle.mjs";
2
- import { BaseSchemaSource, ExtractedSchema, JTDNumberType, JTDSchemaObjectForm, JTDSchemaObjectType, JTDSchemaSchemaSource, JTDSchemaType, JTDStringType, JTDType, JsonSchemaLike, JsonSchemaSchemaSource, ObjectSchema, ReflectionSchemaSource, Schema, SchemaInput, SchemaInputVariant, SchemaMetadata, SchemaSource, SchemaSourceInput, SchemaSourceVariant, StandardSchemaSchemaSource, TypeDefinitionReference, UntypedInputObject, UntypedSchema, UntypedSchemaSource, Zod3SchemaSource } from "./types.mjs";
3
- import { generateCode, stringifyType, stringifyValue } from "./codegen.mjs";
4
- import { JTDTypes } from "./constants.mjs";
2
+ import { BaseSchemaSource, ExtractedSchema, JsonSchema, JsonSchemaLike, JsonSchemaObject, JsonSchemaPrimitiveType, JsonSchemaProperty, JsonSchemaSchemaSource, JsonType, NumberFormat, NumberKeywords, ReflectionSchemaSource, Schema, SchemaInput, SchemaInputVariant, SchemaMetadata, SchemaSource, SchemaSourceInput, SchemaSourceVariant, StandardSchemaSchemaSource, StringKeywords, TypeDefinitionReference, UntypedInputObject, UntypedSchema, UntypedSchemaSource, Zod3SchemaSource } from "./types.mjs";
3
+ import { generateCode, isNullableSchema, stringifyType, stringifyValue } from "./codegen.mjs";
4
+ import { JSON_SCHEMA_DATA_TYPES, JSON_SCHEMA_METADATA_KEYS, JsonSchemaTypes } from "./constants.mjs";
5
5
  import { extract, extractHash, extractJsonSchema, extractReflection, extractResolvedVariant, extractSchema, extractSchemaSchema, extractSource, extractVariant } from "./extract.mjs";
6
6
  import { addProperty, getProperties, getPropertiesList, mergeSchemas } from "./helpers.mjs";
7
- import { jsonSchemaToJtd } from "./jtd.mjs";
7
+ import { applySchemaMetadata, getPrimarySchemaType, isPropertyOptional, isSchemaNullable, readSchemaTypes } from "./metadata.mjs";
8
8
  import { getCacheDirectory, getCacheFilePath, readSchema, readSchemaSafe, writeSchema } from "./persistence.mjs";
9
9
  import { reflectionToJsonSchema } from "./reflection.mjs";
10
10
  import { resolve, resolveModule, resolveReflection } from "./resolve.mjs";
11
- import { isExtractedSchema, isJTDSchema, isJTDSchemaObject, isObjectSchema, isSchema, isUntypedInput, isUntypedSchema } from "./type-checks.mjs";
12
- export { BaseSchemaSource, BundleOptions, ExtractedSchema, JTDNumberType, JTDSchemaObjectForm, JTDSchemaObjectType, JTDSchemaSchemaSource, JTDSchemaType, JTDStringType, JTDType, JTDTypes, JsonSchemaLike, JsonSchemaSchemaSource, ObjectSchema, ReflectionSchemaSource, Schema, SchemaInput, SchemaInputVariant, SchemaMetadata, SchemaSource, SchemaSourceInput, SchemaSourceVariant, StandardSchemaSchemaSource, TypeDefinitionReference, UntypedInputObject, UntypedSchema, UntypedSchemaSource, Zod3SchemaSource, addProperty, bundle, extract, extractHash, extractJsonSchema, extractReflection, extractResolvedVariant, extractSchema, extractSchemaSchema, extractSource, extractVariant, generateCode, getCacheDirectory, getCacheFilePath, getProperties, getPropertiesList, isExtractedSchema, isJTDSchema, isJTDSchemaObject, isObjectSchema, isSchema, isUntypedInput, isUntypedSchema, jsonSchemaToJtd, mergeSchemas, readSchema, readSchemaSafe, reflectionToJsonSchema, resolve, resolveModule, resolveReflection, stringifyType, stringifyValue, writeSchema };
11
+ import { isExtractedSchema, isJsonSchema, isJsonSchemaObject, isNullOnlyJsonSchema, isSchema, isUntypedInput, isUntypedSchema } from "./type-checks.mjs";
12
+ export { BaseSchemaSource, BundleOptions, ExtractedSchema, JSON_SCHEMA_DATA_TYPES, JSON_SCHEMA_METADATA_KEYS, JsonSchema, JsonSchemaLike, JsonSchemaObject, JsonSchemaPrimitiveType, JsonSchemaProperty, JsonSchemaSchemaSource, JsonSchemaTypes, JsonType, NumberFormat, NumberKeywords, ReflectionSchemaSource, Schema, SchemaInput, SchemaInputVariant, SchemaMetadata, SchemaSource, SchemaSourceInput, SchemaSourceVariant, StandardSchemaSchemaSource, StringKeywords, TypeDefinitionReference, UntypedInputObject, UntypedSchema, UntypedSchemaSource, Zod3SchemaSource, addProperty, applySchemaMetadata, bundle, extract, extractHash, extractJsonSchema, extractReflection, extractResolvedVariant, extractSchema, extractSchemaSchema, extractSource, extractVariant, generateCode, getCacheDirectory, getCacheFilePath, getPrimarySchemaType, getProperties, getPropertiesList, isExtractedSchema, isJsonSchema, isJsonSchemaObject, isNullOnlyJsonSchema, isNullableSchema, isPropertyOptional, isSchema, isSchemaNullable, isUntypedInput, isUntypedSchema, mergeSchemas, readSchema, readSchemaSafe, readSchemaTypes, reflectionToJsonSchema, resolve, resolveModule, resolveReflection, stringifyType, stringifyValue, writeSchema };
package/dist/index.mjs CHANGED
@@ -1,12 +1,12 @@
1
1
  import { bundle } from "./bundle.mjs";
2
- import { isExtractedSchema, isJTDSchema, isJTDSchemaObject, isObjectSchema, isSchema, isUntypedInput, isUntypedSchema } from "./type-checks.mjs";
2
+ import { JSON_SCHEMA_DATA_TYPES, JSON_SCHEMA_METADATA_KEYS, JsonSchemaTypes } from "./constants.mjs";
3
+ import { applySchemaMetadata, getPrimarySchemaType, isPropertyOptional, isSchemaNullable, readSchemaTypes } from "./metadata.mjs";
4
+ import { isExtractedSchema, isJsonSchema, isJsonSchemaObject, isNullOnlyJsonSchema, isSchema, isUntypedInput, isUntypedSchema } from "./type-checks.mjs";
3
5
  import { addProperty, getProperties, getPropertiesList, mergeSchemas } from "./helpers.mjs";
4
- import { generateCode, stringifyType, stringifyValue } from "./codegen.mjs";
5
- import { JTDTypes } from "./constants.mjs";
6
- import { jsonSchemaToJtd } from "./jtd.mjs";
6
+ import { generateCode, isNullableSchema, stringifyType, stringifyValue } from "./codegen.mjs";
7
7
  import { getCacheDirectory, getCacheFilePath, readSchema, readSchemaSafe, writeSchema } from "./persistence.mjs";
8
8
  import { reflectionToJsonSchema } from "./reflection.mjs";
9
9
  import { resolve, resolveModule, resolveReflection } from "./resolve.mjs";
10
10
  import { extract, extractHash, extractJsonSchema, extractReflection, extractResolvedVariant, extractSchema, extractSchemaSchema, extractSource, extractVariant } from "./extract.mjs";
11
11
 
12
- export { JTDTypes, addProperty, bundle, extract, extractHash, extractJsonSchema, extractReflection, extractResolvedVariant, extractSchema, extractSchemaSchema, extractSource, extractVariant, generateCode, getCacheDirectory, getCacheFilePath, getProperties, getPropertiesList, isExtractedSchema, isJTDSchema, isJTDSchemaObject, isObjectSchema, isSchema, isUntypedInput, isUntypedSchema, jsonSchemaToJtd, mergeSchemas, readSchema, readSchemaSafe, reflectionToJsonSchema, resolve, resolveModule, resolveReflection, stringifyType, stringifyValue, writeSchema };
12
+ export { JSON_SCHEMA_DATA_TYPES, JSON_SCHEMA_METADATA_KEYS, JsonSchemaTypes, addProperty, applySchemaMetadata, bundle, extract, extractHash, extractJsonSchema, extractReflection, extractResolvedVariant, extractSchema, extractSchemaSchema, extractSource, extractVariant, generateCode, getCacheDirectory, getCacheFilePath, getPrimarySchemaType, getProperties, getPropertiesList, isExtractedSchema, isJsonSchema, isJsonSchemaObject, isNullOnlyJsonSchema, isNullableSchema, isPropertyOptional, isSchema, isSchemaNullable, isUntypedInput, isUntypedSchema, mergeSchemas, readSchema, readSchemaSafe, readSchemaTypes, reflectionToJsonSchema, resolve, resolveModule, resolveReflection, stringifyType, stringifyValue, writeSchema };
@@ -0,0 +1,80 @@
1
+ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
2
+ const require_constants = require('./constants.cjs');
3
+ let _stryke_type_checks = require("@stryke/type-checks");
4
+ let _stryke_type_checks_is_set_object = require("@stryke/type-checks/is-set-object");
5
+
6
+ //#region src/metadata.ts
7
+ /**
8
+ * Applies Powerlines schema metadata onto a JSON Schema fragment.
9
+ *
10
+ * @param schema - The JSON Schema fragment to apply metadata to.
11
+ * @param metadata - The Powerlines schema metadata to apply.
12
+ * @returns A new JSON Schema fragment with the metadata applied.
13
+ */
14
+ function applySchemaMetadata(schema, metadata) {
15
+ if (!metadata) return schema;
16
+ const result = { ...schema };
17
+ for (const key of require_constants.JSON_SCHEMA_METADATA_KEYS) {
18
+ const value = metadata[key];
19
+ if (value !== void 0 && value !== null) result[key] = value;
20
+ }
21
+ return result;
22
+ }
23
+ /**
24
+ * Returns whether a JSON Schema fragment accepts `null`.
25
+ *
26
+ * @remarks
27
+ * This is true if the schema has `nullable: true` or if its `type` includes `"null"`.
28
+ *
29
+ * @param schema - The JSON Schema fragment to check.
30
+ * @returns `true` if the schema accepts `null`, otherwise `false`.
31
+ */
32
+ function isSchemaNullable(schema) {
33
+ if (!(0, _stryke_type_checks_is_set_object.isSetObject)(schema)) return false;
34
+ if (schema.nullable === true) return true;
35
+ return readSchemaTypes(schema).includes("null");
36
+ }
37
+ /**
38
+ * Returns whether an object property is optional (not listed in `required`).
39
+ *
40
+ * @remarks
41
+ * In JSON Schema, object properties are optional by default unless they are listed in the `required` array of the parent schema. This function checks whether a given property name is not included in the `required` array of its parent schema, indicating that it is optional.
42
+ *
43
+ * @param parent - The parent JSON Schema object containing the property.
44
+ * @param propertyName - The name of the property to check for optionality.
45
+ * @returns `true` if the property is optional, otherwise `false`.
46
+ */
47
+ function isPropertyOptional(parent, propertyName) {
48
+ return !(parent.required ?? []).includes(propertyName);
49
+ }
50
+ /**
51
+ * Normalizes the JSON Schema `type` keyword to a string array.
52
+ *
53
+ * @remarks
54
+ * This function ensures that the `type` keyword of a JSON Schema fragment is always represented as an array of strings, even if it was originally defined as a single string. This normalization simplifies type checking and processing of JSON Schemas by providing a consistent format for the `type` information.
55
+ *
56
+ * @param schema - The JSON Schema fragment to read types from.
57
+ * @returns An array of JSON Schema primitive type names defined in the `type` keyword, or an empty array if no valid types are found.
58
+ */
59
+ function readSchemaTypes(schema) {
60
+ if (Array.isArray(schema?.type)) return schema.type.filter(_stryke_type_checks.isSetString);
61
+ if ((0, _stryke_type_checks.isSetString)(schema?.type)) return [schema.type];
62
+ return [];
63
+ }
64
+ /**
65
+ * Returns the primary non-null JSON Schema type name for a fragment.
66
+ *
67
+ * @param schema - The JSON Schema fragment to check.
68
+ * @returns The primary non-null JSON Schema type name, or `undefined` if none is found.
69
+ */
70
+ function getPrimarySchemaType(schema) {
71
+ if (!(0, _stryke_type_checks_is_set_object.isSetObject)(schema)) return;
72
+ return readSchemaTypes(schema).find((type) => type !== "null");
73
+ }
74
+
75
+ //#endregion
76
+ exports.applySchemaMetadata = applySchemaMetadata;
77
+ exports.getPrimarySchemaType = getPrimarySchemaType;
78
+ exports.isPropertyOptional = isPropertyOptional;
79
+ exports.isSchemaNullable = isSchemaNullable;
80
+ exports.readSchemaTypes = readSchemaTypes;