@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.
- package/dist/bundle.cjs +34 -20
- package/dist/bundle.d.cts +3 -3
- package/dist/bundle.d.cts.map +1 -1
- package/dist/bundle.d.mts +3 -3
- package/dist/bundle.d.mts.map +1 -1
- package/dist/bundle.mjs +35 -21
- package/dist/bundle.mjs.map +1 -1
- package/dist/codegen.cjs +40 -36
- package/dist/codegen.d.cts +10 -22
- package/dist/codegen.d.cts.map +1 -1
- package/dist/codegen.d.mts +10 -22
- package/dist/codegen.d.mts.map +1 -1
- package/dist/codegen.mjs +39 -36
- package/dist/codegen.mjs.map +1 -1
- package/dist/constants.cjs +34 -11
- package/dist/constants.d.cts +9 -11
- package/dist/constants.d.cts.map +1 -1
- package/dist/constants.d.mts +9 -11
- package/dist/constants.d.mts.map +1 -1
- package/dist/constants.mjs +32 -11
- package/dist/constants.mjs.map +1 -1
- package/dist/extract.cjs +122 -92
- package/dist/extract.d.cts +34 -69
- package/dist/extract.d.cts.map +1 -1
- package/dist/extract.d.mts +34 -69
- package/dist/extract.d.mts.map +1 -1
- package/dist/extract.mjs +123 -93
- package/dist/extract.mjs.map +1 -1
- package/dist/helpers.cjs +45 -52
- package/dist/helpers.d.cts +28 -24
- package/dist/helpers.d.cts.map +1 -1
- package/dist/helpers.d.mts +28 -24
- package/dist/helpers.d.mts.map +1 -1
- package/dist/helpers.mjs +46 -52
- package/dist/helpers.mjs.map +1 -1
- package/dist/index.cjs +14 -7
- package/dist/index.d.cts +6 -6
- package/dist/index.d.mts +6 -6
- package/dist/index.mjs +5 -5
- package/dist/metadata.cjs +80 -0
- package/dist/metadata.d.cts +52 -0
- package/dist/metadata.d.cts.map +1 -0
- package/dist/metadata.d.mts +52 -0
- package/dist/metadata.d.mts.map +1 -0
- package/dist/metadata.mjs +76 -0
- package/dist/metadata.mjs.map +1 -0
- package/dist/persistence.cjs +1 -2
- package/dist/persistence.d.cts +5 -5
- package/dist/persistence.d.cts.map +1 -1
- package/dist/persistence.d.mts +5 -5
- package/dist/persistence.d.mts.map +1 -1
- package/dist/persistence.mjs +1 -1
- package/dist/persistence.mjs.map +1 -1
- package/dist/reflection.cjs +289 -303
- package/dist/reflection.d.cts +3 -16
- package/dist/reflection.d.cts.map +1 -1
- package/dist/reflection.d.mts +3 -16
- package/dist/reflection.d.mts.map +1 -1
- package/dist/reflection.mjs +290 -304
- package/dist/reflection.mjs.map +1 -1
- package/dist/resolve.cjs +7 -7
- package/dist/resolve.mjs +7 -7
- package/dist/resolve.mjs.map +1 -1
- package/dist/type-checks.cjs +122 -40
- package/dist/type-checks.d.cts +41 -33
- package/dist/type-checks.d.cts.map +1 -1
- package/dist/type-checks.d.mts +41 -33
- package/dist/type-checks.d.mts.map +1 -1
- package/dist/type-checks.mjs +120 -37
- package/dist/type-checks.mjs.map +1 -1
- package/dist/types.d.cts +225 -113
- package/dist/types.d.cts.map +1 -1
- package/dist/types.d.mts +225 -113
- package/dist/types.d.mts.map +1 -1
- package/package.json +11 -7
- package/dist/jtd.cjs +0 -385
- package/dist/jtd.d.cts +0 -15
- package/dist/jtd.d.cts.map +0 -1
- package/dist/jtd.d.mts +0 -15
- package/dist/jtd.d.mts.map +0 -1
- package/dist/jtd.mjs +0 -384
- package/dist/jtd.mjs.map +0 -1
package/dist/helpers.d.cts
CHANGED
|
@@ -1,44 +1,48 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { JsonSchema, JsonSchemaProperty, Schema } from "./types.cjs";
|
|
2
2
|
|
|
3
3
|
//#region src/helpers.d.ts
|
|
4
4
|
/**
|
|
5
|
-
*
|
|
5
|
+
* Extracts object properties from a JSON Schema object form.
|
|
6
6
|
*
|
|
7
|
-
* @
|
|
8
|
-
*
|
|
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<
|
|
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
|
-
*
|
|
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
|
|
18
|
-
* @returns An array of
|
|
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<
|
|
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
|
-
*
|
|
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
|
|
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
|
|
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<
|
|
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
|
|
37
|
+
* Merges multiple JSON Schema object forms into one.
|
|
34
38
|
*
|
|
35
39
|
* @remarks
|
|
36
|
-
* This function
|
|
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
|
|
39
|
-
* @returns A
|
|
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<
|
|
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
|
package/dist/helpers.d.cts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"helpers.d.cts","names":[],"sources":["../src/helpers.ts"],"mappings":";;;;;
|
|
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"}
|
package/dist/helpers.d.mts
CHANGED
|
@@ -1,44 +1,48 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { JsonSchema, JsonSchemaProperty, Schema } from "./types.mjs";
|
|
2
2
|
|
|
3
3
|
//#region src/helpers.d.ts
|
|
4
4
|
/**
|
|
5
|
-
*
|
|
5
|
+
* Extracts object properties from a JSON Schema object form.
|
|
6
6
|
*
|
|
7
|
-
* @
|
|
8
|
-
*
|
|
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<
|
|
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
|
-
*
|
|
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
|
|
18
|
-
* @returns An array of
|
|
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<
|
|
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
|
-
*
|
|
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
|
|
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
|
|
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<
|
|
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
|
|
37
|
+
* Merges multiple JSON Schema object forms into one.
|
|
34
38
|
*
|
|
35
39
|
* @remarks
|
|
36
|
-
* This function
|
|
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
|
|
39
|
-
* @returns A
|
|
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<
|
|
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
|
package/dist/helpers.d.mts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"helpers.d.mts","names":[],"sources":["../src/helpers.ts"],"mappings":";;;;;
|
|
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 {
|
|
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
|
-
*
|
|
8
|
+
* Extracts object properties from a JSON Schema object form.
|
|
8
9
|
*
|
|
9
|
-
* @
|
|
10
|
-
*
|
|
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 =
|
|
15
|
-
if (
|
|
16
|
-
|
|
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
|
-
|
|
23
|
+
nullable: isSchemaNullable(value) || isPropertyOptional(schema, key)
|
|
24
24
|
};
|
|
25
25
|
return properties;
|
|
26
26
|
}
|
|
27
27
|
/**
|
|
28
|
-
*
|
|
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
|
|
31
|
-
* @returns An array of
|
|
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
|
-
*
|
|
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
|
|
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
|
|
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 =
|
|
45
|
-
if (property
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
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
|
|
64
|
+
* Merges multiple JSON Schema object forms into one.
|
|
67
65
|
*
|
|
68
66
|
* @remarks
|
|
69
|
-
* This function
|
|
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
|
|
72
|
-
* @returns A
|
|
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
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
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
|
|
79
|
+
return result;
|
|
86
80
|
}
|
|
87
81
|
|
|
88
82
|
//#endregion
|
package/dist/helpers.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"helpers.mjs","names":[
|
|
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.
|
|
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.
|
|
32
|
-
exports.
|
|
33
|
-
exports.
|
|
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,
|
|
3
|
-
import { generateCode, stringifyType, stringifyValue } from "./codegen.cjs";
|
|
4
|
-
import {
|
|
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 {
|
|
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,
|
|
12
|
-
export { BaseSchemaSource, BundleOptions, ExtractedSchema,
|
|
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,
|
|
3
|
-
import { generateCode, stringifyType, stringifyValue } from "./codegen.mjs";
|
|
4
|
-
import {
|
|
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 {
|
|
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,
|
|
12
|
-
export { BaseSchemaSource, BundleOptions, ExtractedSchema,
|
|
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 {
|
|
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 {
|
|
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;
|