@takeshape/schema 11.128.1 → 11.131.1

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/index.d.ts CHANGED
@@ -42,6 +42,7 @@ export * from './types/utils.ts';
42
42
  export * from './unions.ts';
43
43
  export * from './util/ai-tools.ts';
44
44
  export * from './util/api-indexing.ts';
45
+ export * from './util/coerce-value.ts';
45
46
  export * from './util/detect-cycles.ts';
46
47
  export * from './util/expressions.ts';
47
48
  export * from './util/find-shape-at-path.ts';
package/dist/index.js CHANGED
@@ -39,6 +39,7 @@ export * from "./types/utils.js";
39
39
  export * from "./unions.js";
40
40
  export * from "./util/ai-tools.js";
41
41
  export * from "./util/api-indexing.js";
42
+ export * from "./util/coerce-value.js";
42
43
  export * from "./util/detect-cycles.js";
43
44
  export * from "./util/expressions.js";
44
45
  export * from "./util/find-shape-at-path.js";
@@ -0,0 +1,9 @@
1
+ import type { PropertySchema } from '../project-schema/index.js';
2
+ /**
3
+ * Coerces a value to match the type specified in a PropertySchema only supporting primitive types
4
+ * @param schema - The PropertySchema object defining the expected type
5
+ * @param value - The value to coerce
6
+ * @returns The coerced value
7
+ * @throws Error if coercion fails or type is not supported
8
+ */
9
+ export declare function coercePrimitiveValue(schema: PropertySchema, value: unknown): any;
@@ -0,0 +1,32 @@
1
+ import { coerceToBoolean, coerceToInteger, coerceToNumber, coerceToString } from '@takeshape/util';
2
+ /**
3
+ * Coerces a value to match the type specified in a PropertySchema only supporting primitive types
4
+ * @param schema - The PropertySchema object defining the expected type
5
+ * @param value - The value to coerce
6
+ * @returns The coerced value
7
+ * @throws Error if coercion fails or type is not supported
8
+ */
9
+ export function coercePrimitiveValue(schema, value) {
10
+ // Handle undefined/null values
11
+ if (value === undefined) {
12
+ if (schema.default !== undefined) {
13
+ return schema.default;
14
+ }
15
+ }
16
+ const primaryType = Array.isArray(schema.type) ? schema.type[0] : schema.type;
17
+ switch (primaryType) {
18
+ case 'string':
19
+ return coerceToString(value);
20
+ case 'number':
21
+ return coerceToNumber(value);
22
+ case 'integer':
23
+ return coerceToInteger(value);
24
+ case 'boolean':
25
+ return coerceToBoolean(value);
26
+ case 'null':
27
+ return null;
28
+ default:
29
+ // If no type specified, return as-is
30
+ return value;
31
+ }
32
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@takeshape/schema",
3
- "version": "11.128.1",
3
+ "version": "11.131.1",
4
4
  "description": "TakeShape Schema",
5
5
  "homepage": "https://www.takeshape.io",
6
6
  "repository": {
@@ -56,9 +56,9 @@
56
56
  "p-reduce": "2.1.0",
57
57
  "semver": "7.7.2",
58
58
  "tiny-invariant": "1.3.3",
59
- "@takeshape/json-schema": "11.128.1",
60
- "@takeshape/errors": "11.128.1",
61
- "@takeshape/util": "11.128.1"
59
+ "@takeshape/errors": "11.131.1",
60
+ "@takeshape/json-schema": "11.131.1",
61
+ "@takeshape/util": "11.131.1"
62
62
  },
63
63
  "devDependencies": {
64
64
  "@takeshape/json-schema-to-typescript": "11.0.0",
@@ -75,8 +75,8 @@
75
75
  "glob": "7.2.3",
76
76
  "json-schema-to-ts": "3.1.1",
77
77
  "shortid": "2.2.16",
78
- "@takeshape/infra": "11.128.1",
79
- "@takeshape/logger": "11.128.1"
78
+ "@takeshape/infra": "11.131.1",
79
+ "@takeshape/logger": "11.131.1"
80
80
  },
81
81
  "engines": {
82
82
  "node": ">=22"