@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.
- package/PROMPT.md +21 -0
- package/README.md +13 -0
- package/SCHEMA.md +13 -0
- package/dist/SchemaValidationError.cjs +41 -0
- package/dist/SchemaValidationError.d.ts +20 -0
- package/dist/SchemaValidationError.js +7 -0
- package/dist/SchemaValidationError.spec.cjs +65 -0
- package/dist/SchemaValidationError.spec.d.ts +1 -0
- package/dist/SchemaValidationError.spec.js +59 -0
- package/dist/after.cjs +36 -0
- package/dist/after.d.ts +30 -0
- package/dist/after.js +2 -0
- package/dist/after.spec.cjs +54 -0
- package/dist/after.spec.d.ts +1 -0
- package/dist/after.spec.js +48 -0
- package/dist/and.cjs +36 -0
- package/dist/and.d.ts +26 -0
- package/dist/and.js +2 -0
- package/dist/and.spec.cjs +57 -0
- package/dist/and.spec.d.ts +1 -0
- package/dist/and.spec.js +51 -0
- package/dist/api.cjs +317 -0
- package/dist/api.d.ts +107 -0
- package/dist/api.js +277 -0
- package/dist/api.spec.cjs +512 -0
- package/dist/api.spec.d.ts +1 -0
- package/dist/api.spec.js +506 -0
- package/dist/array.cjs +74 -0
- package/dist/array.d.ts +25 -0
- package/dist/array.js +40 -0
- package/dist/array.spec.cjs +167 -0
- package/dist/array.spec.d.ts +1 -0
- package/dist/array.spec.js +161 -0
- package/dist/before.cjs +36 -0
- package/dist/before.d.ts +30 -0
- package/dist/before.js +2 -0
- package/dist/before.spec.cjs +54 -0
- package/dist/before.spec.d.ts +1 -0
- package/dist/before.spec.js +48 -0
- package/dist/binary.cjs +53 -0
- package/dist/binary.d.ts +24 -0
- package/dist/binary.js +19 -0
- package/dist/binary.spec.cjs +107 -0
- package/dist/binary.spec.d.ts +1 -0
- package/dist/binary.spec.js +101 -0
- package/dist/boolean.cjs +53 -0
- package/dist/boolean.d.ts +24 -0
- package/dist/boolean.js +19 -0
- package/dist/boolean.spec.cjs +96 -0
- package/dist/boolean.spec.d.ts +1 -0
- package/dist/boolean.spec.js +90 -0
- package/dist/context.cjs +125 -0
- package/dist/context.d.ts +101 -0
- package/dist/context.js +76 -0
- package/dist/context.spec.cjs +244 -0
- package/dist/context.spec.d.ts +1 -0
- package/dist/context.spec.js +238 -0
- package/dist/date.cjs +53 -0
- package/dist/date.d.ts +24 -0
- package/dist/date.js +19 -0
- package/dist/date.spec.cjs +97 -0
- package/dist/date.spec.d.ts +1 -0
- package/dist/date.spec.js +91 -0
- package/dist/doc.cjs +54 -0
- package/dist/doc.d.ts +25 -0
- package/dist/doc.js +17 -0
- package/dist/doc.spec.cjs +99 -0
- package/dist/doc.spec.d.ts +1 -0
- package/dist/doc.spec.js +93 -0
- package/dist/enumeration.cjs +74 -0
- package/dist/enumeration.d.ts +50 -0
- package/dist/enumeration.js +40 -0
- package/dist/enumeration.spec.cjs +110 -0
- package/dist/enumeration.spec.d.ts +1 -0
- package/dist/enumeration.spec.js +104 -0
- package/dist/hydrate.cjs +18 -0
- package/dist/hydrate.d.ts +1 -0
- package/dist/hydrate.js +0 -0
- package/dist/index.cjs +145 -0
- package/dist/index.d.ts +34 -0
- package/dist/index.js +24 -0
- package/dist/index.spec.cjs +43 -0
- package/dist/index.spec.d.ts +1 -0
- package/dist/index.spec.js +37 -0
- package/dist/literal.cjs +55 -0
- package/dist/literal.d.ts +25 -0
- package/dist/literal.js +21 -0
- package/dist/literal.spec.cjs +93 -0
- package/dist/literal.spec.d.ts +1 -0
- package/dist/literal.spec.js +87 -0
- package/dist/number.cjs +89 -0
- package/dist/number.d.ts +84 -0
- package/dist/number.js +55 -0
- package/dist/number.spec.cjs +155 -0
- package/dist/number.spec.d.ts +1 -0
- package/dist/number.spec.js +149 -0
- package/dist/object.cjs +66 -0
- package/dist/object.d.ts +37 -0
- package/dist/object.js +32 -0
- package/dist/object.spec.cjs +171 -0
- package/dist/object.spec.d.ts +1 -0
- package/dist/object.spec.js +165 -0
- package/dist/operation.cjs +182 -0
- package/dist/operation.d.ts +197 -0
- package/dist/operation.js +133 -0
- package/dist/operation.spec.cjs +454 -0
- package/dist/operation.spec.d.ts +1 -0
- package/dist/operation.spec.js +448 -0
- package/dist/or.cjs +85 -0
- package/dist/or.d.ts +37 -0
- package/dist/or.js +51 -0
- package/dist/or.spec.cjs +204 -0
- package/dist/or.spec.d.ts +1 -0
- package/dist/or.spec.js +198 -0
- package/dist/schema.cjs +285 -0
- package/dist/schema.d.ts +132 -0
- package/dist/schema.js +233 -0
- package/dist/schema.spec.cjs +587 -0
- package/dist/schema.spec.d.ts +1 -0
- package/dist/schema.spec.js +581 -0
- package/dist/schemaFactory.cjs +125 -0
- package/dist/schemaFactory.d.ts +97 -0
- package/dist/schemaFactory.js +88 -0
- package/dist/schemaFactory.spec.cjs +197 -0
- package/dist/schemaFactory.spec.d.ts +1 -0
- package/dist/schemaFactory.spec.js +191 -0
- package/dist/schemaModule.cjs +280 -0
- package/dist/schemaModule.d.ts +97 -0
- package/dist/schemaModule.js +243 -0
- package/dist/schemaModule.spec.cjs +355 -0
- package/dist/schemaModule.spec.d.ts +1 -0
- package/dist/schemaModule.spec.js +349 -0
- package/dist/string.cjs +93 -0
- package/dist/string.d.ts +85 -0
- package/dist/string.js +59 -0
- package/dist/string.spec.cjs +158 -0
- package/dist/string.spec.d.ts +1 -0
- package/dist/string.spec.js +152 -0
- package/dist/testing/mocks/assertions.mock.cjs +48 -0
- package/dist/testing/mocks/assertions.mock.d.ts +5 -0
- package/dist/testing/mocks/assertions.mock.js +14 -0
- package/dist/testing/mocks/index.cjs +52 -0
- package/dist/testing/mocks/index.d.ts +4 -0
- package/dist/testing/mocks/index.js +3 -0
- package/dist/testing/mocks/schema.mock.cjs +120 -0
- package/dist/testing/mocks/schema.mock.d.ts +37 -0
- package/dist/testing/mocks/schema.mock.js +74 -0
- package/dist/tuple.cjs +58 -0
- package/dist/tuple.d.ts +33 -0
- package/dist/tuple.js +24 -0
- package/dist/tuple.spec.cjs +162 -0
- package/dist/tuple.spec.d.ts +1 -0
- package/dist/tuple.spec.js +156 -0
- package/dist/typeGuards.cjs +60 -0
- package/dist/typeGuards.d.ts +93 -0
- package/dist/typeGuards.js +8 -0
- package/dist/typeGuards.spec.cjs +101 -0
- package/dist/typeGuards.spec.d.ts +1 -0
- package/dist/typeGuards.spec.js +95 -0
- package/dist/types.cjs +18 -0
- package/dist/types.d.ts +289 -0
- package/dist/types.js +0 -0
- package/dist/union.cjs +74 -0
- package/dist/union.d.ts +33 -0
- package/dist/union.js +40 -0
- package/dist/union.spec.cjs +159 -0
- package/dist/union.spec.d.ts +1 -0
- package/dist/union.spec.js +153 -0
- package/package.json +47 -0
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __webpack_exports__ = {};
|
|
3
|
+
const external_vitest_namespaceObject = require("vitest");
|
|
4
|
+
const external_schemaFactory_cjs_namespaceObject = require("./schemaFactory.cjs");
|
|
5
|
+
const external_union_cjs_namespaceObject = require("./union.cjs");
|
|
6
|
+
const index_cjs_namespaceObject = require("./testing/mocks/index.cjs");
|
|
7
|
+
external_vitest_namespaceObject.vi.mock('./schemaFactory.js', ()=>({
|
|
8
|
+
schemaFactory: external_vitest_namespaceObject.vi.fn()
|
|
9
|
+
}));
|
|
10
|
+
const schemaFactoryMock = external_vitest_namespaceObject.vi.mocked(external_schemaFactory_cjs_namespaceObject.schemaFactory);
|
|
11
|
+
const schemaFactoryImplementation = (0, index_cjs_namespaceObject.createSchemaFactoryMock)();
|
|
12
|
+
const buildContextMock = {
|
|
13
|
+
buildId: 'build-union',
|
|
14
|
+
builder: {
|
|
15
|
+
add: external_vitest_namespaceObject.vi.fn((node)=>node),
|
|
16
|
+
getAll: external_vitest_namespaceObject.vi.fn(()=>[])
|
|
17
|
+
},
|
|
18
|
+
schemaPath: [],
|
|
19
|
+
buildOptions: {}
|
|
20
|
+
};
|
|
21
|
+
const schemaContextMock = (0, index_cjs_namespaceObject.createSchemaContextMock)({
|
|
22
|
+
buildContext: buildContextMock
|
|
23
|
+
});
|
|
24
|
+
const firstOptionMock = (0, index_cjs_namespaceObject.createBaseSchemaMock)({
|
|
25
|
+
name: 'firstOption',
|
|
26
|
+
parse: external_vitest_namespaceObject.vi.fn(()=>{
|
|
27
|
+
throw {
|
|
28
|
+
code: 'first.fail'
|
|
29
|
+
};
|
|
30
|
+
}),
|
|
31
|
+
ast: external_vitest_namespaceObject.vi.fn(()=>({
|
|
32
|
+
type: 'string',
|
|
33
|
+
name: 'FirstOption'
|
|
34
|
+
}))
|
|
35
|
+
});
|
|
36
|
+
const secondOptionMock = (0, index_cjs_namespaceObject.createBaseSchemaMock)({
|
|
37
|
+
name: 'secondOption',
|
|
38
|
+
parse: external_vitest_namespaceObject.vi.fn(()=>2),
|
|
39
|
+
ast: external_vitest_namespaceObject.vi.fn(()=>({
|
|
40
|
+
type: 'number',
|
|
41
|
+
name: 'SecondOption'
|
|
42
|
+
}))
|
|
43
|
+
});
|
|
44
|
+
const getFactoryInput = ()=>{
|
|
45
|
+
const call = schemaFactoryMock.mock.calls[0];
|
|
46
|
+
if (!call) throw new Error('schemaFactory should be called once before reading input');
|
|
47
|
+
return call[0];
|
|
48
|
+
};
|
|
49
|
+
(0, external_vitest_namespaceObject.describe)('union()', ()=>{
|
|
50
|
+
(0, external_vitest_namespaceObject.beforeAll)(()=>{
|
|
51
|
+
schemaFactoryMock.mockImplementation(schemaFactoryImplementation);
|
|
52
|
+
});
|
|
53
|
+
(0, external_vitest_namespaceObject.beforeEach)(()=>{
|
|
54
|
+
schemaFactoryMock.mockClear();
|
|
55
|
+
schemaFactoryImplementation.mockClear();
|
|
56
|
+
external_vitest_namespaceObject.vi.mocked(firstOptionMock.parse).mockReset();
|
|
57
|
+
external_vitest_namespaceObject.vi.mocked(firstOptionMock.parse).mockImplementation(()=>{
|
|
58
|
+
throw {
|
|
59
|
+
code: 'first.fail'
|
|
60
|
+
};
|
|
61
|
+
});
|
|
62
|
+
external_vitest_namespaceObject.vi.mocked(firstOptionMock.ast).mockClear();
|
|
63
|
+
external_vitest_namespaceObject.vi.mocked(secondOptionMock.parse).mockReset();
|
|
64
|
+
external_vitest_namespaceObject.vi.mocked(secondOptionMock.parse).mockReturnValue(2);
|
|
65
|
+
external_vitest_namespaceObject.vi.mocked(secondOptionMock.ast).mockClear();
|
|
66
|
+
});
|
|
67
|
+
(0, external_vitest_namespaceObject.afterEach)(()=>{
|
|
68
|
+
external_vitest_namespaceObject.vi.mocked(firstOptionMock.parse).mockClear();
|
|
69
|
+
external_vitest_namespaceObject.vi.mocked(secondOptionMock.parse).mockClear();
|
|
70
|
+
});
|
|
71
|
+
(0, external_vitest_namespaceObject.afterAll)(()=>{
|
|
72
|
+
schemaFactoryMock.mockReset();
|
|
73
|
+
schemaFactoryImplementation.mockReset();
|
|
74
|
+
});
|
|
75
|
+
(0, external_vitest_namespaceObject.describe)('happy', ()=>{
|
|
76
|
+
(0, external_vitest_namespaceObject.it)('should delegate to schemaFactory when union schema is created', ()=>{
|
|
77
|
+
const result = (0, external_union_cjs_namespaceObject.union)({
|
|
78
|
+
name: 'TextOrCount',
|
|
79
|
+
options: [
|
|
80
|
+
firstOptionMock,
|
|
81
|
+
secondOptionMock
|
|
82
|
+
]
|
|
83
|
+
});
|
|
84
|
+
(0, external_vitest_namespaceObject.expect)(schemaFactoryMock).toHaveBeenCalledTimes(1);
|
|
85
|
+
const factoryInput = getFactoryInput();
|
|
86
|
+
(0, external_vitest_namespaceObject.expect)(factoryInput.name).toBe('TextOrCount');
|
|
87
|
+
(0, external_vitest_namespaceObject.expect)(factoryInput.type).toBe('union');
|
|
88
|
+
(0, external_vitest_namespaceObject.expect)(result).toBe(schemaFactoryMock.mock.results[0]?.value);
|
|
89
|
+
});
|
|
90
|
+
(0, external_vitest_namespaceObject.it)('should build ast from option schemas when build context is available', ()=>{
|
|
91
|
+
(0, external_union_cjs_namespaceObject.union)({
|
|
92
|
+
name: 'TextOrCount',
|
|
93
|
+
options: [
|
|
94
|
+
firstOptionMock,
|
|
95
|
+
secondOptionMock
|
|
96
|
+
]
|
|
97
|
+
});
|
|
98
|
+
const factoryInput = getFactoryInput();
|
|
99
|
+
const ast = factoryInput.ast(schemaContextMock);
|
|
100
|
+
(0, external_vitest_namespaceObject.expect)(ast).toEqual({
|
|
101
|
+
type: 'union',
|
|
102
|
+
name: 'TextOrCount',
|
|
103
|
+
children: [
|
|
104
|
+
{
|
|
105
|
+
type: 'string',
|
|
106
|
+
name: 'FirstOption'
|
|
107
|
+
},
|
|
108
|
+
{
|
|
109
|
+
type: 'number',
|
|
110
|
+
name: 'SecondOption'
|
|
111
|
+
}
|
|
112
|
+
]
|
|
113
|
+
});
|
|
114
|
+
(0, external_vitest_namespaceObject.expect)(firstOptionMock.ast).toHaveBeenCalledWith(buildContextMock);
|
|
115
|
+
(0, external_vitest_namespaceObject.expect)(secondOptionMock.ast).toHaveBeenCalledWith(buildContextMock);
|
|
116
|
+
});
|
|
117
|
+
(0, external_vitest_namespaceObject.it)('should return first successful parse result when one option matches', ()=>{
|
|
118
|
+
(0, external_union_cjs_namespaceObject.union)({
|
|
119
|
+
name: 'TextOrCount',
|
|
120
|
+
options: [
|
|
121
|
+
firstOptionMock,
|
|
122
|
+
secondOptionMock
|
|
123
|
+
]
|
|
124
|
+
});
|
|
125
|
+
const factoryInput = getFactoryInput();
|
|
126
|
+
const parsed = factoryInput.validate('input', schemaContextMock);
|
|
127
|
+
(0, external_vitest_namespaceObject.expect)(parsed).toBe(2);
|
|
128
|
+
(0, external_vitest_namespaceObject.expect)(firstOptionMock.parse).toHaveBeenCalledWith('input', schemaContextMock);
|
|
129
|
+
(0, external_vitest_namespaceObject.expect)(secondOptionMock.parse).toHaveBeenCalledWith('input', schemaContextMock);
|
|
130
|
+
});
|
|
131
|
+
});
|
|
132
|
+
(0, external_vitest_namespaceObject.describe)('sad', ()=>{
|
|
133
|
+
(0, external_vitest_namespaceObject.it)('should throw union.match when no option matches', ()=>{
|
|
134
|
+
external_vitest_namespaceObject.vi.mocked(secondOptionMock.parse).mockImplementation(()=>{
|
|
135
|
+
throw {
|
|
136
|
+
code: 'second.fail'
|
|
137
|
+
};
|
|
138
|
+
});
|
|
139
|
+
(0, external_union_cjs_namespaceObject.union)({
|
|
140
|
+
name: 'TextOrCount',
|
|
141
|
+
options: [
|
|
142
|
+
firstOptionMock,
|
|
143
|
+
secondOptionMock
|
|
144
|
+
]
|
|
145
|
+
});
|
|
146
|
+
const factoryInput = getFactoryInput();
|
|
147
|
+
const thrown = (0, index_cjs_namespaceObject.captureThrow)(()=>factoryInput.validate('input', schemaContextMock));
|
|
148
|
+
(0, external_vitest_namespaceObject.expect)(thrown.threw).toBe(true);
|
|
149
|
+
(0, external_vitest_namespaceObject.expect)(thrown.value).toEqual({
|
|
150
|
+
message: 'No union match',
|
|
151
|
+
code: 'union.match'
|
|
152
|
+
});
|
|
153
|
+
});
|
|
154
|
+
});
|
|
155
|
+
});
|
|
156
|
+
for(var __rspack_i in __webpack_exports__)exports[__rspack_i] = __webpack_exports__[__rspack_i];
|
|
157
|
+
Object.defineProperty(exports, '__esModule', {
|
|
158
|
+
value: true
|
|
159
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
|
|
2
|
+
import { schemaFactory } from "./schemaFactory.js";
|
|
3
|
+
import { union } from "./union.js";
|
|
4
|
+
import { captureThrow, createBaseSchemaMock, createSchemaContextMock, createSchemaFactoryMock } from "./testing/mocks/index.js";
|
|
5
|
+
vi.mock('./schemaFactory.js', ()=>({
|
|
6
|
+
schemaFactory: vi.fn()
|
|
7
|
+
}));
|
|
8
|
+
const schemaFactoryMock = vi.mocked(schemaFactory);
|
|
9
|
+
const schemaFactoryImplementation = createSchemaFactoryMock();
|
|
10
|
+
const buildContextMock = {
|
|
11
|
+
buildId: 'build-union',
|
|
12
|
+
builder: {
|
|
13
|
+
add: vi.fn((node)=>node),
|
|
14
|
+
getAll: vi.fn(()=>[])
|
|
15
|
+
},
|
|
16
|
+
schemaPath: [],
|
|
17
|
+
buildOptions: {}
|
|
18
|
+
};
|
|
19
|
+
const schemaContextMock = createSchemaContextMock({
|
|
20
|
+
buildContext: buildContextMock
|
|
21
|
+
});
|
|
22
|
+
const firstOptionMock = createBaseSchemaMock({
|
|
23
|
+
name: 'firstOption',
|
|
24
|
+
parse: vi.fn(()=>{
|
|
25
|
+
throw {
|
|
26
|
+
code: 'first.fail'
|
|
27
|
+
};
|
|
28
|
+
}),
|
|
29
|
+
ast: vi.fn(()=>({
|
|
30
|
+
type: 'string',
|
|
31
|
+
name: 'FirstOption'
|
|
32
|
+
}))
|
|
33
|
+
});
|
|
34
|
+
const secondOptionMock = createBaseSchemaMock({
|
|
35
|
+
name: 'secondOption',
|
|
36
|
+
parse: vi.fn(()=>2),
|
|
37
|
+
ast: vi.fn(()=>({
|
|
38
|
+
type: 'number',
|
|
39
|
+
name: 'SecondOption'
|
|
40
|
+
}))
|
|
41
|
+
});
|
|
42
|
+
const getFactoryInput = ()=>{
|
|
43
|
+
const call = schemaFactoryMock.mock.calls[0];
|
|
44
|
+
if (!call) throw new Error('schemaFactory should be called once before reading input');
|
|
45
|
+
return call[0];
|
|
46
|
+
};
|
|
47
|
+
describe('union()', ()=>{
|
|
48
|
+
beforeAll(()=>{
|
|
49
|
+
schemaFactoryMock.mockImplementation(schemaFactoryImplementation);
|
|
50
|
+
});
|
|
51
|
+
beforeEach(()=>{
|
|
52
|
+
schemaFactoryMock.mockClear();
|
|
53
|
+
schemaFactoryImplementation.mockClear();
|
|
54
|
+
vi.mocked(firstOptionMock.parse).mockReset();
|
|
55
|
+
vi.mocked(firstOptionMock.parse).mockImplementation(()=>{
|
|
56
|
+
throw {
|
|
57
|
+
code: 'first.fail'
|
|
58
|
+
};
|
|
59
|
+
});
|
|
60
|
+
vi.mocked(firstOptionMock.ast).mockClear();
|
|
61
|
+
vi.mocked(secondOptionMock.parse).mockReset();
|
|
62
|
+
vi.mocked(secondOptionMock.parse).mockReturnValue(2);
|
|
63
|
+
vi.mocked(secondOptionMock.ast).mockClear();
|
|
64
|
+
});
|
|
65
|
+
afterEach(()=>{
|
|
66
|
+
vi.mocked(firstOptionMock.parse).mockClear();
|
|
67
|
+
vi.mocked(secondOptionMock.parse).mockClear();
|
|
68
|
+
});
|
|
69
|
+
afterAll(()=>{
|
|
70
|
+
schemaFactoryMock.mockReset();
|
|
71
|
+
schemaFactoryImplementation.mockReset();
|
|
72
|
+
});
|
|
73
|
+
describe('happy', ()=>{
|
|
74
|
+
it('should delegate to schemaFactory when union schema is created', ()=>{
|
|
75
|
+
const result = union({
|
|
76
|
+
name: 'TextOrCount',
|
|
77
|
+
options: [
|
|
78
|
+
firstOptionMock,
|
|
79
|
+
secondOptionMock
|
|
80
|
+
]
|
|
81
|
+
});
|
|
82
|
+
expect(schemaFactoryMock).toHaveBeenCalledTimes(1);
|
|
83
|
+
const factoryInput = getFactoryInput();
|
|
84
|
+
expect(factoryInput.name).toBe('TextOrCount');
|
|
85
|
+
expect(factoryInput.type).toBe('union');
|
|
86
|
+
expect(result).toBe(schemaFactoryMock.mock.results[0]?.value);
|
|
87
|
+
});
|
|
88
|
+
it('should build ast from option schemas when build context is available', ()=>{
|
|
89
|
+
union({
|
|
90
|
+
name: 'TextOrCount',
|
|
91
|
+
options: [
|
|
92
|
+
firstOptionMock,
|
|
93
|
+
secondOptionMock
|
|
94
|
+
]
|
|
95
|
+
});
|
|
96
|
+
const factoryInput = getFactoryInput();
|
|
97
|
+
const ast = factoryInput.ast(schemaContextMock);
|
|
98
|
+
expect(ast).toEqual({
|
|
99
|
+
type: 'union',
|
|
100
|
+
name: 'TextOrCount',
|
|
101
|
+
children: [
|
|
102
|
+
{
|
|
103
|
+
type: 'string',
|
|
104
|
+
name: 'FirstOption'
|
|
105
|
+
},
|
|
106
|
+
{
|
|
107
|
+
type: 'number',
|
|
108
|
+
name: 'SecondOption'
|
|
109
|
+
}
|
|
110
|
+
]
|
|
111
|
+
});
|
|
112
|
+
expect(firstOptionMock.ast).toHaveBeenCalledWith(buildContextMock);
|
|
113
|
+
expect(secondOptionMock.ast).toHaveBeenCalledWith(buildContextMock);
|
|
114
|
+
});
|
|
115
|
+
it('should return first successful parse result when one option matches', ()=>{
|
|
116
|
+
union({
|
|
117
|
+
name: 'TextOrCount',
|
|
118
|
+
options: [
|
|
119
|
+
firstOptionMock,
|
|
120
|
+
secondOptionMock
|
|
121
|
+
]
|
|
122
|
+
});
|
|
123
|
+
const factoryInput = getFactoryInput();
|
|
124
|
+
const parsed = factoryInput.validate('input', schemaContextMock);
|
|
125
|
+
expect(parsed).toBe(2);
|
|
126
|
+
expect(firstOptionMock.parse).toHaveBeenCalledWith('input', schemaContextMock);
|
|
127
|
+
expect(secondOptionMock.parse).toHaveBeenCalledWith('input', schemaContextMock);
|
|
128
|
+
});
|
|
129
|
+
});
|
|
130
|
+
describe('sad', ()=>{
|
|
131
|
+
it('should throw union.match when no option matches', ()=>{
|
|
132
|
+
vi.mocked(secondOptionMock.parse).mockImplementation(()=>{
|
|
133
|
+
throw {
|
|
134
|
+
code: 'second.fail'
|
|
135
|
+
};
|
|
136
|
+
});
|
|
137
|
+
union({
|
|
138
|
+
name: 'TextOrCount',
|
|
139
|
+
options: [
|
|
140
|
+
firstOptionMock,
|
|
141
|
+
secondOptionMock
|
|
142
|
+
]
|
|
143
|
+
});
|
|
144
|
+
const factoryInput = getFactoryInput();
|
|
145
|
+
const thrown = captureThrow(()=>factoryInput.validate('input', schemaContextMock));
|
|
146
|
+
expect(thrown.threw).toBe(true);
|
|
147
|
+
expect(thrown.value).toEqual({
|
|
148
|
+
message: 'No union match',
|
|
149
|
+
code: 'union.match'
|
|
150
|
+
});
|
|
151
|
+
});
|
|
152
|
+
});
|
|
153
|
+
});
|
package/package.json
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@livon/schema",
|
|
3
|
+
"version": "0.27.0-rc.1",
|
|
4
|
+
"private": false,
|
|
5
|
+
"type": "module",
|
|
6
|
+
"publishConfig": {
|
|
7
|
+
"access": "public"
|
|
8
|
+
},
|
|
9
|
+
"files": [
|
|
10
|
+
"dist",
|
|
11
|
+
"README.md",
|
|
12
|
+
"PROMPT.md",
|
|
13
|
+
"SCHEMA.md"
|
|
14
|
+
],
|
|
15
|
+
"exports": {
|
|
16
|
+
".": {
|
|
17
|
+
"development": "./src/index.ts",
|
|
18
|
+
"import": "./dist/index.js",
|
|
19
|
+
"require": "./dist/index.cjs",
|
|
20
|
+
"types": "./dist/index.d.ts"
|
|
21
|
+
}
|
|
22
|
+
},
|
|
23
|
+
"dependencies": {
|
|
24
|
+
"msgpackr": "latest",
|
|
25
|
+
"@livon/config": "0.27.0-rc.1",
|
|
26
|
+
"@livon/runtime": "0.27.0-rc.1"
|
|
27
|
+
},
|
|
28
|
+
"devDependencies": {
|
|
29
|
+
"@rslib/core": "latest",
|
|
30
|
+
"@typescript-eslint/eslint-plugin": "8.54.0",
|
|
31
|
+
"@typescript-eslint/parser": "8.54.0",
|
|
32
|
+
"eslint": "9.0.0",
|
|
33
|
+
"typescript": "latest",
|
|
34
|
+
"vitest": "latest",
|
|
35
|
+
"@livon/config": "0.27.0-rc.1"
|
|
36
|
+
},
|
|
37
|
+
"scripts": {
|
|
38
|
+
"build": "rslib build",
|
|
39
|
+
"build:watch": "rslib dev",
|
|
40
|
+
"dev": "true",
|
|
41
|
+
"lint": "eslint .",
|
|
42
|
+
"typecheck": "tsc -p tsconfig.json --noEmit",
|
|
43
|
+
"test": "vitest run -c vitest.unit.config.ts",
|
|
44
|
+
"test:unit": "vitest run -c vitest.unit.config.ts",
|
|
45
|
+
"test:integration": "vitest run -c vitest.integration.config.ts"
|
|
46
|
+
}
|
|
47
|
+
}
|