@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,167 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __webpack_exports__ = {};
|
|
3
|
+
const external_vitest_namespaceObject = require("vitest");
|
|
4
|
+
const external_array_cjs_namespaceObject = require("./array.cjs");
|
|
5
|
+
const external_schemaFactory_cjs_namespaceObject = require("./schemaFactory.cjs");
|
|
6
|
+
const external_typeGuards_cjs_namespaceObject = require("./typeGuards.cjs");
|
|
7
|
+
const index_cjs_namespaceObject = require("./testing/mocks/index.cjs");
|
|
8
|
+
external_vitest_namespaceObject.vi.mock('./schemaFactory.js', ()=>({
|
|
9
|
+
schemaFactory: external_vitest_namespaceObject.vi.fn()
|
|
10
|
+
}));
|
|
11
|
+
external_vitest_namespaceObject.vi.mock('./typeGuards.js', ()=>({
|
|
12
|
+
isArray: external_vitest_namespaceObject.vi.fn()
|
|
13
|
+
}));
|
|
14
|
+
const schemaFactoryMock = external_vitest_namespaceObject.vi.mocked(external_schemaFactory_cjs_namespaceObject.schemaFactory);
|
|
15
|
+
const isArrayMock = external_vitest_namespaceObject.vi.mocked(external_typeGuards_cjs_namespaceObject.isArray);
|
|
16
|
+
const schemaFactoryImplementation = (0, index_cjs_namespaceObject.createSchemaFactoryMock)();
|
|
17
|
+
const isArrayGuardSpy = external_vitest_namespaceObject.vi.fn((input)=>Array.isArray(input));
|
|
18
|
+
const isArrayGuard = (input)=>isArrayGuardSpy(input);
|
|
19
|
+
const buildContextMock = {
|
|
20
|
+
buildId: 'build-array',
|
|
21
|
+
builder: {
|
|
22
|
+
add: external_vitest_namespaceObject.vi.fn((node)=>node),
|
|
23
|
+
getAll: external_vitest_namespaceObject.vi.fn(()=>[])
|
|
24
|
+
},
|
|
25
|
+
schemaPath: [],
|
|
26
|
+
buildOptions: {}
|
|
27
|
+
};
|
|
28
|
+
const schemaContextMock = (0, index_cjs_namespaceObject.createSchemaContextMock)({
|
|
29
|
+
buildContext: buildContextMock
|
|
30
|
+
});
|
|
31
|
+
const itemSchemaMock = (0, index_cjs_namespaceObject.createBaseSchemaMock)({
|
|
32
|
+
name: 'itemSchema',
|
|
33
|
+
parse: external_vitest_namespaceObject.vi.fn((input)=>`parsed:${String(input)}`),
|
|
34
|
+
ast: external_vitest_namespaceObject.vi.fn(()=>({
|
|
35
|
+
type: 'string',
|
|
36
|
+
name: 'Item'
|
|
37
|
+
}))
|
|
38
|
+
});
|
|
39
|
+
const getFactoryInput = ()=>{
|
|
40
|
+
const call = schemaFactoryMock.mock.calls[0];
|
|
41
|
+
if (!call) throw new Error('schemaFactory should be called once before reading input');
|
|
42
|
+
return call[0];
|
|
43
|
+
};
|
|
44
|
+
(0, external_vitest_namespaceObject.describe)('array()', ()=>{
|
|
45
|
+
(0, external_vitest_namespaceObject.beforeAll)(()=>{
|
|
46
|
+
schemaFactoryMock.mockImplementation(schemaFactoryImplementation);
|
|
47
|
+
isArrayMock.mockImplementation(()=>isArrayGuard);
|
|
48
|
+
});
|
|
49
|
+
(0, external_vitest_namespaceObject.beforeEach)(()=>{
|
|
50
|
+
schemaFactoryMock.mockClear();
|
|
51
|
+
schemaFactoryImplementation.mockClear();
|
|
52
|
+
isArrayMock.mockClear();
|
|
53
|
+
isArrayGuardSpy.mockClear();
|
|
54
|
+
external_vitest_namespaceObject.vi.mocked(itemSchemaMock.parse).mockClear();
|
|
55
|
+
external_vitest_namespaceObject.vi.mocked(itemSchemaMock.ast).mockClear();
|
|
56
|
+
});
|
|
57
|
+
(0, external_vitest_namespaceObject.afterEach)(()=>{
|
|
58
|
+
external_vitest_namespaceObject.vi.mocked(itemSchemaMock.parse).mockClear();
|
|
59
|
+
isArrayGuardSpy.mockClear();
|
|
60
|
+
});
|
|
61
|
+
(0, external_vitest_namespaceObject.afterAll)(()=>{
|
|
62
|
+
schemaFactoryMock.mockReset();
|
|
63
|
+
schemaFactoryImplementation.mockReset();
|
|
64
|
+
isArrayMock.mockReset();
|
|
65
|
+
});
|
|
66
|
+
(0, external_vitest_namespaceObject.describe)('happy', ()=>{
|
|
67
|
+
(0, external_vitest_namespaceObject.it)('should delegate to schemaFactory when array schema is created', ()=>{
|
|
68
|
+
const result = (0, external_array_cjs_namespaceObject.array)({
|
|
69
|
+
name: 'Tags',
|
|
70
|
+
item: itemSchemaMock
|
|
71
|
+
});
|
|
72
|
+
(0, external_vitest_namespaceObject.expect)(schemaFactoryMock).toHaveBeenCalledTimes(1);
|
|
73
|
+
const factoryInput = getFactoryInput();
|
|
74
|
+
(0, external_vitest_namespaceObject.expect)(factoryInput.name).toBe('Tags');
|
|
75
|
+
(0, external_vitest_namespaceObject.expect)(factoryInput.type).toBe('array<itemSchema>');
|
|
76
|
+
(0, external_vitest_namespaceObject.expect)(result).toBe(schemaFactoryMock.mock.results[0]?.value);
|
|
77
|
+
});
|
|
78
|
+
(0, external_vitest_namespaceObject.it)('should build ast from item schema when build context is available', ()=>{
|
|
79
|
+
(0, external_array_cjs_namespaceObject.array)({
|
|
80
|
+
name: 'Tags',
|
|
81
|
+
item: itemSchemaMock
|
|
82
|
+
});
|
|
83
|
+
const factoryInput = getFactoryInput();
|
|
84
|
+
const ast = factoryInput.ast(schemaContextMock);
|
|
85
|
+
(0, external_vitest_namespaceObject.expect)(ast).toEqual({
|
|
86
|
+
type: 'array',
|
|
87
|
+
name: 'Tags',
|
|
88
|
+
children: [
|
|
89
|
+
{
|
|
90
|
+
type: 'string',
|
|
91
|
+
name: 'Item'
|
|
92
|
+
}
|
|
93
|
+
]
|
|
94
|
+
});
|
|
95
|
+
(0, external_vitest_namespaceObject.expect)(itemSchemaMock.ast).toHaveBeenCalledWith(buildContextMock);
|
|
96
|
+
});
|
|
97
|
+
(0, external_vitest_namespaceObject.it)('should validate all items when input is an array', ()=>{
|
|
98
|
+
(0, external_array_cjs_namespaceObject.array)({
|
|
99
|
+
name: 'Tags',
|
|
100
|
+
item: itemSchemaMock
|
|
101
|
+
});
|
|
102
|
+
const factoryInput = getFactoryInput();
|
|
103
|
+
const result = factoryInput.validate([
|
|
104
|
+
'a',
|
|
105
|
+
'b'
|
|
106
|
+
], schemaContextMock);
|
|
107
|
+
(0, external_vitest_namespaceObject.expect)(result).toEqual([
|
|
108
|
+
'parsed:a',
|
|
109
|
+
'parsed:b'
|
|
110
|
+
]);
|
|
111
|
+
(0, external_vitest_namespaceObject.expect)(isArrayMock).toHaveBeenCalledTimes(1);
|
|
112
|
+
(0, external_vitest_namespaceObject.expect)(isArrayGuardSpy).toHaveBeenCalledWith([
|
|
113
|
+
'a',
|
|
114
|
+
'b'
|
|
115
|
+
]);
|
|
116
|
+
(0, external_vitest_namespaceObject.expect)(itemSchemaMock.parse).toHaveBeenNthCalledWith(1, 'a', schemaContextMock);
|
|
117
|
+
(0, external_vitest_namespaceObject.expect)(itemSchemaMock.parse).toHaveBeenNthCalledWith(2, 'b', schemaContextMock);
|
|
118
|
+
});
|
|
119
|
+
});
|
|
120
|
+
(0, external_vitest_namespaceObject.describe)('sad', ()=>{
|
|
121
|
+
(0, external_vitest_namespaceObject.it)('should throw array.type when input is not an array', ()=>{
|
|
122
|
+
isArrayGuardSpy.mockReturnValueOnce(false);
|
|
123
|
+
(0, external_array_cjs_namespaceObject.array)({
|
|
124
|
+
name: 'Tags',
|
|
125
|
+
item: itemSchemaMock
|
|
126
|
+
});
|
|
127
|
+
const factoryInput = getFactoryInput();
|
|
128
|
+
const thrown = (0, index_cjs_namespaceObject.captureThrow)(()=>factoryInput.validate('x', schemaContextMock));
|
|
129
|
+
(0, external_vitest_namespaceObject.expect)(thrown.threw).toBe(true);
|
|
130
|
+
(0, external_vitest_namespaceObject.expect)(thrown.value).toEqual({
|
|
131
|
+
message: 'Expected array',
|
|
132
|
+
code: 'array.type'
|
|
133
|
+
});
|
|
134
|
+
});
|
|
135
|
+
(0, external_vitest_namespaceObject.it)('should throw array.item with index context when item parse fails', ()=>{
|
|
136
|
+
const itemFailure = {
|
|
137
|
+
message: 'item failed',
|
|
138
|
+
code: 'item.invalid'
|
|
139
|
+
};
|
|
140
|
+
external_vitest_namespaceObject.vi.mocked(itemSchemaMock.parse).mockReturnValueOnce('parsed:a').mockImplementationOnce(()=>{
|
|
141
|
+
throw itemFailure;
|
|
142
|
+
});
|
|
143
|
+
(0, external_array_cjs_namespaceObject.array)({
|
|
144
|
+
name: 'Tags',
|
|
145
|
+
item: itemSchemaMock
|
|
146
|
+
});
|
|
147
|
+
const factoryInput = getFactoryInput();
|
|
148
|
+
const thrown = (0, index_cjs_namespaceObject.captureThrow)(()=>factoryInput.validate([
|
|
149
|
+
'a',
|
|
150
|
+
'b'
|
|
151
|
+
], schemaContextMock));
|
|
152
|
+
(0, external_vitest_namespaceObject.expect)(thrown.threw).toBe(true);
|
|
153
|
+
(0, external_vitest_namespaceObject.expect)(thrown.value).toEqual({
|
|
154
|
+
message: 'Invalid item at index 1',
|
|
155
|
+
code: 'array.item',
|
|
156
|
+
context: {
|
|
157
|
+
index: 1,
|
|
158
|
+
error: itemFailure
|
|
159
|
+
}
|
|
160
|
+
});
|
|
161
|
+
});
|
|
162
|
+
});
|
|
163
|
+
});
|
|
164
|
+
for(var __rspack_i in __webpack_exports__)exports[__rspack_i] = __webpack_exports__[__rspack_i];
|
|
165
|
+
Object.defineProperty(exports, '__esModule', {
|
|
166
|
+
value: true
|
|
167
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
|
|
2
|
+
import { array } from "./array.js";
|
|
3
|
+
import { schemaFactory } from "./schemaFactory.js";
|
|
4
|
+
import { isArray } from "./typeGuards.js";
|
|
5
|
+
import { captureThrow, createBaseSchemaMock, createSchemaContextMock, createSchemaFactoryMock } from "./testing/mocks/index.js";
|
|
6
|
+
vi.mock('./schemaFactory.js', ()=>({
|
|
7
|
+
schemaFactory: vi.fn()
|
|
8
|
+
}));
|
|
9
|
+
vi.mock('./typeGuards.js', ()=>({
|
|
10
|
+
isArray: vi.fn()
|
|
11
|
+
}));
|
|
12
|
+
const schemaFactoryMock = vi.mocked(schemaFactory);
|
|
13
|
+
const isArrayMock = vi.mocked(isArray);
|
|
14
|
+
const schemaFactoryImplementation = createSchemaFactoryMock();
|
|
15
|
+
const isArrayGuardSpy = vi.fn((input)=>Array.isArray(input));
|
|
16
|
+
const isArrayGuard = (input)=>isArrayGuardSpy(input);
|
|
17
|
+
const buildContextMock = {
|
|
18
|
+
buildId: 'build-array',
|
|
19
|
+
builder: {
|
|
20
|
+
add: vi.fn((node)=>node),
|
|
21
|
+
getAll: vi.fn(()=>[])
|
|
22
|
+
},
|
|
23
|
+
schemaPath: [],
|
|
24
|
+
buildOptions: {}
|
|
25
|
+
};
|
|
26
|
+
const schemaContextMock = createSchemaContextMock({
|
|
27
|
+
buildContext: buildContextMock
|
|
28
|
+
});
|
|
29
|
+
const itemSchemaMock = createBaseSchemaMock({
|
|
30
|
+
name: 'itemSchema',
|
|
31
|
+
parse: vi.fn((input)=>`parsed:${String(input)}`),
|
|
32
|
+
ast: vi.fn(()=>({
|
|
33
|
+
type: 'string',
|
|
34
|
+
name: 'Item'
|
|
35
|
+
}))
|
|
36
|
+
});
|
|
37
|
+
const getFactoryInput = ()=>{
|
|
38
|
+
const call = schemaFactoryMock.mock.calls[0];
|
|
39
|
+
if (!call) throw new Error('schemaFactory should be called once before reading input');
|
|
40
|
+
return call[0];
|
|
41
|
+
};
|
|
42
|
+
describe('array()', ()=>{
|
|
43
|
+
beforeAll(()=>{
|
|
44
|
+
schemaFactoryMock.mockImplementation(schemaFactoryImplementation);
|
|
45
|
+
isArrayMock.mockImplementation(()=>isArrayGuard);
|
|
46
|
+
});
|
|
47
|
+
beforeEach(()=>{
|
|
48
|
+
schemaFactoryMock.mockClear();
|
|
49
|
+
schemaFactoryImplementation.mockClear();
|
|
50
|
+
isArrayMock.mockClear();
|
|
51
|
+
isArrayGuardSpy.mockClear();
|
|
52
|
+
vi.mocked(itemSchemaMock.parse).mockClear();
|
|
53
|
+
vi.mocked(itemSchemaMock.ast).mockClear();
|
|
54
|
+
});
|
|
55
|
+
afterEach(()=>{
|
|
56
|
+
vi.mocked(itemSchemaMock.parse).mockClear();
|
|
57
|
+
isArrayGuardSpy.mockClear();
|
|
58
|
+
});
|
|
59
|
+
afterAll(()=>{
|
|
60
|
+
schemaFactoryMock.mockReset();
|
|
61
|
+
schemaFactoryImplementation.mockReset();
|
|
62
|
+
isArrayMock.mockReset();
|
|
63
|
+
});
|
|
64
|
+
describe('happy', ()=>{
|
|
65
|
+
it('should delegate to schemaFactory when array schema is created', ()=>{
|
|
66
|
+
const result = array({
|
|
67
|
+
name: 'Tags',
|
|
68
|
+
item: itemSchemaMock
|
|
69
|
+
});
|
|
70
|
+
expect(schemaFactoryMock).toHaveBeenCalledTimes(1);
|
|
71
|
+
const factoryInput = getFactoryInput();
|
|
72
|
+
expect(factoryInput.name).toBe('Tags');
|
|
73
|
+
expect(factoryInput.type).toBe('array<itemSchema>');
|
|
74
|
+
expect(result).toBe(schemaFactoryMock.mock.results[0]?.value);
|
|
75
|
+
});
|
|
76
|
+
it('should build ast from item schema when build context is available', ()=>{
|
|
77
|
+
array({
|
|
78
|
+
name: 'Tags',
|
|
79
|
+
item: itemSchemaMock
|
|
80
|
+
});
|
|
81
|
+
const factoryInput = getFactoryInput();
|
|
82
|
+
const ast = factoryInput.ast(schemaContextMock);
|
|
83
|
+
expect(ast).toEqual({
|
|
84
|
+
type: 'array',
|
|
85
|
+
name: 'Tags',
|
|
86
|
+
children: [
|
|
87
|
+
{
|
|
88
|
+
type: 'string',
|
|
89
|
+
name: 'Item'
|
|
90
|
+
}
|
|
91
|
+
]
|
|
92
|
+
});
|
|
93
|
+
expect(itemSchemaMock.ast).toHaveBeenCalledWith(buildContextMock);
|
|
94
|
+
});
|
|
95
|
+
it('should validate all items when input is an array', ()=>{
|
|
96
|
+
array({
|
|
97
|
+
name: 'Tags',
|
|
98
|
+
item: itemSchemaMock
|
|
99
|
+
});
|
|
100
|
+
const factoryInput = getFactoryInput();
|
|
101
|
+
const result = factoryInput.validate([
|
|
102
|
+
'a',
|
|
103
|
+
'b'
|
|
104
|
+
], schemaContextMock);
|
|
105
|
+
expect(result).toEqual([
|
|
106
|
+
'parsed:a',
|
|
107
|
+
'parsed:b'
|
|
108
|
+
]);
|
|
109
|
+
expect(isArrayMock).toHaveBeenCalledTimes(1);
|
|
110
|
+
expect(isArrayGuardSpy).toHaveBeenCalledWith([
|
|
111
|
+
'a',
|
|
112
|
+
'b'
|
|
113
|
+
]);
|
|
114
|
+
expect(itemSchemaMock.parse).toHaveBeenNthCalledWith(1, 'a', schemaContextMock);
|
|
115
|
+
expect(itemSchemaMock.parse).toHaveBeenNthCalledWith(2, 'b', schemaContextMock);
|
|
116
|
+
});
|
|
117
|
+
});
|
|
118
|
+
describe('sad', ()=>{
|
|
119
|
+
it('should throw array.type when input is not an array', ()=>{
|
|
120
|
+
isArrayGuardSpy.mockReturnValueOnce(false);
|
|
121
|
+
array({
|
|
122
|
+
name: 'Tags',
|
|
123
|
+
item: itemSchemaMock
|
|
124
|
+
});
|
|
125
|
+
const factoryInput = getFactoryInput();
|
|
126
|
+
const thrown = captureThrow(()=>factoryInput.validate('x', schemaContextMock));
|
|
127
|
+
expect(thrown.threw).toBe(true);
|
|
128
|
+
expect(thrown.value).toEqual({
|
|
129
|
+
message: 'Expected array',
|
|
130
|
+
code: 'array.type'
|
|
131
|
+
});
|
|
132
|
+
});
|
|
133
|
+
it('should throw array.item with index context when item parse fails', ()=>{
|
|
134
|
+
const itemFailure = {
|
|
135
|
+
message: 'item failed',
|
|
136
|
+
code: 'item.invalid'
|
|
137
|
+
};
|
|
138
|
+
vi.mocked(itemSchemaMock.parse).mockReturnValueOnce('parsed:a').mockImplementationOnce(()=>{
|
|
139
|
+
throw itemFailure;
|
|
140
|
+
});
|
|
141
|
+
array({
|
|
142
|
+
name: 'Tags',
|
|
143
|
+
item: itemSchemaMock
|
|
144
|
+
});
|
|
145
|
+
const factoryInput = getFactoryInput();
|
|
146
|
+
const thrown = captureThrow(()=>factoryInput.validate([
|
|
147
|
+
'a',
|
|
148
|
+
'b'
|
|
149
|
+
], schemaContextMock));
|
|
150
|
+
expect(thrown.threw).toBe(true);
|
|
151
|
+
expect(thrown.value).toEqual({
|
|
152
|
+
message: 'Invalid item at index 1',
|
|
153
|
+
code: 'array.item',
|
|
154
|
+
context: {
|
|
155
|
+
index: 1,
|
|
156
|
+
error: itemFailure
|
|
157
|
+
}
|
|
158
|
+
});
|
|
159
|
+
});
|
|
160
|
+
});
|
|
161
|
+
});
|
package/dist/before.cjs
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
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
|
+
before: ()=>before
|
|
28
|
+
});
|
|
29
|
+
const before = ({ schema, hook })=>schema.before(hook);
|
|
30
|
+
exports.before = __webpack_exports__.before;
|
|
31
|
+
for(var __rspack_i in __webpack_exports__)if (-1 === [
|
|
32
|
+
"before"
|
|
33
|
+
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
|
|
34
|
+
Object.defineProperty(exports, '__esModule', {
|
|
35
|
+
value: true
|
|
36
|
+
});
|
package/dist/before.d.ts
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { Schema } from './types.js';
|
|
2
|
+
export interface BeforeInput<T> {
|
|
3
|
+
schema: Schema<T>;
|
|
4
|
+
hook: Parameters<Schema<T>['before']>[0];
|
|
5
|
+
}
|
|
6
|
+
/**
|
|
7
|
+
* before is part of the public LIVON API.
|
|
8
|
+
*
|
|
9
|
+
* @remarks
|
|
10
|
+
* Parameter and return types are defined in the TypeScript signature.
|
|
11
|
+
*
|
|
12
|
+
* @see https://live-input-vector-output-node.github.io/livon-ts/docs/schema/before
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* // Preprocesses string input by trimming whitespace before validation.
|
|
16
|
+
* const TrimmedName = before({
|
|
17
|
+
* schema: string(),
|
|
18
|
+
* hook: (input) => (typeof input === 'string' ? input.trim() : input),
|
|
19
|
+
* });
|
|
20
|
+
* TrimmedName.parse(' alice ');
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* // Extends the preprocessed schema to also allow undefined.
|
|
24
|
+
* const OptionalTrimmedName = before({
|
|
25
|
+
* schema: string(),
|
|
26
|
+
* hook: (input) => (typeof input === 'string' ? input.trim() : input),
|
|
27
|
+
* }).optional();
|
|
28
|
+
* OptionalTrimmedName.parse(undefined);
|
|
29
|
+
*/
|
|
30
|
+
export declare const before: <T>({ schema, hook }: BeforeInput<T>) => Schema<T>;
|
package/dist/before.js
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __webpack_exports__ = {};
|
|
3
|
+
const external_vitest_namespaceObject = require("vitest");
|
|
4
|
+
const external_before_cjs_namespaceObject = require("./before.cjs");
|
|
5
|
+
const index_cjs_namespaceObject = require("./testing/mocks/index.cjs");
|
|
6
|
+
const schemaMock = (0, index_cjs_namespaceObject.createBaseSchemaMock)();
|
|
7
|
+
const nextSchemaMock = (0, index_cjs_namespaceObject.createBaseSchemaMock)({
|
|
8
|
+
name: 'next.before.schema'
|
|
9
|
+
});
|
|
10
|
+
const hookMock = external_vitest_namespaceObject.vi.fn((input)=>input);
|
|
11
|
+
(0, external_vitest_namespaceObject.describe)('before()', ()=>{
|
|
12
|
+
(0, external_vitest_namespaceObject.beforeAll)(()=>{
|
|
13
|
+
external_vitest_namespaceObject.vi.mocked(schemaMock.before).mockImplementation(()=>nextSchemaMock);
|
|
14
|
+
});
|
|
15
|
+
(0, external_vitest_namespaceObject.beforeEach)(()=>{
|
|
16
|
+
external_vitest_namespaceObject.vi.mocked(schemaMock.before).mockClear();
|
|
17
|
+
hookMock.mockClear();
|
|
18
|
+
});
|
|
19
|
+
(0, external_vitest_namespaceObject.afterEach)(()=>{
|
|
20
|
+
external_vitest_namespaceObject.vi.mocked(schemaMock.before).mockClear();
|
|
21
|
+
});
|
|
22
|
+
(0, external_vitest_namespaceObject.afterAll)(()=>{
|
|
23
|
+
external_vitest_namespaceObject.vi.mocked(schemaMock.before).mockReset();
|
|
24
|
+
});
|
|
25
|
+
(0, external_vitest_namespaceObject.describe)('happy', ()=>{
|
|
26
|
+
(0, external_vitest_namespaceObject.it)('should delegate to schema.before when hook is provided', ()=>{
|
|
27
|
+
const result = (0, external_before_cjs_namespaceObject.before)({
|
|
28
|
+
schema: schemaMock,
|
|
29
|
+
hook: hookMock
|
|
30
|
+
});
|
|
31
|
+
(0, external_vitest_namespaceObject.expect)(schemaMock.before).toHaveBeenCalledTimes(1);
|
|
32
|
+
(0, external_vitest_namespaceObject.expect)(schemaMock.before).toHaveBeenCalledWith(hookMock);
|
|
33
|
+
(0, external_vitest_namespaceObject.expect)(result).toBe(nextSchemaMock);
|
|
34
|
+
});
|
|
35
|
+
});
|
|
36
|
+
(0, external_vitest_namespaceObject.describe)('sad', ()=>{
|
|
37
|
+
(0, external_vitest_namespaceObject.it)('should rethrow error when schema.before throws', ()=>{
|
|
38
|
+
const beforeError = new Error('before hook failed');
|
|
39
|
+
external_vitest_namespaceObject.vi.mocked(schemaMock.before).mockImplementationOnce(()=>{
|
|
40
|
+
throw beforeError;
|
|
41
|
+
});
|
|
42
|
+
const thrown = (0, index_cjs_namespaceObject.captureThrow)(()=>(0, external_before_cjs_namespaceObject.before)({
|
|
43
|
+
schema: schemaMock,
|
|
44
|
+
hook: hookMock
|
|
45
|
+
}));
|
|
46
|
+
(0, external_vitest_namespaceObject.expect)(thrown.threw).toBe(true);
|
|
47
|
+
(0, external_vitest_namespaceObject.expect)(thrown.value).toBe(beforeError);
|
|
48
|
+
});
|
|
49
|
+
});
|
|
50
|
+
});
|
|
51
|
+
for(var __rspack_i in __webpack_exports__)exports[__rspack_i] = __webpack_exports__[__rspack_i];
|
|
52
|
+
Object.defineProperty(exports, '__esModule', {
|
|
53
|
+
value: true
|
|
54
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
|
|
2
|
+
import { before } from "./before.js";
|
|
3
|
+
import { captureThrow, createBaseSchemaMock } from "./testing/mocks/index.js";
|
|
4
|
+
const schemaMock = createBaseSchemaMock();
|
|
5
|
+
const nextSchemaMock = createBaseSchemaMock({
|
|
6
|
+
name: 'next.before.schema'
|
|
7
|
+
});
|
|
8
|
+
const hookMock = vi.fn((input)=>input);
|
|
9
|
+
describe('before()', ()=>{
|
|
10
|
+
beforeAll(()=>{
|
|
11
|
+
vi.mocked(schemaMock.before).mockImplementation(()=>nextSchemaMock);
|
|
12
|
+
});
|
|
13
|
+
beforeEach(()=>{
|
|
14
|
+
vi.mocked(schemaMock.before).mockClear();
|
|
15
|
+
hookMock.mockClear();
|
|
16
|
+
});
|
|
17
|
+
afterEach(()=>{
|
|
18
|
+
vi.mocked(schemaMock.before).mockClear();
|
|
19
|
+
});
|
|
20
|
+
afterAll(()=>{
|
|
21
|
+
vi.mocked(schemaMock.before).mockReset();
|
|
22
|
+
});
|
|
23
|
+
describe('happy', ()=>{
|
|
24
|
+
it('should delegate to schema.before when hook is provided', ()=>{
|
|
25
|
+
const result = before({
|
|
26
|
+
schema: schemaMock,
|
|
27
|
+
hook: hookMock
|
|
28
|
+
});
|
|
29
|
+
expect(schemaMock.before).toHaveBeenCalledTimes(1);
|
|
30
|
+
expect(schemaMock.before).toHaveBeenCalledWith(hookMock);
|
|
31
|
+
expect(result).toBe(nextSchemaMock);
|
|
32
|
+
});
|
|
33
|
+
});
|
|
34
|
+
describe('sad', ()=>{
|
|
35
|
+
it('should rethrow error when schema.before throws', ()=>{
|
|
36
|
+
const beforeError = new Error('before hook failed');
|
|
37
|
+
vi.mocked(schemaMock.before).mockImplementationOnce(()=>{
|
|
38
|
+
throw beforeError;
|
|
39
|
+
});
|
|
40
|
+
const thrown = captureThrow(()=>before({
|
|
41
|
+
schema: schemaMock,
|
|
42
|
+
hook: hookMock
|
|
43
|
+
}));
|
|
44
|
+
expect(thrown.threw).toBe(true);
|
|
45
|
+
expect(thrown.value).toBe(beforeError);
|
|
46
|
+
});
|
|
47
|
+
});
|
|
48
|
+
});
|
package/dist/binary.cjs
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
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
|
+
binary: ()=>binary
|
|
28
|
+
});
|
|
29
|
+
const external_schemaFactory_cjs_namespaceObject = require("./schemaFactory.cjs");
|
|
30
|
+
const external_typeGuards_cjs_namespaceObject = require("./typeGuards.cjs");
|
|
31
|
+
const binary = ({ name, doc })=>(0, external_schemaFactory_cjs_namespaceObject.schemaFactory)({
|
|
32
|
+
name,
|
|
33
|
+
type: 'binary',
|
|
34
|
+
doc,
|
|
35
|
+
ast: ()=>({
|
|
36
|
+
type: 'binary',
|
|
37
|
+
name
|
|
38
|
+
}),
|
|
39
|
+
validate: (input)=>{
|
|
40
|
+
if (!(0, external_typeGuards_cjs_namespaceObject.isUint8Array)(input)) throw {
|
|
41
|
+
message: 'Expected Uint8Array',
|
|
42
|
+
code: 'binary.type'
|
|
43
|
+
};
|
|
44
|
+
return input;
|
|
45
|
+
}
|
|
46
|
+
});
|
|
47
|
+
exports.binary = __webpack_exports__.binary;
|
|
48
|
+
for(var __rspack_i in __webpack_exports__)if (-1 === [
|
|
49
|
+
"binary"
|
|
50
|
+
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
|
|
51
|
+
Object.defineProperty(exports, '__esModule', {
|
|
52
|
+
value: true
|
|
53
|
+
});
|
package/dist/binary.d.ts
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { SchemaDoc } from './types.js';
|
|
2
|
+
export interface BinarySchemaInput {
|
|
3
|
+
name: string;
|
|
4
|
+
doc?: SchemaDoc;
|
|
5
|
+
}
|
|
6
|
+
/**
|
|
7
|
+
* binary is part of the public LIVON API.
|
|
8
|
+
*
|
|
9
|
+
* @remarks
|
|
10
|
+
* Parameter and return types are defined in the TypeScript signature.
|
|
11
|
+
*
|
|
12
|
+
* @see https://live-input-vector-output-node.github.io/livon-ts/docs/schema/binary
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* // Creates a binary schema and validates a Uint8Array payload.
|
|
16
|
+
* const Payload = binary({ name: 'payload' });
|
|
17
|
+
* Payload.parse(new Uint8Array([1, 2, 3]));
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* // Extends binary validation to also allow undefined.
|
|
21
|
+
* const MaybePayload = binary({ name: 'payload' }).optional();
|
|
22
|
+
* MaybePayload.parse(undefined);
|
|
23
|
+
*/
|
|
24
|
+
export declare const binary: ({ name, doc }: BinarySchemaInput) => import("./schemaFactory.js").SchemaWithChain<Uint8Array<ArrayBufferLike>, import("./schemaFactory.js").SchemaFactoryChainDefinition<Uint8Array<ArrayBufferLike>>>;
|
package/dist/binary.js
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { schemaFactory } from "./schemaFactory.js";
|
|
2
|
+
import { isUint8Array } from "./typeGuards.js";
|
|
3
|
+
const binary = ({ name, doc })=>schemaFactory({
|
|
4
|
+
name,
|
|
5
|
+
type: 'binary',
|
|
6
|
+
doc,
|
|
7
|
+
ast: ()=>({
|
|
8
|
+
type: 'binary',
|
|
9
|
+
name
|
|
10
|
+
}),
|
|
11
|
+
validate: (input)=>{
|
|
12
|
+
if (!isUint8Array(input)) throw {
|
|
13
|
+
message: 'Expected Uint8Array',
|
|
14
|
+
code: 'binary.type'
|
|
15
|
+
};
|
|
16
|
+
return input;
|
|
17
|
+
}
|
|
18
|
+
});
|
|
19
|
+
export { binary };
|