@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,158 @@
|
|
|
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_string_cjs_namespaceObject = require("./string.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
|
+
isString: external_vitest_namespaceObject.vi.fn()
|
|
13
|
+
}));
|
|
14
|
+
const schemaFactoryMock = external_vitest_namespaceObject.vi.mocked(external_schemaFactory_cjs_namespaceObject.schemaFactory);
|
|
15
|
+
const isStringMock = external_vitest_namespaceObject.vi.mocked(external_typeGuards_cjs_namespaceObject.isString);
|
|
16
|
+
const schemaContextMock = (0, index_cjs_namespaceObject.createSchemaContextMock)();
|
|
17
|
+
const schemaFactoryImplementation = (0, index_cjs_namespaceObject.createSchemaFactoryMock)();
|
|
18
|
+
const getFactoryInput = ()=>{
|
|
19
|
+
const call = schemaFactoryMock.mock.calls[0];
|
|
20
|
+
if (!call) throw new Error('schemaFactory should be called once before reading input');
|
|
21
|
+
return call[0];
|
|
22
|
+
};
|
|
23
|
+
(0, external_vitest_namespaceObject.describe)('string()', ()=>{
|
|
24
|
+
(0, external_vitest_namespaceObject.beforeAll)(()=>{
|
|
25
|
+
schemaFactoryMock.mockImplementation(schemaFactoryImplementation);
|
|
26
|
+
});
|
|
27
|
+
(0, external_vitest_namespaceObject.beforeEach)(()=>{
|
|
28
|
+
schemaFactoryMock.mockClear();
|
|
29
|
+
schemaFactoryImplementation.mockClear();
|
|
30
|
+
isStringMock.mockReset();
|
|
31
|
+
isStringMock.mockReturnValue(true);
|
|
32
|
+
});
|
|
33
|
+
(0, external_vitest_namespaceObject.afterEach)(()=>{
|
|
34
|
+
isStringMock.mockClear();
|
|
35
|
+
});
|
|
36
|
+
(0, external_vitest_namespaceObject.afterAll)(()=>{
|
|
37
|
+
schemaFactoryMock.mockReset();
|
|
38
|
+
schemaFactoryImplementation.mockReset();
|
|
39
|
+
isStringMock.mockReset();
|
|
40
|
+
});
|
|
41
|
+
(0, external_vitest_namespaceObject.describe)('happy', ()=>{
|
|
42
|
+
(0, external_vitest_namespaceObject.it)('should delegate to schemaFactory when string schema is created', ()=>{
|
|
43
|
+
const result = (0, external_string_cjs_namespaceObject.string)();
|
|
44
|
+
(0, external_vitest_namespaceObject.expect)(schemaFactoryMock).toHaveBeenCalledTimes(1);
|
|
45
|
+
const factoryInput = getFactoryInput();
|
|
46
|
+
(0, external_vitest_namespaceObject.expect)(factoryInput.name).toBe('string');
|
|
47
|
+
(0, external_vitest_namespaceObject.expect)(factoryInput.type).toBe('string');
|
|
48
|
+
(0, external_vitest_namespaceObject.expect)(factoryInput.ast(schemaContextMock)).toEqual({
|
|
49
|
+
type: 'string',
|
|
50
|
+
name: 'string'
|
|
51
|
+
});
|
|
52
|
+
(0, external_vitest_namespaceObject.expect)(Object.keys(factoryInput.chain)).toEqual([
|
|
53
|
+
'min',
|
|
54
|
+
'max',
|
|
55
|
+
'email',
|
|
56
|
+
'regex'
|
|
57
|
+
]);
|
|
58
|
+
(0, external_vitest_namespaceObject.expect)(result).toBe(schemaFactoryMock.mock.results[0]?.value);
|
|
59
|
+
});
|
|
60
|
+
(0, external_vitest_namespaceObject.it)('should pass custom metadata to schemaFactory when options are provided', ()=>{
|
|
61
|
+
const doc = {
|
|
62
|
+
summary: 'string doc'
|
|
63
|
+
};
|
|
64
|
+
(0, external_string_cjs_namespaceObject.string)({
|
|
65
|
+
name: 'Title',
|
|
66
|
+
doc
|
|
67
|
+
});
|
|
68
|
+
const factoryInput = getFactoryInput();
|
|
69
|
+
(0, external_vitest_namespaceObject.expect)(factoryInput.name).toBe('Title');
|
|
70
|
+
(0, external_vitest_namespaceObject.expect)(factoryInput.doc).toEqual(doc);
|
|
71
|
+
(0, external_vitest_namespaceObject.expect)(factoryInput.ast(schemaContextMock)).toEqual({
|
|
72
|
+
type: 'string',
|
|
73
|
+
name: 'Title'
|
|
74
|
+
});
|
|
75
|
+
});
|
|
76
|
+
(0, external_vitest_namespaceObject.it)('should validate input through isString when input is string', ()=>{
|
|
77
|
+
(0, external_string_cjs_namespaceObject.string)();
|
|
78
|
+
const factoryInput = getFactoryInput();
|
|
79
|
+
const parsed = factoryInput.validate('abc', schemaContextMock);
|
|
80
|
+
(0, external_vitest_namespaceObject.expect)(parsed).toBe('abc');
|
|
81
|
+
(0, external_vitest_namespaceObject.expect)(isStringMock).toHaveBeenCalledWith('abc');
|
|
82
|
+
});
|
|
83
|
+
(0, external_vitest_namespaceObject.it)('should return input for all chain methods when constraints are valid', ()=>{
|
|
84
|
+
(0, external_string_cjs_namespaceObject.string)();
|
|
85
|
+
const factoryInput = getFactoryInput();
|
|
86
|
+
(0, external_vitest_namespaceObject.expect)(factoryInput.chain.min('abcd', schemaContextMock)(3)).toBe('abcd');
|
|
87
|
+
(0, external_vitest_namespaceObject.expect)(factoryInput.chain.max('abcd', schemaContextMock)(5)).toBe('abcd');
|
|
88
|
+
(0, external_vitest_namespaceObject.expect)(factoryInput.chain.email('a@b.c', schemaContextMock)()).toBe('a@b.c');
|
|
89
|
+
(0, external_vitest_namespaceObject.expect)(factoryInput.chain.regex('abc-123', schemaContextMock)(/^[a-z]+-\d+$/)).toBe('abc-123');
|
|
90
|
+
});
|
|
91
|
+
});
|
|
92
|
+
(0, external_vitest_namespaceObject.describe)('sad', ()=>{
|
|
93
|
+
(0, external_vitest_namespaceObject.it)('should throw schema error payload when input is not string', ()=>{
|
|
94
|
+
isStringMock.mockReturnValue(false);
|
|
95
|
+
(0, external_string_cjs_namespaceObject.string)();
|
|
96
|
+
const factoryInput = getFactoryInput();
|
|
97
|
+
const thrown = (0, index_cjs_namespaceObject.captureThrow)(()=>factoryInput.validate(10, schemaContextMock));
|
|
98
|
+
(0, external_vitest_namespaceObject.expect)(thrown.threw).toBe(true);
|
|
99
|
+
(0, external_vitest_namespaceObject.expect)(thrown.value).toEqual({
|
|
100
|
+
message: 'Data is not a string',
|
|
101
|
+
code: 'string.type'
|
|
102
|
+
});
|
|
103
|
+
});
|
|
104
|
+
(0, external_vitest_namespaceObject.it)('should throw min error when input length is below minimum', ()=>{
|
|
105
|
+
(0, external_string_cjs_namespaceObject.string)();
|
|
106
|
+
const factoryInput = getFactoryInput();
|
|
107
|
+
const thrown = (0, index_cjs_namespaceObject.captureThrow)(()=>factoryInput.chain.min('ab', schemaContextMock)(3));
|
|
108
|
+
(0, external_vitest_namespaceObject.expect)(thrown.threw).toBe(true);
|
|
109
|
+
(0, external_vitest_namespaceObject.expect)(thrown.value).toEqual({
|
|
110
|
+
message: 'String is too short',
|
|
111
|
+
code: 'string.min',
|
|
112
|
+
context: {
|
|
113
|
+
min: 3
|
|
114
|
+
}
|
|
115
|
+
});
|
|
116
|
+
});
|
|
117
|
+
(0, external_vitest_namespaceObject.it)('should throw max error when input length is above maximum', ()=>{
|
|
118
|
+
(0, external_string_cjs_namespaceObject.string)();
|
|
119
|
+
const factoryInput = getFactoryInput();
|
|
120
|
+
const thrown = (0, index_cjs_namespaceObject.captureThrow)(()=>factoryInput.chain.max('abcdef', schemaContextMock)(5));
|
|
121
|
+
(0, external_vitest_namespaceObject.expect)(thrown.threw).toBe(true);
|
|
122
|
+
(0, external_vitest_namespaceObject.expect)(thrown.value).toEqual({
|
|
123
|
+
message: 'String is too long',
|
|
124
|
+
code: 'string.max',
|
|
125
|
+
context: {
|
|
126
|
+
max: 5
|
|
127
|
+
}
|
|
128
|
+
});
|
|
129
|
+
});
|
|
130
|
+
(0, external_vitest_namespaceObject.it)('should throw email error when input is not an email', ()=>{
|
|
131
|
+
(0, external_string_cjs_namespaceObject.string)();
|
|
132
|
+
const factoryInput = getFactoryInput();
|
|
133
|
+
const thrown = (0, index_cjs_namespaceObject.captureThrow)(()=>factoryInput.chain.email('abc', schemaContextMock)());
|
|
134
|
+
(0, external_vitest_namespaceObject.expect)(thrown.threw).toBe(true);
|
|
135
|
+
(0, external_vitest_namespaceObject.expect)(thrown.value).toEqual({
|
|
136
|
+
message: 'Invalid email format',
|
|
137
|
+
code: 'string.email'
|
|
138
|
+
});
|
|
139
|
+
});
|
|
140
|
+
(0, external_vitest_namespaceObject.it)('should throw regex error when input does not match pattern', ()=>{
|
|
141
|
+
(0, external_string_cjs_namespaceObject.string)();
|
|
142
|
+
const factoryInput = getFactoryInput();
|
|
143
|
+
const thrown = (0, index_cjs_namespaceObject.captureThrow)(()=>factoryInput.chain.regex('abc', schemaContextMock)(/^\d+$/));
|
|
144
|
+
(0, external_vitest_namespaceObject.expect)(thrown.threw).toBe(true);
|
|
145
|
+
(0, external_vitest_namespaceObject.expect)(thrown.value).toEqual({
|
|
146
|
+
message: 'String does not match pattern',
|
|
147
|
+
code: 'string.regex',
|
|
148
|
+
context: {
|
|
149
|
+
pattern: '^\\d+$'
|
|
150
|
+
}
|
|
151
|
+
});
|
|
152
|
+
});
|
|
153
|
+
});
|
|
154
|
+
});
|
|
155
|
+
for(var __rspack_i in __webpack_exports__)exports[__rspack_i] = __webpack_exports__[__rspack_i];
|
|
156
|
+
Object.defineProperty(exports, '__esModule', {
|
|
157
|
+
value: true
|
|
158
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
|
|
2
|
+
import { schemaFactory } from "./schemaFactory.js";
|
|
3
|
+
import { string } from "./string.js";
|
|
4
|
+
import { isString } from "./typeGuards.js";
|
|
5
|
+
import { captureThrow, createSchemaContextMock, createSchemaFactoryMock } from "./testing/mocks/index.js";
|
|
6
|
+
vi.mock('./schemaFactory.js', ()=>({
|
|
7
|
+
schemaFactory: vi.fn()
|
|
8
|
+
}));
|
|
9
|
+
vi.mock('./typeGuards.js', ()=>({
|
|
10
|
+
isString: vi.fn()
|
|
11
|
+
}));
|
|
12
|
+
const schemaFactoryMock = vi.mocked(schemaFactory);
|
|
13
|
+
const isStringMock = vi.mocked(isString);
|
|
14
|
+
const schemaContextMock = createSchemaContextMock();
|
|
15
|
+
const schemaFactoryImplementation = createSchemaFactoryMock();
|
|
16
|
+
const getFactoryInput = ()=>{
|
|
17
|
+
const call = schemaFactoryMock.mock.calls[0];
|
|
18
|
+
if (!call) throw new Error('schemaFactory should be called once before reading input');
|
|
19
|
+
return call[0];
|
|
20
|
+
};
|
|
21
|
+
describe('string()', ()=>{
|
|
22
|
+
beforeAll(()=>{
|
|
23
|
+
schemaFactoryMock.mockImplementation(schemaFactoryImplementation);
|
|
24
|
+
});
|
|
25
|
+
beforeEach(()=>{
|
|
26
|
+
schemaFactoryMock.mockClear();
|
|
27
|
+
schemaFactoryImplementation.mockClear();
|
|
28
|
+
isStringMock.mockReset();
|
|
29
|
+
isStringMock.mockReturnValue(true);
|
|
30
|
+
});
|
|
31
|
+
afterEach(()=>{
|
|
32
|
+
isStringMock.mockClear();
|
|
33
|
+
});
|
|
34
|
+
afterAll(()=>{
|
|
35
|
+
schemaFactoryMock.mockReset();
|
|
36
|
+
schemaFactoryImplementation.mockReset();
|
|
37
|
+
isStringMock.mockReset();
|
|
38
|
+
});
|
|
39
|
+
describe('happy', ()=>{
|
|
40
|
+
it('should delegate to schemaFactory when string schema is created', ()=>{
|
|
41
|
+
const result = string();
|
|
42
|
+
expect(schemaFactoryMock).toHaveBeenCalledTimes(1);
|
|
43
|
+
const factoryInput = getFactoryInput();
|
|
44
|
+
expect(factoryInput.name).toBe('string');
|
|
45
|
+
expect(factoryInput.type).toBe('string');
|
|
46
|
+
expect(factoryInput.ast(schemaContextMock)).toEqual({
|
|
47
|
+
type: 'string',
|
|
48
|
+
name: 'string'
|
|
49
|
+
});
|
|
50
|
+
expect(Object.keys(factoryInput.chain)).toEqual([
|
|
51
|
+
'min',
|
|
52
|
+
'max',
|
|
53
|
+
'email',
|
|
54
|
+
'regex'
|
|
55
|
+
]);
|
|
56
|
+
expect(result).toBe(schemaFactoryMock.mock.results[0]?.value);
|
|
57
|
+
});
|
|
58
|
+
it('should pass custom metadata to schemaFactory when options are provided', ()=>{
|
|
59
|
+
const doc = {
|
|
60
|
+
summary: 'string doc'
|
|
61
|
+
};
|
|
62
|
+
string({
|
|
63
|
+
name: 'Title',
|
|
64
|
+
doc
|
|
65
|
+
});
|
|
66
|
+
const factoryInput = getFactoryInput();
|
|
67
|
+
expect(factoryInput.name).toBe('Title');
|
|
68
|
+
expect(factoryInput.doc).toEqual(doc);
|
|
69
|
+
expect(factoryInput.ast(schemaContextMock)).toEqual({
|
|
70
|
+
type: 'string',
|
|
71
|
+
name: 'Title'
|
|
72
|
+
});
|
|
73
|
+
});
|
|
74
|
+
it('should validate input through isString when input is string', ()=>{
|
|
75
|
+
string();
|
|
76
|
+
const factoryInput = getFactoryInput();
|
|
77
|
+
const parsed = factoryInput.validate('abc', schemaContextMock);
|
|
78
|
+
expect(parsed).toBe('abc');
|
|
79
|
+
expect(isStringMock).toHaveBeenCalledWith('abc');
|
|
80
|
+
});
|
|
81
|
+
it('should return input for all chain methods when constraints are valid', ()=>{
|
|
82
|
+
string();
|
|
83
|
+
const factoryInput = getFactoryInput();
|
|
84
|
+
expect(factoryInput.chain.min('abcd', schemaContextMock)(3)).toBe('abcd');
|
|
85
|
+
expect(factoryInput.chain.max('abcd', schemaContextMock)(5)).toBe('abcd');
|
|
86
|
+
expect(factoryInput.chain.email('a@b.c', schemaContextMock)()).toBe('a@b.c');
|
|
87
|
+
expect(factoryInput.chain.regex('abc-123', schemaContextMock)(/^[a-z]+-\d+$/)).toBe('abc-123');
|
|
88
|
+
});
|
|
89
|
+
});
|
|
90
|
+
describe('sad', ()=>{
|
|
91
|
+
it('should throw schema error payload when input is not string', ()=>{
|
|
92
|
+
isStringMock.mockReturnValue(false);
|
|
93
|
+
string();
|
|
94
|
+
const factoryInput = getFactoryInput();
|
|
95
|
+
const thrown = captureThrow(()=>factoryInput.validate(10, schemaContextMock));
|
|
96
|
+
expect(thrown.threw).toBe(true);
|
|
97
|
+
expect(thrown.value).toEqual({
|
|
98
|
+
message: 'Data is not a string',
|
|
99
|
+
code: 'string.type'
|
|
100
|
+
});
|
|
101
|
+
});
|
|
102
|
+
it('should throw min error when input length is below minimum', ()=>{
|
|
103
|
+
string();
|
|
104
|
+
const factoryInput = getFactoryInput();
|
|
105
|
+
const thrown = captureThrow(()=>factoryInput.chain.min('ab', schemaContextMock)(3));
|
|
106
|
+
expect(thrown.threw).toBe(true);
|
|
107
|
+
expect(thrown.value).toEqual({
|
|
108
|
+
message: 'String is too short',
|
|
109
|
+
code: 'string.min',
|
|
110
|
+
context: {
|
|
111
|
+
min: 3
|
|
112
|
+
}
|
|
113
|
+
});
|
|
114
|
+
});
|
|
115
|
+
it('should throw max error when input length is above maximum', ()=>{
|
|
116
|
+
string();
|
|
117
|
+
const factoryInput = getFactoryInput();
|
|
118
|
+
const thrown = captureThrow(()=>factoryInput.chain.max('abcdef', schemaContextMock)(5));
|
|
119
|
+
expect(thrown.threw).toBe(true);
|
|
120
|
+
expect(thrown.value).toEqual({
|
|
121
|
+
message: 'String is too long',
|
|
122
|
+
code: 'string.max',
|
|
123
|
+
context: {
|
|
124
|
+
max: 5
|
|
125
|
+
}
|
|
126
|
+
});
|
|
127
|
+
});
|
|
128
|
+
it('should throw email error when input is not an email', ()=>{
|
|
129
|
+
string();
|
|
130
|
+
const factoryInput = getFactoryInput();
|
|
131
|
+
const thrown = captureThrow(()=>factoryInput.chain.email('abc', schemaContextMock)());
|
|
132
|
+
expect(thrown.threw).toBe(true);
|
|
133
|
+
expect(thrown.value).toEqual({
|
|
134
|
+
message: 'Invalid email format',
|
|
135
|
+
code: 'string.email'
|
|
136
|
+
});
|
|
137
|
+
});
|
|
138
|
+
it('should throw regex error when input does not match pattern', ()=>{
|
|
139
|
+
string();
|
|
140
|
+
const factoryInput = getFactoryInput();
|
|
141
|
+
const thrown = captureThrow(()=>factoryInput.chain.regex('abc', schemaContextMock)(/^\d+$/));
|
|
142
|
+
expect(thrown.threw).toBe(true);
|
|
143
|
+
expect(thrown.value).toEqual({
|
|
144
|
+
message: 'String does not match pattern',
|
|
145
|
+
code: 'string.regex',
|
|
146
|
+
context: {
|
|
147
|
+
pattern: '^\\d+$'
|
|
148
|
+
}
|
|
149
|
+
});
|
|
150
|
+
});
|
|
151
|
+
});
|
|
152
|
+
});
|
|
@@ -0,0 +1,48 @@
|
|
|
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
|
+
captureThrow: ()=>captureThrow
|
|
28
|
+
});
|
|
29
|
+
const captureThrow = (fn)=>{
|
|
30
|
+
try {
|
|
31
|
+
fn();
|
|
32
|
+
return {
|
|
33
|
+
threw: false
|
|
34
|
+
};
|
|
35
|
+
} catch (error) {
|
|
36
|
+
return {
|
|
37
|
+
threw: true,
|
|
38
|
+
value: error
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
};
|
|
42
|
+
exports.captureThrow = __webpack_exports__.captureThrow;
|
|
43
|
+
for(var __rspack_i in __webpack_exports__)if (-1 === [
|
|
44
|
+
"captureThrow"
|
|
45
|
+
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
|
|
46
|
+
Object.defineProperty(exports, '__esModule', {
|
|
47
|
+
value: true
|
|
48
|
+
});
|
|
@@ -0,0 +1,52 @@
|
|
|
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
|
+
createSchemaWithChainMock: ()=>external_schema_mock_cjs_namespaceObject.createSchemaWithChainMock,
|
|
28
|
+
createBaseSchemaMock: ()=>external_schema_mock_cjs_namespaceObject.createBaseSchemaMock,
|
|
29
|
+
captureThrow: ()=>external_assertions_mock_cjs_namespaceObject.captureThrow,
|
|
30
|
+
createSchemaContextMock: ()=>external_schema_mock_cjs_namespaceObject.createSchemaContextMock,
|
|
31
|
+
createSchemaFactoryMock: ()=>external_schema_mock_cjs_namespaceObject.createSchemaFactoryMock,
|
|
32
|
+
createSchemaStateMock: ()=>external_schema_mock_cjs_namespaceObject.createSchemaStateMock
|
|
33
|
+
});
|
|
34
|
+
const external_schema_mock_cjs_namespaceObject = require("./schema.mock.cjs");
|
|
35
|
+
const external_assertions_mock_cjs_namespaceObject = require("./assertions.mock.cjs");
|
|
36
|
+
exports.captureThrow = __webpack_exports__.captureThrow;
|
|
37
|
+
exports.createBaseSchemaMock = __webpack_exports__.createBaseSchemaMock;
|
|
38
|
+
exports.createSchemaContextMock = __webpack_exports__.createSchemaContextMock;
|
|
39
|
+
exports.createSchemaFactoryMock = __webpack_exports__.createSchemaFactoryMock;
|
|
40
|
+
exports.createSchemaStateMock = __webpack_exports__.createSchemaStateMock;
|
|
41
|
+
exports.createSchemaWithChainMock = __webpack_exports__.createSchemaWithChainMock;
|
|
42
|
+
for(var __rspack_i in __webpack_exports__)if (-1 === [
|
|
43
|
+
"captureThrow",
|
|
44
|
+
"createBaseSchemaMock",
|
|
45
|
+
"createSchemaContextMock",
|
|
46
|
+
"createSchemaFactoryMock",
|
|
47
|
+
"createSchemaStateMock",
|
|
48
|
+
"createSchemaWithChainMock"
|
|
49
|
+
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
|
|
50
|
+
Object.defineProperty(exports, '__esModule', {
|
|
51
|
+
value: true
|
|
52
|
+
});
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export { createBaseSchemaMock, createSchemaStateMock, createSchemaContextMock, createSchemaWithChainMock, createSchemaFactoryMock, } from './schema.mock.js';
|
|
2
|
+
export { captureThrow } from './assertions.mock.js';
|
|
3
|
+
export type { BaseSchemaMockOverrides, SchemaStateMockOverrides, SchemaContextMockOverrides, SchemaWithChainMockInput, SchemaFactoryMockInput, SchemaFactoryMock, } from './schema.mock.js';
|
|
4
|
+
export type { CapturedThrow } from './assertions.mock.js';
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
import { createBaseSchemaMock, createSchemaContextMock, createSchemaFactoryMock, createSchemaStateMock, createSchemaWithChainMock } from "./schema.mock.js";
|
|
2
|
+
import { captureThrow } from "./assertions.mock.js";
|
|
3
|
+
export { captureThrow, createBaseSchemaMock, createSchemaContextMock, createSchemaFactoryMock, createSchemaStateMock, createSchemaWithChainMock };
|
|
@@ -0,0 +1,120 @@
|
|
|
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
|
+
createSchemaWithChainMock: ()=>createSchemaWithChainMock,
|
|
28
|
+
createBaseSchemaMock: ()=>createBaseSchemaMock,
|
|
29
|
+
createSchemaContextMock: ()=>createSchemaContextMock,
|
|
30
|
+
createSchemaFactoryMock: ()=>createSchemaFactoryMock,
|
|
31
|
+
createSchemaStateMock: ()=>createSchemaStateMock
|
|
32
|
+
});
|
|
33
|
+
const external_vitest_namespaceObject = require("vitest");
|
|
34
|
+
const createSchemaStateMock = (overrides = {})=>({
|
|
35
|
+
get: external_vitest_namespaceObject.vi.fn(()=>void 0),
|
|
36
|
+
set: external_vitest_namespaceObject.vi.fn(()=>void 0),
|
|
37
|
+
update: external_vitest_namespaceObject.vi.fn(()=>void 0),
|
|
38
|
+
snapshot: external_vitest_namespaceObject.vi.fn(()=>({})),
|
|
39
|
+
...overrides
|
|
40
|
+
});
|
|
41
|
+
const createSchemaContextMock = (overrides = {})=>{
|
|
42
|
+
const state = overrides.state ?? createSchemaStateMock();
|
|
43
|
+
const getBuildContext = external_vitest_namespaceObject.vi.fn(()=>overrides.buildContext);
|
|
44
|
+
const setBuildContext = external_vitest_namespaceObject.vi.fn(()=>void 0);
|
|
45
|
+
const getRequestContext = external_vitest_namespaceObject.vi.fn(()=>overrides.requestContext);
|
|
46
|
+
const setRequestContext = external_vitest_namespaceObject.vi.fn((_input)=>void 0);
|
|
47
|
+
return {
|
|
48
|
+
getBuildContext,
|
|
49
|
+
setBuildContext,
|
|
50
|
+
getRequestContext,
|
|
51
|
+
setRequestContext,
|
|
52
|
+
get request () {
|
|
53
|
+
return overrides.requestContext;
|
|
54
|
+
},
|
|
55
|
+
state,
|
|
56
|
+
...overrides
|
|
57
|
+
};
|
|
58
|
+
};
|
|
59
|
+
const createDefaultAstNode = (name, type)=>({
|
|
60
|
+
name,
|
|
61
|
+
type
|
|
62
|
+
});
|
|
63
|
+
const createBaseSchemaMock = (overrides = {})=>{
|
|
64
|
+
const name = overrides.name ?? 'schema.mock';
|
|
65
|
+
const type = overrides.type ?? 'schema.mock';
|
|
66
|
+
const astNode = overrides.astNode ?? createDefaultAstNode(name, type);
|
|
67
|
+
const outputValue = overrides.outputValue;
|
|
68
|
+
const validateResult = overrides.validateResult ?? {
|
|
69
|
+
ok: true,
|
|
70
|
+
value: outputValue
|
|
71
|
+
};
|
|
72
|
+
const schema = {};
|
|
73
|
+
schema.name = name;
|
|
74
|
+
schema.type = type;
|
|
75
|
+
schema.ast = external_vitest_namespaceObject.vi.fn(()=>astNode);
|
|
76
|
+
schema.validate = external_vitest_namespaceObject.vi.fn(()=>validateResult);
|
|
77
|
+
schema.parse = external_vitest_namespaceObject.vi.fn(()=>outputValue);
|
|
78
|
+
schema.typed = external_vitest_namespaceObject.vi.fn((input)=>input);
|
|
79
|
+
schema.optional = external_vitest_namespaceObject.vi.fn(()=>schema);
|
|
80
|
+
schema.nullable = external_vitest_namespaceObject.vi.fn(()=>schema);
|
|
81
|
+
schema.describe = external_vitest_namespaceObject.vi.fn(()=>schema);
|
|
82
|
+
schema.refine = external_vitest_namespaceObject.vi.fn(()=>schema);
|
|
83
|
+
schema.before = external_vitest_namespaceObject.vi.fn(()=>schema);
|
|
84
|
+
schema.after = external_vitest_namespaceObject.vi.fn(()=>schema);
|
|
85
|
+
schema.and = external_vitest_namespaceObject.vi.fn(()=>schema);
|
|
86
|
+
return {
|
|
87
|
+
...schema,
|
|
88
|
+
...overrides
|
|
89
|
+
};
|
|
90
|
+
};
|
|
91
|
+
const createSchemaWithChainMock = (input = {})=>{
|
|
92
|
+
const schema = createBaseSchemaMock({
|
|
93
|
+
name: input.name,
|
|
94
|
+
type: input.type
|
|
95
|
+
});
|
|
96
|
+
(input.chainKeys ?? []).forEach((chainKey)=>{
|
|
97
|
+
schema[chainKey] = external_vitest_namespaceObject.vi.fn(()=>schema);
|
|
98
|
+
});
|
|
99
|
+
return input.overrides ? Object.assign(schema, input.overrides) : schema;
|
|
100
|
+
};
|
|
101
|
+
const createSchemaFactoryMock = ()=>external_vitest_namespaceObject.vi.fn((factoryInput)=>createSchemaWithChainMock({
|
|
102
|
+
name: factoryInput.name,
|
|
103
|
+
type: factoryInput.type,
|
|
104
|
+
chainKeys: Object.keys(factoryInput.chain ?? {})
|
|
105
|
+
}));
|
|
106
|
+
exports.createBaseSchemaMock = __webpack_exports__.createBaseSchemaMock;
|
|
107
|
+
exports.createSchemaContextMock = __webpack_exports__.createSchemaContextMock;
|
|
108
|
+
exports.createSchemaFactoryMock = __webpack_exports__.createSchemaFactoryMock;
|
|
109
|
+
exports.createSchemaStateMock = __webpack_exports__.createSchemaStateMock;
|
|
110
|
+
exports.createSchemaWithChainMock = __webpack_exports__.createSchemaWithChainMock;
|
|
111
|
+
for(var __rspack_i in __webpack_exports__)if (-1 === [
|
|
112
|
+
"createBaseSchemaMock",
|
|
113
|
+
"createSchemaContextMock",
|
|
114
|
+
"createSchemaFactoryMock",
|
|
115
|
+
"createSchemaStateMock",
|
|
116
|
+
"createSchemaWithChainMock"
|
|
117
|
+
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
|
|
118
|
+
Object.defineProperty(exports, '__esModule', {
|
|
119
|
+
value: true
|
|
120
|
+
});
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { vi } from 'vitest';
|
|
2
|
+
import type { AstNode, Schema, SchemaBuildContext, SchemaContext, SchemaDoc, SchemaRequestContext, SchemaState, SchemaResult } from '../../types.js';
|
|
3
|
+
import type { SchemaFactoryChainDefinition, SchemaWithChain } from '../../schemaFactory.js';
|
|
4
|
+
export interface BaseSchemaMockOverrides<TValue> extends Partial<Schema<TValue>> {
|
|
5
|
+
name?: string;
|
|
6
|
+
type?: string;
|
|
7
|
+
astNode?: AstNode;
|
|
8
|
+
outputValue?: TValue;
|
|
9
|
+
validateResult?: SchemaResult<TValue>;
|
|
10
|
+
}
|
|
11
|
+
export interface SchemaStateMockOverrides extends Partial<SchemaState> {
|
|
12
|
+
}
|
|
13
|
+
export declare const createSchemaStateMock: (overrides?: SchemaStateMockOverrides) => SchemaState;
|
|
14
|
+
export interface SchemaContextMockOverrides extends Partial<SchemaContext> {
|
|
15
|
+
buildContext?: SchemaBuildContext;
|
|
16
|
+
requestContext?: SchemaRequestContext;
|
|
17
|
+
state?: SchemaState;
|
|
18
|
+
}
|
|
19
|
+
export declare const createSchemaContextMock: (overrides?: SchemaContextMockOverrides) => SchemaContext;
|
|
20
|
+
export declare const createBaseSchemaMock: <TValue>(overrides?: BaseSchemaMockOverrides<TValue>) => Schema<TValue>;
|
|
21
|
+
export interface SchemaWithChainMockInput<TValue = unknown> {
|
|
22
|
+
name?: string;
|
|
23
|
+
type?: string;
|
|
24
|
+
chainKeys?: readonly string[];
|
|
25
|
+
overrides?: Partial<SchemaWithChain<TValue, SchemaFactoryChainDefinition<TValue>>>;
|
|
26
|
+
}
|
|
27
|
+
export declare const createSchemaWithChainMock: <TValue = unknown>(input?: SchemaWithChainMockInput<TValue>) => SchemaWithChain<TValue, SchemaFactoryChainDefinition<TValue>>;
|
|
28
|
+
export interface SchemaFactoryMockInput {
|
|
29
|
+
name: string;
|
|
30
|
+
type: string;
|
|
31
|
+
doc?: SchemaDoc;
|
|
32
|
+
ast: (ctx: SchemaContext) => AstNode;
|
|
33
|
+
validate: (input: unknown, ctx: SchemaContext) => unknown;
|
|
34
|
+
chain?: Record<string, (value: unknown, ctx: SchemaContext) => (...args: readonly unknown[]) => unknown>;
|
|
35
|
+
}
|
|
36
|
+
export type SchemaFactoryMock = ReturnType<typeof vi.fn<(input: SchemaFactoryMockInput) => SchemaWithChain<unknown, SchemaFactoryChainDefinition<unknown>>>>;
|
|
37
|
+
export declare const createSchemaFactoryMock: () => SchemaFactoryMock;
|