@flare-ts/lib 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (80) hide show
  1. package/LICENSE +21 -0
  2. package/dist/index.d.ts +2 -0
  3. package/dist/index.d.ts.map +1 -0
  4. package/dist/index.js +1 -0
  5. package/dist/schema/index.d.ts +10 -0
  6. package/dist/schema/index.d.ts.map +1 -0
  7. package/dist/schema/index.js +21 -0
  8. package/dist/schema/internal/index.d.ts +4 -0
  9. package/dist/schema/internal/index.d.ts.map +1 -0
  10. package/dist/schema/internal/index.js +8 -0
  11. package/dist/schema/internal/parser/array.d.ts +7 -0
  12. package/dist/schema/internal/parser/array.d.ts.map +1 -0
  13. package/dist/schema/internal/parser/array.js +33 -0
  14. package/dist/schema/internal/parser/discriminated.d.ts +11 -0
  15. package/dist/schema/internal/parser/discriminated.d.ts.map +1 -0
  16. package/dist/schema/internal/parser/discriminated.js +40 -0
  17. package/dist/schema/internal/parser/input.d.ts +26 -0
  18. package/dist/schema/internal/parser/input.d.ts.map +1 -0
  19. package/dist/schema/internal/parser/input.js +56 -0
  20. package/dist/schema/internal/parser/object.d.ts +16 -0
  21. package/dist/schema/internal/parser/object.d.ts.map +1 -0
  22. package/dist/schema/internal/parser/object.js +81 -0
  23. package/dist/schema/internal/parser/path.d.ts +5 -0
  24. package/dist/schema/internal/parser/path.d.ts.map +1 -0
  25. package/dist/schema/internal/parser/path.js +11 -0
  26. package/dist/schema/internal/parser/record.d.ts +8 -0
  27. package/dist/schema/internal/parser/record.d.ts.map +1 -0
  28. package/dist/schema/internal/parser/record.js +44 -0
  29. package/dist/schema/internal/token/optionality.d.ts +6 -0
  30. package/dist/schema/internal/token/optionality.d.ts.map +1 -0
  31. package/dist/schema/internal/token/optionality.js +11 -0
  32. package/dist/schema/internal/token/symbols.d.ts +7 -0
  33. package/dist/schema/internal/token/symbols.d.ts.map +1 -0
  34. package/dist/schema/internal/token/symbols.js +8 -0
  35. package/dist/schema/internal/types/inference.d.ts +20 -0
  36. package/dist/schema/internal/types/inference.d.ts.map +1 -0
  37. package/dist/schema/internal/types/inference.js +0 -0
  38. package/dist/schema/json/serializer.d.ts +51 -0
  39. package/dist/schema/json/serializer.d.ts.map +1 -0
  40. package/dist/schema/json/serializer.js +265 -0
  41. package/dist/schema/model.d.ts +54 -0
  42. package/dist/schema/model.d.ts.map +1 -0
  43. package/dist/schema/model.js +41 -0
  44. package/dist/schema/primitives/array.d.ts +14 -0
  45. package/dist/schema/primitives/array.d.ts.map +1 -0
  46. package/dist/schema/primitives/array.js +21 -0
  47. package/dist/schema/primitives/bool.d.ts +7 -0
  48. package/dist/schema/primitives/bool.d.ts.map +1 -0
  49. package/dist/schema/primitives/bool.js +12 -0
  50. package/dist/schema/primitives/date.d.ts +23 -0
  51. package/dist/schema/primitives/date.d.ts.map +1 -0
  52. package/dist/schema/primitives/date.js +92 -0
  53. package/dist/schema/primitives/enum.d.ts +19 -0
  54. package/dist/schema/primitives/enum.d.ts.map +1 -0
  55. package/dist/schema/primitives/enum.js +19 -0
  56. package/dist/schema/primitives/float.d.ts +23 -0
  57. package/dist/schema/primitives/float.d.ts.map +1 -0
  58. package/dist/schema/primitives/float.js +33 -0
  59. package/dist/schema/primitives/index.d.ts +89 -0
  60. package/dist/schema/primitives/index.d.ts.map +1 -0
  61. package/dist/schema/primitives/index.js +44 -0
  62. package/dist/schema/primitives/int.d.ts +24 -0
  63. package/dist/schema/primitives/int.d.ts.map +1 -0
  64. package/dist/schema/primitives/int.js +43 -0
  65. package/dist/schema/primitives/str.d.ts +26 -0
  66. package/dist/schema/primitives/str.d.ts.map +1 -0
  67. package/dist/schema/primitives/str.js +30 -0
  68. package/dist/schema/primitives/text.d.ts +33 -0
  69. package/dist/schema/primitives/text.d.ts.map +1 -0
  70. package/dist/schema/primitives/text.js +30 -0
  71. package/dist/schema/primitives/uuid.d.ts +7 -0
  72. package/dist/schema/primitives/uuid.d.ts.map +1 -0
  73. package/dist/schema/primitives/uuid.js +13 -0
  74. package/dist/schema/schema.d.ts +167 -0
  75. package/dist/schema/schema.d.ts.map +1 -0
  76. package/dist/schema/schema.js +74 -0
  77. package/dist/schema/symbol.d.ts +15 -0
  78. package/dist/schema/symbol.d.ts.map +1 -0
  79. package/dist/schema/symbol.js +6 -0
  80. package/package.json +49 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Lumex Solutions
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,2 @@
1
+ export * from "./schema/index.js";
2
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,mBAAmB,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1 @@
1
+ export * from "./schema/index.js";
@@ -0,0 +1,10 @@
1
+ export { array, bool, date, defaultTo, enums, float, int, optional, str, text, uuid } from "./primitives/index.js";
2
+ export type { ArrayTypedPrimitive, Primitive, PrimitiveJsonSchema, TypedPrimitive } from "./primitives/index.js";
3
+ export { model } from "./model.js";
4
+ export { schema } from "./schema.js";
5
+ export type { ModelTokenBuilder } from "./model.js";
6
+ export type { BranchShape, DescriptorValue, DiscriminantValues, OpaqueSchemaToken, SchemaToken } from "./schema.js";
7
+ export type { FieldError, JsonObject, JsonValue, SafeParseResult, ValidationError } from "./schema.js";
8
+ export { compileSerializer, toJsonSchema } from "./json/serializer.js";
9
+ export type { Serializer } from "./json/serializer.js";
10
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/schema/index.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,KAAK,EAAE,IAAI,EAAE,IAAI,EAAE,SAAS,EAAE,KAAK,EAAE,KAAK,EAAE,GAAG,EAAE,QAAQ,EAAE,GAAG,EAAE,IAAI,EAAE,IAAI,EAAE,MAAM,uBAAuB,CAAC;AAEnH,YAAY,EAAE,mBAAmB,EAAE,SAAS,EAAE,mBAAmB,EAAE,cAAc,EAAE,MAAM,uBAAuB,CAAC;AAEjH,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AACnC,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAErC,YAAY,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAC;AACpD,YAAY,EAAE,WAAW,EAAE,eAAe,EAAE,kBAAkB,EAAE,iBAAiB,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAEpH,YAAY,EAAE,UAAU,EAAE,UAAU,EAAE,SAAS,EAAE,eAAe,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AAEvG,OAAO,EAAE,iBAAiB,EAAE,YAAY,EAAE,MAAM,sBAAsB,CAAC;AACvE,YAAY,EAAE,UAAU,EAAE,MAAM,sBAAsB,CAAC"}
@@ -0,0 +1,21 @@
1
+ import { array, bool, date, defaultTo, enums, float, int, optional, str, text, uuid } from "./primitives/index.js";
2
+ import { model } from "./model.js";
3
+ import { schema } from "./schema.js";
4
+ import { compileSerializer, toJsonSchema } from "./json/serializer.js";
5
+ export {
6
+ array,
7
+ bool,
8
+ compileSerializer,
9
+ date,
10
+ defaultTo,
11
+ enums,
12
+ float,
13
+ int,
14
+ model,
15
+ optional,
16
+ schema,
17
+ str,
18
+ text,
19
+ toJsonSchema,
20
+ uuid
21
+ };
@@ -0,0 +1,4 @@
1
+ export { isSchemaRequired } from "./token/optionality.js";
2
+ export { SCHEMA_BRAND, SCHEMA_DESCRIPTOR, SCHEMA_REQUIRED } from "./token/symbols.js";
3
+ export type { AnyDescriptorValue, InferSchemaShape } from "./types/inference.js";
4
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/schema/internal/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,wBAAwB,CAAC;AAC1D,OAAO,EAAE,YAAY,EAAE,iBAAiB,EAAE,eAAe,EAAE,MAAM,oBAAoB,CAAC;AAEtF,YAAY,EAAE,kBAAkB,EAAE,gBAAgB,EAAE,MAAM,sBAAsB,CAAC"}
@@ -0,0 +1,8 @@
1
+ import { isSchemaRequired } from "./token/optionality.js";
2
+ import { SCHEMA_BRAND, SCHEMA_DESCRIPTOR, SCHEMA_REQUIRED } from "./token/symbols.js";
3
+ export {
4
+ SCHEMA_BRAND,
5
+ SCHEMA_DESCRIPTOR,
6
+ SCHEMA_REQUIRED,
7
+ isSchemaRequired
8
+ };
@@ -0,0 +1,7 @@
1
+ import type { JsonValue, SafeParseResult, SchemaToken } from "../../schema.js";
2
+ /**
3
+ * @internal
4
+ * Core implementation for top-level array schemas.
5
+ */
6
+ export declare function arraySafeParse<T>(raw: ArrayBuffer | string | JsonValue, itemSchema: SchemaToken<T>): SafeParseResult<T[]>;
7
+ //# sourceMappingURL=array.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"array.d.ts","sourceRoot":"","sources":["../../../../src/schema/internal/parser/array.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAc,SAAS,EAAE,eAAe,EAAE,WAAW,EAAE,MAAM,iBAAiB,CAAC;AAI3F;;;GAGG;AACH,wBAAgB,cAAc,CAAC,CAAC,EAC9B,GAAG,EAAE,WAAW,GAAG,MAAM,GAAG,SAAS,EACrC,UAAU,EAAE,WAAW,CAAC,CAAC,CAAC,GACzB,eAAe,CAAC,CAAC,EAAE,CAAC,CA8BtB"}
@@ -0,0 +1,33 @@
1
+ import { resolveArrayInput } from "./input.js";
2
+ import { prefixRootArrayItemPath } from "./path.js";
3
+ function arraySafeParse(raw, itemSchema) {
4
+ try {
5
+ const parsed = resolveArrayInput(raw);
6
+ const result = new Array(parsed.length);
7
+ const errors = [];
8
+ for (let i = 0; i < parsed.length; i++) {
9
+ const nested = itemSchema.safeParse(parsed[i]);
10
+ if (!nested.success) {
11
+ nested.error.fields.forEach((e) => errors.push({ ...e, path: prefixRootArrayItemPath(i, e.path) }));
12
+ continue;
13
+ }
14
+ result[i] = nested.data;
15
+ }
16
+ if (errors.length > 0) return { success: false, error: { fields: errors } };
17
+ return { success: true, data: result };
18
+ } catch (e) {
19
+ return {
20
+ success: false,
21
+ error: {
22
+ fields: [{
23
+ path: "",
24
+ message: `Failed to parse JSON: ${e instanceof Error ? e.message : String(e)}`,
25
+ received: ""
26
+ }]
27
+ }
28
+ };
29
+ }
30
+ }
31
+ export {
32
+ arraySafeParse
33
+ };
@@ -0,0 +1,11 @@
1
+ import type { DescriptorValue, JsonValue, SafeParseResult } from "../../schema.js";
2
+ /**
3
+ * @internal
4
+ * Core implementation for discriminated union schemas.
5
+ */
6
+ export declare function discriminatedSafeParse<T, K extends keyof T>(raw: ArrayBuffer | string | JsonValue, discriminant: K, branches: {
7
+ [key: string]: {
8
+ [field: string]: DescriptorValue<T[keyof T]>;
9
+ };
10
+ }): SafeParseResult<T>;
11
+ //# sourceMappingURL=discriminated.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"discriminated.d.ts","sourceRoot":"","sources":["../../../../src/schema/internal/parser/discriminated.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAc,SAAS,EAAE,eAAe,EAAE,MAAM,iBAAiB,CAAC;AAI/F;;;GAGG;AACH,wBAAgB,sBAAsB,CAAC,CAAC,EAAE,CAAC,SAAS,MAAM,CAAC,EACzD,GAAG,EAAE,WAAW,GAAG,MAAM,GAAG,SAAS,EACrC,YAAY,EAAE,CAAC,EACf,QAAQ,EAAE;IAAE,CAAC,GAAG,EAAE,MAAM,GAAG;QAAE,CAAC,KAAK,EAAE,MAAM,GAAG,eAAe,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC;KAAE,CAAC;CAAE,GAC9E,eAAe,CAAC,CAAC,CAAC,CAoCpB"}
@@ -0,0 +1,40 @@
1
+ import { resolveInput, tryGetValue } from "./input.js";
2
+ import { processField } from "./object.js";
3
+ function discriminatedSafeParse(raw, discriminant, branches) {
4
+ try {
5
+ const parsed = resolveInput(raw);
6
+ const discriminantValue = tryGetValue(parsed, discriminant);
7
+ if (discriminantValue === void 0) {
8
+ return invalidSchemaError(String(discriminant), "Missing or invalid discriminant field", "");
9
+ }
10
+ const branch = branches[discriminantValue];
11
+ if (!branch) {
12
+ return invalidSchemaError(String(discriminant), "Invalid discriminant value", String(discriminantValue));
13
+ }
14
+ const result = {};
15
+ const errors = [];
16
+ result[String(discriminant)] = discriminantValue;
17
+ for (const key in branch) {
18
+ processField(key, branch[key], parsed[key], result, errors);
19
+ }
20
+ if (errors.length > 0) return { success: false, error: { fields: errors } };
21
+ return { success: true, data: result };
22
+ } catch (e) {
23
+ return {
24
+ success: false,
25
+ error: {
26
+ fields: [{
27
+ path: "",
28
+ message: `Failed to parse JSON: ${e instanceof Error ? e.message : String(e)}`,
29
+ received: ""
30
+ }]
31
+ }
32
+ };
33
+ }
34
+ }
35
+ function invalidSchemaError(path, message, received) {
36
+ return { success: false, error: { fields: [{ path, message, received }] } };
37
+ }
38
+ export {
39
+ discriminatedSafeParse
40
+ };
@@ -0,0 +1,26 @@
1
+ import type { JsonValue } from "../../schema.js";
2
+ /**
3
+ * @internal
4
+ * Reads a string or number value from a parsed object by key.
5
+ * Returns `undefined` if the key is absent or its value is not a string or number.
6
+ */
7
+ export declare function tryGetValue(obj: {
8
+ [key: string]: JsonValue;
9
+ }, key: string | number | symbol): string | number | undefined;
10
+ /**
11
+ * @internal
12
+ * Resolves all accepted raw input forms into a plain JSON object.
13
+ * JSON strings and ArrayBuffers are parsed; plain objects are passed through.
14
+ * Throws for any other JsonValue (arrays, primitives).
15
+ */
16
+ export declare function resolveInput(raw: ArrayBuffer | string | JsonValue): {
17
+ [key: string]: JsonValue;
18
+ };
19
+ /**
20
+ * @internal
21
+ * Resolves all accepted raw input forms into a JSON array.
22
+ * JSON strings and ArrayBuffers are parsed; plain arrays are passed through.
23
+ * Throws for any other JsonValue (objects, primitives).
24
+ */
25
+ export declare function resolveArrayInput(raw: ArrayBuffer | string | JsonValue): JsonValue[];
26
+ //# sourceMappingURL=input.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"input.d.ts","sourceRoot":"","sources":["../../../../src/schema/internal/parser/input.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAEjD;;;;GAIG;AACH,wBAAgB,WAAW,CACzB,GAAG,EAAE;IAAE,CAAC,GAAG,EAAE,MAAM,GAAG,SAAS,CAAC;CAAE,EAClC,GAAG,EAAE,MAAM,GAAG,MAAM,GAAG,MAAM,GAC5B,MAAM,GAAG,MAAM,GAAG,SAAS,CAI7B;AAED;;;;;GAKG;AACH,wBAAgB,YAAY,CAAC,GAAG,EAAE,WAAW,GAAG,MAAM,GAAG,SAAS,GAAG;IAAE,CAAC,GAAG,EAAE,MAAM,GAAG,SAAS,CAAC;CAAE,CAQjG;AAED;;;;;GAKG;AACH,wBAAgB,iBAAiB,CAAC,GAAG,EAAE,WAAW,GAAG,MAAM,GAAG,SAAS,GAAG,SAAS,EAAE,CAQpF"}
@@ -0,0 +1,56 @@
1
+ function tryGetValue(obj, key) {
2
+ const value = obj[String(key)];
3
+ if (typeof value !== "string" && typeof value !== "number") return void 0;
4
+ return value;
5
+ }
6
+ function resolveInput(raw) {
7
+ if (raw instanceof ArrayBuffer || typeof raw === "string") {
8
+ return tryParseJSON(raw);
9
+ }
10
+ if (typeof raw === "object" && raw !== null && !Array.isArray(raw)) {
11
+ return raw;
12
+ }
13
+ throw new Error("Expected object");
14
+ }
15
+ function resolveArrayInput(raw) {
16
+ if (raw instanceof ArrayBuffer || typeof raw === "string") {
17
+ return tryParseJSONArray(raw);
18
+ }
19
+ if (Array.isArray(raw)) {
20
+ return raw;
21
+ }
22
+ throw new Error("Expected array");
23
+ }
24
+ function parseJSON(raw) {
25
+ try {
26
+ return JSON.parse(typeof raw === "string" ? raw : new TextDecoder().decode(raw));
27
+ } catch (e) {
28
+ throw new Error(e instanceof Error ? e.message : String(e));
29
+ }
30
+ }
31
+ function tryParseJSON(raw) {
32
+ try {
33
+ const parsed = parseJSON(raw);
34
+ if (typeof parsed !== "object" || parsed === null) {
35
+ throw new Error("Expected object");
36
+ }
37
+ if (Array.isArray(parsed)) {
38
+ throw new Error("Expected object, received array");
39
+ }
40
+ return parsed;
41
+ } catch (e) {
42
+ throw new Error(e instanceof Error ? e.message : String(e));
43
+ }
44
+ }
45
+ function tryParseJSONArray(raw) {
46
+ const parsed = parseJSON(raw);
47
+ if (!Array.isArray(parsed)) {
48
+ throw new Error("Expected array");
49
+ }
50
+ return parsed;
51
+ }
52
+ export {
53
+ resolveArrayInput,
54
+ resolveInput,
55
+ tryGetValue
56
+ };
@@ -0,0 +1,16 @@
1
+ import type { DescriptorValue, FieldError, JsonValue, SafeParseResult } from "../../schema.js";
2
+ /**
3
+ * @internal
4
+ * Validates a single field value against its descriptor entry (either a
5
+ * primitive or a nested schema) and writes the result into `result`, or appends
6
+ * to `errors` on failure.
7
+ */
8
+ export declare function processField<T>(key: string, primitiveOrSchema: DescriptorValue<T[keyof T]>, value: JsonValue | undefined, result: Record<string, T[keyof T]>, errors: FieldError[]): void;
9
+ /**
10
+ * @internal
11
+ * Core implementation for flat object schemas.
12
+ */
13
+ export declare function flatSafeParse<T>(raw: ArrayBuffer | string | JsonValue, descriptor: {
14
+ [K in keyof T]: DescriptorValue<T[K]>;
15
+ }): SafeParseResult<T>;
16
+ //# sourceMappingURL=object.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"object.d.ts","sourceRoot":"","sources":["../../../../src/schema/internal/parser/object.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,UAAU,EAAE,SAAS,EAAE,eAAe,EAAe,MAAM,iBAAiB,CAAC;AAM5G;;;;;GAKG;AACH,wBAAgB,YAAY,CAAC,CAAC,EAC5B,GAAG,EAAE,MAAM,EACX,iBAAiB,EAAE,eAAe,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,EAC9C,KAAK,EAAE,SAAS,GAAG,SAAS,EAC5B,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,EAClC,MAAM,EAAE,UAAU,EAAE,GACnB,IAAI,CAgDN;AAED;;;GAGG;AACH,wBAAgB,aAAa,CAAC,CAAC,EAC7B,GAAG,EAAE,WAAW,GAAG,MAAM,GAAG,SAAS,EACrC,UAAU,EAAE;KAAG,CAAC,IAAI,MAAM,CAAC,GAAG,eAAe,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;CAAG,GACrD,eAAe,CAAC,CAAC,CAAC,CAyBpB"}
@@ -0,0 +1,81 @@
1
+ import { isSchemaRequired } from "../token/optionality.js";
2
+ import { SCHEMA_BRAND } from "../token/symbols.js";
3
+ import { resolveInput } from "./input.js";
4
+ import { prefixNestedPath } from "./path.js";
5
+ function processField(key, primitiveOrSchema, value, result, errors) {
6
+ if (primitiveOrSchema[SCHEMA_BRAND] === true) {
7
+ const schema = primitiveOrSchema;
8
+ const isOptional = !isSchemaRequired(schema);
9
+ if (value === void 0 || value === null) {
10
+ if (!isOptional) {
11
+ errors.push({ path: key, message: "Missing required field", received: value === null ? "null" : "" });
12
+ }
13
+ return;
14
+ }
15
+ const nested = schema.safeParse(value);
16
+ if (!nested.success) {
17
+ nested.error.fields.forEach((e) => errors.push({ ...e, path: prefixNestedPath(key, e.path) }));
18
+ } else {
19
+ result[key] = nested.data;
20
+ }
21
+ } else {
22
+ const primitive = primitiveOrSchema;
23
+ if (value === void 0 || value === null) {
24
+ if (primitive._required) {
25
+ errors.push({ path: key, message: "Missing required field", received: value === null ? "null" : "" });
26
+ } else {
27
+ const fallback = primitive("");
28
+ if (fallback !== void 0) result[key] = fallback;
29
+ }
30
+ return;
31
+ }
32
+ try {
33
+ let parsed;
34
+ if (typeof value === "object") {
35
+ if (!Array.isArray(value)) {
36
+ throw new Error(`Expected primitive value, got object`);
37
+ }
38
+ parsed = primitive(value.map(String));
39
+ } else {
40
+ parsed = primitive(String(value));
41
+ }
42
+ if (parsed !== void 0) {
43
+ result[key] = parsed;
44
+ }
45
+ } catch (e) {
46
+ errors.push({
47
+ path: key,
48
+ message: e instanceof Error ? e.message : "Invalid value",
49
+ received: JSON.stringify(value)
50
+ });
51
+ }
52
+ }
53
+ }
54
+ function flatSafeParse(raw, descriptor) {
55
+ try {
56
+ const parsed = resolveInput(raw);
57
+ const result = {};
58
+ const errors = [];
59
+ for (const key in descriptor) {
60
+ const value = Object.hasOwn(parsed, key) ? parsed[key] : void 0;
61
+ processField(key, descriptor[key], value, result, errors);
62
+ }
63
+ if (errors.length > 0) return { success: false, error: { fields: errors } };
64
+ return { success: true, data: result };
65
+ } catch (e) {
66
+ return {
67
+ success: false,
68
+ error: {
69
+ fields: [{
70
+ path: "",
71
+ message: `Failed to parse JSON: ${e instanceof Error ? e.message : String(e)}`,
72
+ received: ""
73
+ }]
74
+ }
75
+ };
76
+ }
77
+ }
78
+ export {
79
+ flatSafeParse,
80
+ processField
81
+ };
@@ -0,0 +1,5 @@
1
+ /** @internal Prefixes a nested field path with its parent object key. */
2
+ export declare function prefixNestedPath(key: string, path: string): string;
3
+ /** @internal Prefixes a nested field path with a top-level array index. */
4
+ export declare function prefixRootArrayItemPath(idx: number, path: string): string;
5
+ //# sourceMappingURL=path.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"path.d.ts","sourceRoot":"","sources":["../../../../src/schema/internal/parser/path.ts"],"names":[],"mappings":"AAAA,yEAAyE;AACzE,wBAAgB,gBAAgB,CAAC,GAAG,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,MAAM,CAGlE;AAED,2EAA2E;AAC3E,wBAAgB,uBAAuB,CAAC,GAAG,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,MAAM,CAEzE"}
@@ -0,0 +1,11 @@
1
+ function prefixNestedPath(key, path) {
2
+ if (!path) return key;
3
+ return path.startsWith("[") ? `${key}${path}` : `${key}.${path}`;
4
+ }
5
+ function prefixRootArrayItemPath(idx, path) {
6
+ return path ? `[${idx}].${path}` : `[${idx}]`;
7
+ }
8
+ export {
9
+ prefixNestedPath,
10
+ prefixRootArrayItemPath
11
+ };
@@ -0,0 +1,8 @@
1
+ import type { JsonValue, SafeParseResult, SchemaToken } from "../../schema.js";
2
+ /**
3
+ * @internal
4
+ * Core implementation for record schemas. Parses a JSON object whose values
5
+ * are all validated by a single `valueSchema`.
6
+ */
7
+ export declare function recordSafeParse<V>(raw: ArrayBuffer | string | JsonValue, valueSchema: SchemaToken<V>): SafeParseResult<Record<string, V>>;
8
+ //# sourceMappingURL=record.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"record.d.ts","sourceRoot":"","sources":["../../../../src/schema/internal/parser/record.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAc,SAAS,EAAE,eAAe,EAAE,WAAW,EAAE,MAAM,iBAAiB,CAAC;AAM3F;;;;GAIG;AACH,wBAAgB,eAAe,CAAC,CAAC,EAC/B,GAAG,EAAE,WAAW,GAAG,MAAM,GAAG,SAAS,EACrC,WAAW,EAAE,WAAW,CAAC,CAAC,CAAC,GAC1B,eAAe,CAAC,MAAM,CAAC,MAAM,EAAE,CAAC,CAAC,CAAC,CAyCpC"}
@@ -0,0 +1,44 @@
1
+ import { resolveInput } from "./input.js";
2
+ import { prefixNestedPath } from "./path.js";
3
+ const UNSAFE_RECORD_KEYS = /* @__PURE__ */ new Set(["__proto__", "prototype", "constructor"]);
4
+ function recordSafeParse(raw, valueSchema) {
5
+ try {
6
+ const parsed = resolveInput(raw);
7
+ if (typeof parsed !== "object" || parsed === null || Array.isArray(parsed)) {
8
+ return {
9
+ success: false,
10
+ error: { fields: [{ path: "", message: "Expected an object", received: JSON.stringify(parsed) }] }
11
+ };
12
+ }
13
+ const result = /* @__PURE__ */ Object.create(null);
14
+ const errors = [];
15
+ for (const [key, value] of Object.entries(parsed)) {
16
+ if (UNSAFE_RECORD_KEYS.has(key)) {
17
+ errors.push({ path: key, message: "Unsafe record key", received: key });
18
+ continue;
19
+ }
20
+ const nested = valueSchema.safeParse(value);
21
+ if (!nested.success) {
22
+ nested.error.fields.forEach((e) => errors.push({ ...e, path: prefixNestedPath(key, e.path) }));
23
+ } else {
24
+ result[key] = nested.data;
25
+ }
26
+ }
27
+ if (errors.length > 0) return { success: false, error: { fields: errors } };
28
+ return { success: true, data: result };
29
+ } catch (e) {
30
+ return {
31
+ success: false,
32
+ error: {
33
+ fields: [{
34
+ path: "",
35
+ message: `Failed to parse JSON: ${e instanceof Error ? e.message : String(e)}`,
36
+ received: ""
37
+ }]
38
+ }
39
+ };
40
+ }
41
+ }
42
+ export {
43
+ recordSafeParse
44
+ };
@@ -0,0 +1,6 @@
1
+ import type { SchemaToken } from "../../schema.js";
2
+ /** @internal Creates an optional copy of a schema token without mutating the original. */
3
+ export declare function makeOptionalSchemaToken<T>(token: SchemaToken<T>): SchemaToken<T>;
4
+ /** @internal Returns whether a schema token requires a value. False means the field is optional. */
5
+ export declare function isSchemaRequired<T>(token: SchemaToken<T>): boolean;
6
+ //# sourceMappingURL=optionality.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"optionality.d.ts","sourceRoot":"","sources":["../../../../src/schema/internal/token/optionality.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,iBAAiB,CAAC;AAGnD,0FAA0F;AAC1F,wBAAgB,uBAAuB,CAAC,CAAC,EAAE,KAAK,EAAE,WAAW,CAAC,CAAC,CAAC,GAAG,WAAW,CAAC,CAAC,CAAC,CAEhF;AAED,oGAAoG;AACpG,wBAAgB,gBAAgB,CAAC,CAAC,EAAE,KAAK,EAAE,WAAW,CAAC,CAAC,CAAC,GAAG,OAAO,CAElE"}
@@ -0,0 +1,11 @@
1
+ import { SCHEMA_REQUIRED } from "./symbols.js";
2
+ function makeOptionalSchemaToken(token) {
3
+ return { ...token, [SCHEMA_REQUIRED]: false };
4
+ }
5
+ function isSchemaRequired(token) {
6
+ return token[SCHEMA_REQUIRED] !== false;
7
+ }
8
+ export {
9
+ isSchemaRequired,
10
+ makeOptionalSchemaToken
11
+ };
@@ -0,0 +1,7 @@
1
+ /** @internal Unique symbol used as a static brand on all schema tokens. */
2
+ export declare const SCHEMA_BRAND: unique symbol;
3
+ /** @internal Symbol used to track optionality on schema tokens. */
4
+ export declare const SCHEMA_REQUIRED: unique symbol;
5
+ /** @internal Symbol used to store the descriptor on schema tokens for compile-time introspection. */
6
+ export declare const SCHEMA_DESCRIPTOR: unique symbol;
7
+ //# sourceMappingURL=symbols.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"symbols.d.ts","sourceRoot":"","sources":["../../../../src/schema/internal/token/symbols.ts"],"names":[],"mappings":"AAAA,2EAA2E;AAC3E,eAAO,MAAM,YAAY,EAAE,OAAO,MAAsD,CAAC;AAEzF,mEAAmE;AACnE,eAAO,MAAM,eAAe,EAAE,OAAO,MAAyD,CAAC;AAE/F,qGAAqG;AACrG,eAAO,MAAM,iBAAiB,EAAE,OAAO,MAA2D,CAAC"}
@@ -0,0 +1,8 @@
1
+ const SCHEMA_BRAND = /* @__PURE__ */ Symbol.for("@flare-ts/schema/brand");
2
+ const SCHEMA_REQUIRED = /* @__PURE__ */ Symbol.for("@flare-ts/schema/required");
3
+ const SCHEMA_DESCRIPTOR = /* @__PURE__ */ Symbol.for("@flare-ts/schema/descriptor");
4
+ export {
5
+ SCHEMA_BRAND,
6
+ SCHEMA_DESCRIPTOR,
7
+ SCHEMA_REQUIRED
8
+ };
@@ -0,0 +1,20 @@
1
+ import type { TypedPrimitive } from "../../primitives/index.js";
2
+ import type { SchemaToken } from "../../schema.js";
3
+ /**
4
+ * @internal
5
+ * The union of every value type accepted in a flat schema descriptor.
6
+ * Used by the inferred overload of {@link schema} to constrain `D`
7
+ * without requiring the output type `T` to be stated up-front.
8
+ */
9
+ export type AnyDescriptorValue = TypedPrimitive<unknown> | SchemaToken<unknown>;
10
+ /**
11
+ * @internal
12
+ * Maps a concrete descriptor shape `D` to the corresponding output object type.
13
+ *
14
+ * - `SchemaToken<U>` -> `U`
15
+ * - `TypedPrimitive<U>` -> `U`
16
+ */
17
+ export type InferSchemaShape<D extends Record<string, AnyDescriptorValue>> = {
18
+ [K in keyof D]: D[K] extends SchemaToken<infer U> ? U : D[K] extends TypedPrimitive<infer U> ? U : never;
19
+ };
20
+ //# sourceMappingURL=inference.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"inference.d.ts","sourceRoot":"","sources":["../../../../src/schema/internal/types/inference.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,2BAA2B,CAAC;AAChE,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,iBAAiB,CAAC;AAEnD;;;;;GAKG;AACH,MAAM,MAAM,kBAAkB,GAAG,cAAc,CAAC,OAAO,CAAC,GAAG,WAAW,CAAC,OAAO,CAAC,CAAC;AAEhF;;;;;;GAMG;AACH,MAAM,MAAM,gBAAgB,CAAC,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,kBAAkB,CAAC,IAAI;KAC1E,CAAC,IAAI,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,SAAS,WAAW,CAAC,MAAM,CAAC,CAAC,GAAG,CAAC,GACjD,CAAC,CAAC,CAAC,CAAC,SAAS,cAAc,CAAC,MAAM,CAAC,CAAC,GAAG,CAAC,GACxC,KAAK;CACV,CAAC"}
File without changes
@@ -0,0 +1,51 @@
1
+ import type { JsonValue, OpaqueSchemaToken } from "../schema.js";
2
+ /**
3
+ * Serializes a matching JSON value to a string, compiled from a schema token.
4
+ */
5
+ export type Serializer = (doc: JsonValue) => string;
6
+ type JsonSchema = {
7
+ type: "object";
8
+ properties: Record<string, JsonSchema>;
9
+ required?: string[];
10
+ } | {
11
+ type: "object";
12
+ additionalProperties: JsonSchema;
13
+ } | {
14
+ type: "array";
15
+ items: JsonSchema;
16
+ } | {
17
+ type: "string";
18
+ format?: string;
19
+ enum?: readonly string[];
20
+ } | {
21
+ type: "integer" | "number";
22
+ minimum?: number;
23
+ maximum?: number;
24
+ } | {
25
+ type: "boolean";
26
+ } | {
27
+ anyOf: JsonSchema[];
28
+ } | Record<string, never>;
29
+ /**
30
+ * Compiles a fast JSON serializer from a schema token.
31
+ *
32
+ * The compiled serializer outperforms fast-json-stringify on typical API response
33
+ * shapes because Flare's schema carries type information (uuid, date, enum, string
34
+ * vs text) that allows the codegen to skip escape scans and use pre-built LUTs
35
+ * wherever the field domain guarantees safety.
36
+ *
37
+ * For record and discriminated-union descriptors there is no codegen branch yet,
38
+ * so a Serializer that delegates to JSON.stringify is returned. Callers see the
39
+ * same shape and behaviour as a compiled one, just without the fast-path.
40
+ *
41
+ * @param token - A schema token produced by {@link schema} or {@link model}.
42
+ */
43
+ export declare function compileSerializer(token: OpaqueSchemaToken): Serializer;
44
+ /**
45
+ * Converts a schema token into a JSON Schema Draft 7 object.
46
+ *
47
+ * @throws {Error} When the descriptor contains an unsupported shape.
48
+ */
49
+ export declare function toJsonSchema(token: OpaqueSchemaToken): JsonSchema;
50
+ export {};
51
+ //# sourceMappingURL=serializer.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"serializer.d.ts","sourceRoot":"","sources":["../../../src/schema/json/serializer.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,SAAS,EAAE,iBAAiB,EAAE,MAAM,cAAc,CAAC;AAsBjE;;GAEG;AACH,MAAM,MAAM,UAAU,GAAG,CAAC,GAAG,EAAE,SAAS,KAAK,MAAM,CAAC;AAoEpD,KAAK,UAAU,GACX;IAAE,IAAI,EAAE,QAAQ,CAAC;IAAC,UAAU,EAAE,MAAM,CAAC,MAAM,EAAE,UAAU,CAAC,CAAC;IAAC,QAAQ,CAAC,EAAE,MAAM,EAAE,CAAC;CAAE,GAChF;IAAE,IAAI,EAAE,QAAQ,CAAC;IAAC,oBAAoB,EAAE,UAAU,CAAC;CAAE,GACrD;IAAE,IAAI,EAAE,OAAO,CAAC;IAAC,KAAK,EAAE,UAAU,CAAC;CAAE,GACrC;IAAE,IAAI,EAAE,QAAQ,CAAC;IAAC,MAAM,CAAC,EAAE,MAAM,CAAC;IAAC,IAAI,CAAC,EAAE,SAAS,MAAM,EAAE,CAAC;CAAE,GAC9D;IAAE,IAAI,EAAE,SAAS,GAAG,QAAQ,CAAC;IAAC,OAAO,CAAC,EAAE,MAAM,CAAC;IAAC,OAAO,CAAC,EAAE,MAAM,CAAC;CAAE,GACnE;IAAE,IAAI,EAAE,SAAS,CAAC;CAAE,GACpB;IAAE,KAAK,EAAE,UAAU,EAAE,CAAC;CAAE,GACxB,MAAM,CAAC,MAAM,EAAE,KAAK,CAAC,CAAC;AAO1B;;;;;;;;;;;;;GAaG;AACH,wBAAgB,iBAAiB,CAAC,KAAK,EAAE,iBAAiB,GAAG,UAAU,CA4BtE;AAwMD;;;;GAIG;AACH,wBAAgB,YAAY,CAAC,KAAK,EAAE,iBAAiB,GAAG,UAAU,CAEjE"}