@livon/schema 0.27.0-rc.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.
Files changed (169) hide show
  1. package/PROMPT.md +21 -0
  2. package/README.md +13 -0
  3. package/SCHEMA.md +13 -0
  4. package/dist/SchemaValidationError.cjs +41 -0
  5. package/dist/SchemaValidationError.d.ts +20 -0
  6. package/dist/SchemaValidationError.js +7 -0
  7. package/dist/SchemaValidationError.spec.cjs +65 -0
  8. package/dist/SchemaValidationError.spec.d.ts +1 -0
  9. package/dist/SchemaValidationError.spec.js +59 -0
  10. package/dist/after.cjs +36 -0
  11. package/dist/after.d.ts +30 -0
  12. package/dist/after.js +2 -0
  13. package/dist/after.spec.cjs +54 -0
  14. package/dist/after.spec.d.ts +1 -0
  15. package/dist/after.spec.js +48 -0
  16. package/dist/and.cjs +36 -0
  17. package/dist/and.d.ts +26 -0
  18. package/dist/and.js +2 -0
  19. package/dist/and.spec.cjs +57 -0
  20. package/dist/and.spec.d.ts +1 -0
  21. package/dist/and.spec.js +51 -0
  22. package/dist/api.cjs +317 -0
  23. package/dist/api.d.ts +107 -0
  24. package/dist/api.js +277 -0
  25. package/dist/api.spec.cjs +512 -0
  26. package/dist/api.spec.d.ts +1 -0
  27. package/dist/api.spec.js +506 -0
  28. package/dist/array.cjs +74 -0
  29. package/dist/array.d.ts +25 -0
  30. package/dist/array.js +40 -0
  31. package/dist/array.spec.cjs +167 -0
  32. package/dist/array.spec.d.ts +1 -0
  33. package/dist/array.spec.js +161 -0
  34. package/dist/before.cjs +36 -0
  35. package/dist/before.d.ts +30 -0
  36. package/dist/before.js +2 -0
  37. package/dist/before.spec.cjs +54 -0
  38. package/dist/before.spec.d.ts +1 -0
  39. package/dist/before.spec.js +48 -0
  40. package/dist/binary.cjs +53 -0
  41. package/dist/binary.d.ts +24 -0
  42. package/dist/binary.js +19 -0
  43. package/dist/binary.spec.cjs +107 -0
  44. package/dist/binary.spec.d.ts +1 -0
  45. package/dist/binary.spec.js +101 -0
  46. package/dist/boolean.cjs +53 -0
  47. package/dist/boolean.d.ts +24 -0
  48. package/dist/boolean.js +19 -0
  49. package/dist/boolean.spec.cjs +96 -0
  50. package/dist/boolean.spec.d.ts +1 -0
  51. package/dist/boolean.spec.js +90 -0
  52. package/dist/context.cjs +125 -0
  53. package/dist/context.d.ts +101 -0
  54. package/dist/context.js +76 -0
  55. package/dist/context.spec.cjs +244 -0
  56. package/dist/context.spec.d.ts +1 -0
  57. package/dist/context.spec.js +238 -0
  58. package/dist/date.cjs +53 -0
  59. package/dist/date.d.ts +24 -0
  60. package/dist/date.js +19 -0
  61. package/dist/date.spec.cjs +97 -0
  62. package/dist/date.spec.d.ts +1 -0
  63. package/dist/date.spec.js +91 -0
  64. package/dist/doc.cjs +54 -0
  65. package/dist/doc.d.ts +25 -0
  66. package/dist/doc.js +17 -0
  67. package/dist/doc.spec.cjs +99 -0
  68. package/dist/doc.spec.d.ts +1 -0
  69. package/dist/doc.spec.js +93 -0
  70. package/dist/enumeration.cjs +74 -0
  71. package/dist/enumeration.d.ts +50 -0
  72. package/dist/enumeration.js +40 -0
  73. package/dist/enumeration.spec.cjs +110 -0
  74. package/dist/enumeration.spec.d.ts +1 -0
  75. package/dist/enumeration.spec.js +104 -0
  76. package/dist/hydrate.cjs +18 -0
  77. package/dist/hydrate.d.ts +1 -0
  78. package/dist/hydrate.js +0 -0
  79. package/dist/index.cjs +145 -0
  80. package/dist/index.d.ts +34 -0
  81. package/dist/index.js +24 -0
  82. package/dist/index.spec.cjs +43 -0
  83. package/dist/index.spec.d.ts +1 -0
  84. package/dist/index.spec.js +37 -0
  85. package/dist/literal.cjs +55 -0
  86. package/dist/literal.d.ts +25 -0
  87. package/dist/literal.js +21 -0
  88. package/dist/literal.spec.cjs +93 -0
  89. package/dist/literal.spec.d.ts +1 -0
  90. package/dist/literal.spec.js +87 -0
  91. package/dist/number.cjs +89 -0
  92. package/dist/number.d.ts +84 -0
  93. package/dist/number.js +55 -0
  94. package/dist/number.spec.cjs +155 -0
  95. package/dist/number.spec.d.ts +1 -0
  96. package/dist/number.spec.js +149 -0
  97. package/dist/object.cjs +66 -0
  98. package/dist/object.d.ts +37 -0
  99. package/dist/object.js +32 -0
  100. package/dist/object.spec.cjs +171 -0
  101. package/dist/object.spec.d.ts +1 -0
  102. package/dist/object.spec.js +165 -0
  103. package/dist/operation.cjs +182 -0
  104. package/dist/operation.d.ts +197 -0
  105. package/dist/operation.js +133 -0
  106. package/dist/operation.spec.cjs +454 -0
  107. package/dist/operation.spec.d.ts +1 -0
  108. package/dist/operation.spec.js +448 -0
  109. package/dist/or.cjs +85 -0
  110. package/dist/or.d.ts +37 -0
  111. package/dist/or.js +51 -0
  112. package/dist/or.spec.cjs +204 -0
  113. package/dist/or.spec.d.ts +1 -0
  114. package/dist/or.spec.js +198 -0
  115. package/dist/schema.cjs +285 -0
  116. package/dist/schema.d.ts +132 -0
  117. package/dist/schema.js +233 -0
  118. package/dist/schema.spec.cjs +587 -0
  119. package/dist/schema.spec.d.ts +1 -0
  120. package/dist/schema.spec.js +581 -0
  121. package/dist/schemaFactory.cjs +125 -0
  122. package/dist/schemaFactory.d.ts +97 -0
  123. package/dist/schemaFactory.js +88 -0
  124. package/dist/schemaFactory.spec.cjs +197 -0
  125. package/dist/schemaFactory.spec.d.ts +1 -0
  126. package/dist/schemaFactory.spec.js +191 -0
  127. package/dist/schemaModule.cjs +280 -0
  128. package/dist/schemaModule.d.ts +97 -0
  129. package/dist/schemaModule.js +243 -0
  130. package/dist/schemaModule.spec.cjs +355 -0
  131. package/dist/schemaModule.spec.d.ts +1 -0
  132. package/dist/schemaModule.spec.js +349 -0
  133. package/dist/string.cjs +93 -0
  134. package/dist/string.d.ts +85 -0
  135. package/dist/string.js +59 -0
  136. package/dist/string.spec.cjs +158 -0
  137. package/dist/string.spec.d.ts +1 -0
  138. package/dist/string.spec.js +152 -0
  139. package/dist/testing/mocks/assertions.mock.cjs +48 -0
  140. package/dist/testing/mocks/assertions.mock.d.ts +5 -0
  141. package/dist/testing/mocks/assertions.mock.js +14 -0
  142. package/dist/testing/mocks/index.cjs +52 -0
  143. package/dist/testing/mocks/index.d.ts +4 -0
  144. package/dist/testing/mocks/index.js +3 -0
  145. package/dist/testing/mocks/schema.mock.cjs +120 -0
  146. package/dist/testing/mocks/schema.mock.d.ts +37 -0
  147. package/dist/testing/mocks/schema.mock.js +74 -0
  148. package/dist/tuple.cjs +58 -0
  149. package/dist/tuple.d.ts +33 -0
  150. package/dist/tuple.js +24 -0
  151. package/dist/tuple.spec.cjs +162 -0
  152. package/dist/tuple.spec.d.ts +1 -0
  153. package/dist/tuple.spec.js +156 -0
  154. package/dist/typeGuards.cjs +60 -0
  155. package/dist/typeGuards.d.ts +93 -0
  156. package/dist/typeGuards.js +8 -0
  157. package/dist/typeGuards.spec.cjs +101 -0
  158. package/dist/typeGuards.spec.d.ts +1 -0
  159. package/dist/typeGuards.spec.js +95 -0
  160. package/dist/types.cjs +18 -0
  161. package/dist/types.d.ts +289 -0
  162. package/dist/types.js +0 -0
  163. package/dist/union.cjs +74 -0
  164. package/dist/union.d.ts +33 -0
  165. package/dist/union.js +40 -0
  166. package/dist/union.spec.cjs +159 -0
  167. package/dist/union.spec.d.ts +1 -0
  168. package/dist/union.spec.js +153 -0
  169. package/package.json +47 -0
@@ -0,0 +1,101 @@
1
+ "use strict";
2
+ var __webpack_exports__ = {};
3
+ const external_vitest_namespaceObject = require("vitest");
4
+ const external_typeGuards_cjs_namespaceObject = require("./typeGuards.cjs");
5
+ (0, external_vitest_namespaceObject.describe)('typeGuards', ()=>{
6
+ let validDate;
7
+ let invalidDate;
8
+ let bytes;
9
+ (0, external_vitest_namespaceObject.beforeAll)(()=>{
10
+ validDate = new Date(0);
11
+ invalidDate = new Date('invalid');
12
+ bytes = new Uint8Array([]);
13
+ });
14
+ (0, external_vitest_namespaceObject.beforeEach)(()=>{
15
+ validDate = new Date('2026-01-01T00:00:00.000Z');
16
+ invalidDate = new Date('invalid');
17
+ bytes = new Uint8Array([
18
+ 1,
19
+ 2,
20
+ 3
21
+ ]);
22
+ });
23
+ (0, external_vitest_namespaceObject.afterEach)(()=>{
24
+ validDate = new Date(0);
25
+ invalidDate = new Date('invalid');
26
+ bytes = new Uint8Array([]);
27
+ });
28
+ (0, external_vitest_namespaceObject.afterAll)(()=>{
29
+ validDate = new Date(0);
30
+ invalidDate = new Date('invalid');
31
+ bytes = new Uint8Array([]);
32
+ });
33
+ (0, external_vitest_namespaceObject.describe)('happy', ()=>{
34
+ (0, external_vitest_namespaceObject.it)('should return true when isString receives string input', ()=>{
35
+ (0, external_vitest_namespaceObject.expect)((0, external_typeGuards_cjs_namespaceObject.isString)('livon')).toBe(true);
36
+ });
37
+ (0, external_vitest_namespaceObject.it)('should return true when isNumber receives finite number input', ()=>{
38
+ (0, external_vitest_namespaceObject.expect)((0, external_typeGuards_cjs_namespaceObject.isNumber)(42)).toBe(true);
39
+ });
40
+ (0, external_vitest_namespaceObject.it)('should return true when isBoolean receives boolean input', ()=>{
41
+ (0, external_vitest_namespaceObject.expect)((0, external_typeGuards_cjs_namespaceObject.isBoolean)(false)).toBe(true);
42
+ });
43
+ (0, external_vitest_namespaceObject.it)('should return true when isDate receives valid date input', ()=>{
44
+ (0, external_vitest_namespaceObject.expect)((0, external_typeGuards_cjs_namespaceObject.isDate)(validDate)).toBe(true);
45
+ });
46
+ (0, external_vitest_namespaceObject.it)('should return true when isUint8Array receives uint8array input', ()=>{
47
+ (0, external_vitest_namespaceObject.expect)((0, external_typeGuards_cjs_namespaceObject.isUint8Array)(bytes)).toBe(true);
48
+ });
49
+ (0, external_vitest_namespaceObject.it)('should return true when isRecord receives plain object input', ()=>{
50
+ (0, external_vitest_namespaceObject.expect)((0, external_typeGuards_cjs_namespaceObject.isRecord)({
51
+ id: 'u-1'
52
+ })).toBe(true);
53
+ });
54
+ (0, external_vitest_namespaceObject.it)('should return true when isArray guard receives array input', ()=>{
55
+ const guard = (0, external_typeGuards_cjs_namespaceObject.isArray)();
56
+ (0, external_vitest_namespaceObject.expect)(guard([
57
+ 'a',
58
+ 'b'
59
+ ])).toBe(true);
60
+ });
61
+ });
62
+ (0, external_vitest_namespaceObject.describe)('sad', ()=>{
63
+ (0, external_vitest_namespaceObject.it)('should return false when isString receives non-string input', ()=>{
64
+ (0, external_vitest_namespaceObject.expect)((0, external_typeGuards_cjs_namespaceObject.isString)(42)).toBe(false);
65
+ });
66
+ (0, external_vitest_namespaceObject.it)('should return false when isNumber receives nan input', ()=>{
67
+ (0, external_vitest_namespaceObject.expect)((0, external_typeGuards_cjs_namespaceObject.isNumber)(NaN)).toBe(false);
68
+ });
69
+ (0, external_vitest_namespaceObject.it)('should return false when isBoolean receives non-boolean input', ()=>{
70
+ (0, external_vitest_namespaceObject.expect)((0, external_typeGuards_cjs_namespaceObject.isBoolean)('true')).toBe(false);
71
+ });
72
+ (0, external_vitest_namespaceObject.it)('should return false when isDate receives invalid date input', ()=>{
73
+ (0, external_vitest_namespaceObject.expect)((0, external_typeGuards_cjs_namespaceObject.isDate)(invalidDate)).toBe(false);
74
+ });
75
+ (0, external_vitest_namespaceObject.it)('should return false when isUint8Array receives non-uint8array input', ()=>{
76
+ (0, external_vitest_namespaceObject.expect)((0, external_typeGuards_cjs_namespaceObject.isUint8Array)([
77
+ 1,
78
+ 2,
79
+ 3
80
+ ])).toBe(false);
81
+ });
82
+ (0, external_vitest_namespaceObject.it)('should return false when isRecord receives null input', ()=>{
83
+ (0, external_vitest_namespaceObject.expect)((0, external_typeGuards_cjs_namespaceObject.isRecord)(null)).toBe(false);
84
+ });
85
+ (0, external_vitest_namespaceObject.it)('should return false when isRecord receives array input', ()=>{
86
+ (0, external_vitest_namespaceObject.expect)((0, external_typeGuards_cjs_namespaceObject.isRecord)([
87
+ 'a'
88
+ ])).toBe(false);
89
+ });
90
+ (0, external_vitest_namespaceObject.it)('should return false when isArray guard receives non-array input', ()=>{
91
+ const guard = (0, external_typeGuards_cjs_namespaceObject.isArray)();
92
+ (0, external_vitest_namespaceObject.expect)(guard({
93
+ value: 'x'
94
+ })).toBe(false);
95
+ });
96
+ });
97
+ });
98
+ for(var __rspack_i in __webpack_exports__)exports[__rspack_i] = __webpack_exports__[__rspack_i];
99
+ Object.defineProperty(exports, '__esModule', {
100
+ value: true
101
+ });
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,95 @@
1
+ import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it } from "vitest";
2
+ import { isArray, isBoolean, isDate, isNumber, isRecord, isString, isUint8Array } from "./typeGuards.js";
3
+ describe('typeGuards', ()=>{
4
+ let validDate;
5
+ let invalidDate;
6
+ let bytes;
7
+ beforeAll(()=>{
8
+ validDate = new Date(0);
9
+ invalidDate = new Date('invalid');
10
+ bytes = new Uint8Array([]);
11
+ });
12
+ beforeEach(()=>{
13
+ validDate = new Date('2026-01-01T00:00:00.000Z');
14
+ invalidDate = new Date('invalid');
15
+ bytes = new Uint8Array([
16
+ 1,
17
+ 2,
18
+ 3
19
+ ]);
20
+ });
21
+ afterEach(()=>{
22
+ validDate = new Date(0);
23
+ invalidDate = new Date('invalid');
24
+ bytes = new Uint8Array([]);
25
+ });
26
+ afterAll(()=>{
27
+ validDate = new Date(0);
28
+ invalidDate = new Date('invalid');
29
+ bytes = new Uint8Array([]);
30
+ });
31
+ describe('happy', ()=>{
32
+ it('should return true when isString receives string input', ()=>{
33
+ expect(isString('livon')).toBe(true);
34
+ });
35
+ it('should return true when isNumber receives finite number input', ()=>{
36
+ expect(isNumber(42)).toBe(true);
37
+ });
38
+ it('should return true when isBoolean receives boolean input', ()=>{
39
+ expect(isBoolean(false)).toBe(true);
40
+ });
41
+ it('should return true when isDate receives valid date input', ()=>{
42
+ expect(isDate(validDate)).toBe(true);
43
+ });
44
+ it('should return true when isUint8Array receives uint8array input', ()=>{
45
+ expect(isUint8Array(bytes)).toBe(true);
46
+ });
47
+ it('should return true when isRecord receives plain object input', ()=>{
48
+ expect(isRecord({
49
+ id: 'u-1'
50
+ })).toBe(true);
51
+ });
52
+ it('should return true when isArray guard receives array input', ()=>{
53
+ const guard = isArray();
54
+ expect(guard([
55
+ 'a',
56
+ 'b'
57
+ ])).toBe(true);
58
+ });
59
+ });
60
+ describe('sad', ()=>{
61
+ it('should return false when isString receives non-string input', ()=>{
62
+ expect(isString(42)).toBe(false);
63
+ });
64
+ it('should return false when isNumber receives nan input', ()=>{
65
+ expect(isNumber(NaN)).toBe(false);
66
+ });
67
+ it('should return false when isBoolean receives non-boolean input', ()=>{
68
+ expect(isBoolean('true')).toBe(false);
69
+ });
70
+ it('should return false when isDate receives invalid date input', ()=>{
71
+ expect(isDate(invalidDate)).toBe(false);
72
+ });
73
+ it('should return false when isUint8Array receives non-uint8array input', ()=>{
74
+ expect(isUint8Array([
75
+ 1,
76
+ 2,
77
+ 3
78
+ ])).toBe(false);
79
+ });
80
+ it('should return false when isRecord receives null input', ()=>{
81
+ expect(isRecord(null)).toBe(false);
82
+ });
83
+ it('should return false when isRecord receives array input', ()=>{
84
+ expect(isRecord([
85
+ 'a'
86
+ ])).toBe(false);
87
+ });
88
+ it('should return false when isArray guard receives non-array input', ()=>{
89
+ const guard = isArray();
90
+ expect(guard({
91
+ value: 'x'
92
+ })).toBe(false);
93
+ });
94
+ });
95
+ });
package/dist/types.cjs ADDED
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ var __webpack_require__ = {};
3
+ (()=>{
4
+ __webpack_require__.r = (exports1)=>{
5
+ if ("u" > typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
6
+ value: 'Module'
7
+ });
8
+ Object.defineProperty(exports1, '__esModule', {
9
+ value: true
10
+ });
11
+ };
12
+ })();
13
+ var __webpack_exports__ = {};
14
+ __webpack_require__.r(__webpack_exports__);
15
+ for(var __rspack_i in __webpack_exports__)exports[__rspack_i] = __webpack_exports__[__rspack_i];
16
+ Object.defineProperty(exports, '__esModule', {
17
+ value: true
18
+ });
@@ -0,0 +1,289 @@
1
+ export type SchemaPath = readonly (string | number)[];
2
+ export type SchemaIssue = {
3
+ path: SchemaPath;
4
+ message: string;
5
+ code?: string;
6
+ context?: Readonly<Record<string, unknown>>;
7
+ };
8
+ export type SchemaResultOk<T> = {
9
+ ok: true;
10
+ value: T;
11
+ };
12
+ export type SchemaResultFail = {
13
+ ok: false;
14
+ issues: readonly SchemaIssue[];
15
+ meta?: SchemaErrorMeta;
16
+ };
17
+ export type SchemaResult<T> = SchemaResultOk<T> | SchemaResultFail;
18
+ export type SchemaErrorMeta = {
19
+ request?: SchemaRequestContext;
20
+ build?: SchemaBuildContext;
21
+ type?: string;
22
+ name?: string;
23
+ };
24
+ export interface AstNode {
25
+ type: string;
26
+ name?: string;
27
+ doc?: Readonly<Record<string, unknown>>;
28
+ request?: string;
29
+ response?: string;
30
+ dependsOn?: string;
31
+ constraints?: Readonly<Record<string, unknown>>;
32
+ children?: readonly AstNode[];
33
+ }
34
+ export type SchemaDoc = string | Readonly<Record<string, unknown>>;
35
+ export interface AstBuilderAdd {
36
+ (node: AstNode): AstNode;
37
+ }
38
+ export interface AstBuilderGetAll {
39
+ (): readonly AstNode[];
40
+ }
41
+ export interface AstBuilder {
42
+ add: AstBuilderAdd;
43
+ getAll: AstBuilderGetAll;
44
+ }
45
+ export interface SchemaBuildContext {
46
+ buildId: string;
47
+ builder: AstBuilder;
48
+ parentNode?: AstNode;
49
+ schemaPath: readonly string[];
50
+ buildOptions: Readonly<Record<string, unknown>>;
51
+ }
52
+ export type SchemaBuildContextInput = Partial<SchemaBuildContext>;
53
+ export interface SchemaStateGet {
54
+ <T = unknown>(key: string): T | undefined;
55
+ }
56
+ export interface SchemaStateSet {
57
+ <T = unknown>(key: string, value: T): void;
58
+ }
59
+ export interface SchemaStateUpdater<T> {
60
+ (current: T | undefined): T;
61
+ }
62
+ export interface SchemaStateUpdate {
63
+ <T = unknown>(key: string, updater: SchemaStateUpdater<T>): void;
64
+ }
65
+ export interface SchemaStateSnapshot {
66
+ (): Readonly<Record<string, unknown>>;
67
+ }
68
+ export interface SchemaState {
69
+ get: SchemaStateGet;
70
+ set: SchemaStateSet;
71
+ update: SchemaStateUpdate;
72
+ snapshot: SchemaStateSnapshot;
73
+ }
74
+ export type AckMode = 'received' | 'handled';
75
+ export interface AckConfig {
76
+ required?: boolean;
77
+ mode?: AckMode;
78
+ timeoutMs?: number;
79
+ retries?: number;
80
+ }
81
+ export type PublishAck = boolean | AckConfig;
82
+ export type PublishInput = {
83
+ topic: string;
84
+ payload: unknown;
85
+ input?: unknown;
86
+ ack?: PublishAck;
87
+ key?: string;
88
+ meta?: Readonly<Record<string, unknown>>;
89
+ };
90
+ export interface Publisher {
91
+ (input: PublishInput): void | Promise<void>;
92
+ }
93
+ export interface Logger {
94
+ error?: (message: string, info?: Readonly<Record<string, unknown>>, error?: unknown) => void;
95
+ warn?: (message: string, info?: Readonly<Record<string, unknown>>) => void;
96
+ info?: (message: string, info?: Readonly<Record<string, unknown>>) => void;
97
+ debug?: (message: string, info?: Readonly<Record<string, unknown>>) => void;
98
+ }
99
+ export interface PublishErrorHandler {
100
+ (error: unknown, info?: Readonly<Record<string, unknown>>): void;
101
+ }
102
+ export interface SchemaRequestContext {
103
+ requestId: string;
104
+ timestamp: number;
105
+ correlationId?: string;
106
+ sourceId?: string;
107
+ userId?: string;
108
+ tenantId?: string;
109
+ metadata?: Readonly<Record<string, unknown>>;
110
+ normalized?: true;
111
+ state: SchemaState;
112
+ publisher?: Publisher;
113
+ onPublishError?: PublishErrorHandler;
114
+ logger?: Logger;
115
+ }
116
+ export type SchemaRequestContextInput = Partial<Omit<SchemaRequestContext, 'normalized' | 'state'>> | SchemaRequestContext;
117
+ export interface SchemaContextGetBuild {
118
+ (): SchemaBuildContext | undefined;
119
+ }
120
+ export interface SchemaContextSetBuild {
121
+ (ctx?: SchemaBuildContext): void;
122
+ }
123
+ export interface SchemaContextGetRequest {
124
+ (): SchemaRequestContext | undefined;
125
+ }
126
+ export interface SchemaContextSetRequest {
127
+ (ctx?: SchemaRequestContextInput): void;
128
+ }
129
+ export interface SchemaContext {
130
+ getBuildContext: SchemaContextGetBuild;
131
+ setBuildContext: SchemaContextSetBuild;
132
+ getRequestContext: SchemaContextGetRequest;
133
+ setRequestContext: SchemaContextSetRequest;
134
+ request?: SchemaRequestContext;
135
+ state: SchemaState;
136
+ }
137
+ export interface SchemaParse<T> {
138
+ (input: unknown, ctx?: SchemaContext): T;
139
+ }
140
+ export interface SchemaValidate<T> {
141
+ (input: unknown, ctx?: SchemaContext): SchemaResult<T>;
142
+ }
143
+ export interface SchemaTyped<T> {
144
+ (input: T, ctx?: SchemaContext): T;
145
+ }
146
+ export interface SchemaAst {
147
+ (input?: SchemaBuildContextInput): AstNode;
148
+ }
149
+ export interface SchemaRefine<T> {
150
+ (value: T, ctx: SchemaContext): boolean;
151
+ }
152
+ export interface SchemaRefineInput<T> {
153
+ predicate: SchemaRefine<T>;
154
+ message: string;
155
+ code?: string;
156
+ }
157
+ export type SchemaHookBeforeResult = unknown | {
158
+ input: unknown;
159
+ } | {
160
+ issues: readonly SchemaIssue[];
161
+ } | {
162
+ input: unknown;
163
+ issues: readonly SchemaIssue[];
164
+ };
165
+ export type SchemaHookAfterResult<U> = U | {
166
+ value: U;
167
+ } | {
168
+ issues: readonly SchemaIssue[];
169
+ } | {
170
+ value: U;
171
+ issues: readonly SchemaIssue[];
172
+ };
173
+ export interface SchemaHookBefore {
174
+ (input: unknown, ctx: SchemaContext): SchemaHookBeforeResult;
175
+ }
176
+ export interface SchemaHookAfter<T, U> {
177
+ (value: T, ctx: SchemaContext): SchemaHookAfterResult<U>;
178
+ }
179
+ /**
180
+ * Adds `undefined` as an accepted value for the current schema.
181
+ *
182
+ * @returns A schema that accepts the original type or `undefined`.
183
+ * @see https://live-input-vector-output-node.github.io/livon-ts/docs/schema/type-safety
184
+ *
185
+ * @example
186
+ * const MaybeName = string().optional();
187
+ */
188
+ export interface SchemaOptional<T> {
189
+ (): Schema<T | undefined>;
190
+ }
191
+ /**
192
+ * Adds `null` as an accepted value for the current schema.
193
+ *
194
+ * @returns A schema that accepts the original type or `null`.
195
+ * @see https://live-input-vector-output-node.github.io/livon-ts/docs/schema/type-safety
196
+ *
197
+ * @example
198
+ * const NullableName = string().nullable();
199
+ */
200
+ export interface SchemaNullable<T> {
201
+ (): Schema<T | null>;
202
+ }
203
+ /**
204
+ * Adds an additional predicate-based validation step.
205
+ *
206
+ * @param input - Predicate and error metadata.
207
+ * @returns A schema with the extra refinement rule.
208
+ * @see https://live-input-vector-output-node.github.io/livon-ts/docs/schema/type-safety
209
+ *
210
+ * @example
211
+ * const Adult = number().refine({
212
+ * predicate: (value) => value >= 18,
213
+ * message: 'Must be at least 18',
214
+ * });
215
+ */
216
+ export interface SchemaRefineMethod<T> {
217
+ (input: SchemaRefineInput<T>): Schema<T>;
218
+ }
219
+ /**
220
+ * Runs a preprocessing hook before base validation.
221
+ *
222
+ * @param hook - Hook that can transform input and/or emit issues.
223
+ * @returns A schema with the before-hook applied.
224
+ * @see https://live-input-vector-output-node.github.io/livon-ts/docs/schema/type-safety
225
+ *
226
+ * @example
227
+ * const Trimmed = string().before((input) =>
228
+ * typeof input === 'string' ? input.trim() : input
229
+ * );
230
+ */
231
+ export interface SchemaBeforeMethod<T> {
232
+ (hook: SchemaHookBefore): Schema<T>;
233
+ }
234
+ /**
235
+ * Runs a postprocessing hook after successful validation.
236
+ *
237
+ * @param hook - Hook that maps validated value to a new value.
238
+ * @returns A schema of the transformed output type.
239
+ * @see https://live-input-vector-output-node.github.io/livon-ts/docs/schema/type-safety
240
+ *
241
+ * @example
242
+ * const UserId = string().after((value) => value.toUpperCase());
243
+ */
244
+ export interface SchemaAfterMethod<T> {
245
+ <U>(hook: SchemaHookAfter<T, U>): Schema<U>;
246
+ }
247
+ /**
248
+ * Creates an intersection schema with another schema.
249
+ *
250
+ * @param other - Schema to intersect with.
251
+ * @returns A schema that must satisfy both schemas.
252
+ * @see https://live-input-vector-output-node.github.io/livon-ts/docs/schema/type-safety
253
+ *
254
+ * @example
255
+ * const Combined = LeftSchema.and(RightSchema);
256
+ */
257
+ export interface SchemaAndMethod<T> {
258
+ <U>(other: Schema<U>): Schema<T & U>;
259
+ }
260
+ /**
261
+ * Attaches documentation metadata to the schema AST node.
262
+ *
263
+ * @param doc - Documentation text or structured metadata.
264
+ * @returns A schema with merged documentation metadata.
265
+ * @see https://live-input-vector-output-node.github.io/livon-ts/docs/schema/type-safety
266
+ *
267
+ * @example
268
+ * const Email = string().describe('User e-mail address');
269
+ */
270
+ export interface SchemaDescribeMethod<T> {
271
+ (doc: SchemaDoc): Schema<T>;
272
+ }
273
+ export interface Schema<T> {
274
+ name: string;
275
+ type: string;
276
+ ast: SchemaAst;
277
+ validate: SchemaValidate<T>;
278
+ parse: SchemaParse<T>;
279
+ typed: SchemaTyped<T>;
280
+ optional: SchemaOptional<T>;
281
+ nullable: SchemaNullable<T>;
282
+ describe: SchemaDescribeMethod<T>;
283
+ refine: SchemaRefineMethod<T>;
284
+ before: SchemaBeforeMethod<T>;
285
+ after: SchemaAfterMethod<T>;
286
+ and: SchemaAndMethod<T>;
287
+ }
288
+ export type Shape = Readonly<Record<string, Schema<unknown>>>;
289
+ export type Infer<TSchema> = TSchema extends Schema<infer TValue> ? TValue : never;
package/dist/types.js ADDED
File without changes
package/dist/union.cjs ADDED
@@ -0,0 +1,74 @@
1
+ "use strict";
2
+ var __webpack_require__ = {};
3
+ (()=>{
4
+ __webpack_require__.d = (exports1, definition)=>{
5
+ for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
6
+ enumerable: true,
7
+ get: definition[key]
8
+ });
9
+ };
10
+ })();
11
+ (()=>{
12
+ __webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
13
+ })();
14
+ (()=>{
15
+ __webpack_require__.r = (exports1)=>{
16
+ if ("u" > typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
17
+ value: 'Module'
18
+ });
19
+ Object.defineProperty(exports1, '__esModule', {
20
+ value: true
21
+ });
22
+ };
23
+ })();
24
+ var __webpack_exports__ = {};
25
+ __webpack_require__.r(__webpack_exports__);
26
+ __webpack_require__.d(__webpack_exports__, {
27
+ union: ()=>union
28
+ });
29
+ const external_schemaFactory_cjs_namespaceObject = require("./schemaFactory.cjs");
30
+ const resolveUnionMatch = ({ input, ctx, options })=>{
31
+ const matches = options.map((option)=>{
32
+ try {
33
+ return {
34
+ ok: true,
35
+ value: option.parse(input, ctx)
36
+ };
37
+ } catch (error) {
38
+ return {
39
+ ok: false,
40
+ error
41
+ };
42
+ }
43
+ }).find((result)=>result.ok);
44
+ if (!matches) throw {
45
+ message: 'No union match',
46
+ code: 'union.match'
47
+ };
48
+ return matches.value;
49
+ };
50
+ const union = ({ name, options, doc })=>(0, external_schemaFactory_cjs_namespaceObject.schemaFactory)({
51
+ name,
52
+ type: 'union',
53
+ doc,
54
+ ast: (ctx)=>{
55
+ const build = ctx.getBuildContext();
56
+ return {
57
+ type: 'union',
58
+ name,
59
+ children: options.map((option)=>option.ast(build ?? void 0))
60
+ };
61
+ },
62
+ validate: (input, ctx)=>resolveUnionMatch({
63
+ input,
64
+ ctx,
65
+ options
66
+ })
67
+ });
68
+ exports.union = __webpack_exports__.union;
69
+ for(var __rspack_i in __webpack_exports__)if (-1 === [
70
+ "union"
71
+ ].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
72
+ Object.defineProperty(exports, '__esModule', {
73
+ value: true
74
+ });
@@ -0,0 +1,33 @@
1
+ import { Schema, SchemaDoc } from './types.js';
2
+ type AnySchema = Schema<any>;
3
+ export interface UnionSchemaInput<TValues extends readonly AnySchema[]> {
4
+ name: string;
5
+ options: TValues;
6
+ doc?: SchemaDoc;
7
+ }
8
+ /**
9
+ * union is part of the public LIVON API.
10
+ *
11
+ * @remarks
12
+ * Parameter and return types are defined in the TypeScript signature.
13
+ *
14
+ * @see https://live-input-vector-output-node.github.io/livon-ts/docs/schema/union
15
+ *
16
+ * @example
17
+ * // Creates a union schema that accepts either string or number identifiers.
18
+ * const Identifier = union({
19
+ * name: 'identifier',
20
+ * options: [string(), number()] as const,
21
+ * });
22
+ * Identifier.parse('user-1');
23
+ *
24
+ * @example
25
+ * // Extends the union schema to also allow undefined.
26
+ * const OptionalIdentifier = union({
27
+ * name: 'identifier',
28
+ * options: [string(), number()] as const,
29
+ * }).optional();
30
+ * OptionalIdentifier.parse(undefined);
31
+ */
32
+ export declare const union: <TValues extends readonly AnySchema[]>({ name, options, doc, }: UnionSchemaInput<TValues>) => import("./schemaFactory.js").SchemaWithChain<ReturnType<TValues[number]["parse"]>, import("./schemaFactory.js").SchemaFactoryChainDefinition<ReturnType<TValues[number]["parse"]>>>;
33
+ export {};
package/dist/union.js ADDED
@@ -0,0 +1,40 @@
1
+ import { schemaFactory } from "./schemaFactory.js";
2
+ const resolveUnionMatch = ({ input, ctx, options })=>{
3
+ const matches = options.map((option)=>{
4
+ try {
5
+ return {
6
+ ok: true,
7
+ value: option.parse(input, ctx)
8
+ };
9
+ } catch (error) {
10
+ return {
11
+ ok: false,
12
+ error
13
+ };
14
+ }
15
+ }).find((result)=>result.ok);
16
+ if (!matches) throw {
17
+ message: 'No union match',
18
+ code: 'union.match'
19
+ };
20
+ return matches.value;
21
+ };
22
+ const union = ({ name, options, doc })=>schemaFactory({
23
+ name,
24
+ type: 'union',
25
+ doc,
26
+ ast: (ctx)=>{
27
+ const build = ctx.getBuildContext();
28
+ return {
29
+ type: 'union',
30
+ name,
31
+ children: options.map((option)=>option.ast(build ?? void 0))
32
+ };
33
+ },
34
+ validate: (input, ctx)=>resolveUnionMatch({
35
+ input,
36
+ ctx,
37
+ options
38
+ })
39
+ });
40
+ export { union };