@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,107 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __webpack_exports__ = {};
|
|
3
|
+
const external_vitest_namespaceObject = require("vitest");
|
|
4
|
+
const external_binary_cjs_namespaceObject = require("./binary.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
|
+
isUint8Array: external_vitest_namespaceObject.vi.fn()
|
|
13
|
+
}));
|
|
14
|
+
const schemaFactoryMock = external_vitest_namespaceObject.vi.mocked(external_schemaFactory_cjs_namespaceObject.schemaFactory);
|
|
15
|
+
const isUint8ArrayMock = external_vitest_namespaceObject.vi.mocked(external_typeGuards_cjs_namespaceObject.isUint8Array);
|
|
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)('binary()', ()=>{
|
|
24
|
+
(0, external_vitest_namespaceObject.beforeAll)(()=>{
|
|
25
|
+
schemaFactoryMock.mockImplementation(schemaFactoryImplementation);
|
|
26
|
+
});
|
|
27
|
+
(0, external_vitest_namespaceObject.beforeEach)(()=>{
|
|
28
|
+
schemaFactoryMock.mockClear();
|
|
29
|
+
schemaFactoryImplementation.mockClear();
|
|
30
|
+
isUint8ArrayMock.mockReset();
|
|
31
|
+
isUint8ArrayMock.mockReturnValue(true);
|
|
32
|
+
});
|
|
33
|
+
(0, external_vitest_namespaceObject.afterEach)(()=>{
|
|
34
|
+
isUint8ArrayMock.mockClear();
|
|
35
|
+
});
|
|
36
|
+
(0, external_vitest_namespaceObject.afterAll)(()=>{
|
|
37
|
+
schemaFactoryMock.mockReset();
|
|
38
|
+
schemaFactoryImplementation.mockReset();
|
|
39
|
+
isUint8ArrayMock.mockReset();
|
|
40
|
+
});
|
|
41
|
+
(0, external_vitest_namespaceObject.describe)('happy', ()=>{
|
|
42
|
+
(0, external_vitest_namespaceObject.it)('should delegate to schemaFactory when binary input metadata is provided', ()=>{
|
|
43
|
+
const result = (0, external_binary_cjs_namespaceObject.binary)({
|
|
44
|
+
name: 'Attachment'
|
|
45
|
+
});
|
|
46
|
+
(0, external_vitest_namespaceObject.expect)(schemaFactoryMock).toHaveBeenCalledTimes(1);
|
|
47
|
+
const factoryInput = getFactoryInput();
|
|
48
|
+
(0, external_vitest_namespaceObject.expect)(factoryInput.name).toBe('Attachment');
|
|
49
|
+
(0, external_vitest_namespaceObject.expect)(factoryInput.type).toBe('binary');
|
|
50
|
+
(0, external_vitest_namespaceObject.expect)(factoryInput.doc).toBeUndefined();
|
|
51
|
+
(0, external_vitest_namespaceObject.expect)(factoryInput.ast(schemaContextMock)).toEqual({
|
|
52
|
+
type: 'binary',
|
|
53
|
+
name: 'Attachment'
|
|
54
|
+
});
|
|
55
|
+
(0, external_vitest_namespaceObject.expect)(result).toBe(schemaFactoryMock.mock.results[0]?.value);
|
|
56
|
+
});
|
|
57
|
+
(0, external_vitest_namespaceObject.it)('should pass doc metadata to schemaFactory when doc is provided', ()=>{
|
|
58
|
+
const doc = {
|
|
59
|
+
summary: 'binary doc'
|
|
60
|
+
};
|
|
61
|
+
(0, external_binary_cjs_namespaceObject.binary)({
|
|
62
|
+
name: 'Payload',
|
|
63
|
+
doc
|
|
64
|
+
});
|
|
65
|
+
const factoryInput = getFactoryInput();
|
|
66
|
+
(0, external_vitest_namespaceObject.expect)(factoryInput.name).toBe('Payload');
|
|
67
|
+
(0, external_vitest_namespaceObject.expect)(factoryInput.doc).toEqual(doc);
|
|
68
|
+
(0, external_vitest_namespaceObject.expect)(factoryInput.ast(schemaContextMock)).toEqual({
|
|
69
|
+
type: 'binary',
|
|
70
|
+
name: 'Payload'
|
|
71
|
+
});
|
|
72
|
+
});
|
|
73
|
+
(0, external_vitest_namespaceObject.it)('should validate input through isUint8Array when input is Uint8Array', ()=>{
|
|
74
|
+
const input = new Uint8Array([
|
|
75
|
+
9,
|
|
76
|
+
8,
|
|
77
|
+
7
|
|
78
|
+
]);
|
|
79
|
+
(0, external_binary_cjs_namespaceObject.binary)({
|
|
80
|
+
name: 'Blob'
|
|
81
|
+
});
|
|
82
|
+
const factoryInput = getFactoryInput();
|
|
83
|
+
const parsed = factoryInput.validate(input, schemaContextMock);
|
|
84
|
+
(0, external_vitest_namespaceObject.expect)(parsed).toBe(input);
|
|
85
|
+
(0, external_vitest_namespaceObject.expect)(isUint8ArrayMock).toHaveBeenCalledWith(input);
|
|
86
|
+
});
|
|
87
|
+
});
|
|
88
|
+
(0, external_vitest_namespaceObject.describe)('sad', ()=>{
|
|
89
|
+
(0, external_vitest_namespaceObject.it)('should throw schema error payload when input is not Uint8Array', ()=>{
|
|
90
|
+
isUint8ArrayMock.mockReturnValue(false);
|
|
91
|
+
(0, external_binary_cjs_namespaceObject.binary)({
|
|
92
|
+
name: 'Blob'
|
|
93
|
+
});
|
|
94
|
+
const factoryInput = getFactoryInput();
|
|
95
|
+
const thrown = (0, index_cjs_namespaceObject.captureThrow)(()=>factoryInput.validate('x', schemaContextMock));
|
|
96
|
+
(0, external_vitest_namespaceObject.expect)(thrown.threw).toBe(true);
|
|
97
|
+
(0, external_vitest_namespaceObject.expect)(thrown.value).toEqual({
|
|
98
|
+
message: 'Expected Uint8Array',
|
|
99
|
+
code: 'binary.type'
|
|
100
|
+
});
|
|
101
|
+
});
|
|
102
|
+
});
|
|
103
|
+
});
|
|
104
|
+
for(var __rspack_i in __webpack_exports__)exports[__rspack_i] = __webpack_exports__[__rspack_i];
|
|
105
|
+
Object.defineProperty(exports, '__esModule', {
|
|
106
|
+
value: true
|
|
107
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
|
|
2
|
+
import { binary } from "./binary.js";
|
|
3
|
+
import { schemaFactory } from "./schemaFactory.js";
|
|
4
|
+
import { isUint8Array } 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
|
+
isUint8Array: vi.fn()
|
|
11
|
+
}));
|
|
12
|
+
const schemaFactoryMock = vi.mocked(schemaFactory);
|
|
13
|
+
const isUint8ArrayMock = vi.mocked(isUint8Array);
|
|
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('binary()', ()=>{
|
|
22
|
+
beforeAll(()=>{
|
|
23
|
+
schemaFactoryMock.mockImplementation(schemaFactoryImplementation);
|
|
24
|
+
});
|
|
25
|
+
beforeEach(()=>{
|
|
26
|
+
schemaFactoryMock.mockClear();
|
|
27
|
+
schemaFactoryImplementation.mockClear();
|
|
28
|
+
isUint8ArrayMock.mockReset();
|
|
29
|
+
isUint8ArrayMock.mockReturnValue(true);
|
|
30
|
+
});
|
|
31
|
+
afterEach(()=>{
|
|
32
|
+
isUint8ArrayMock.mockClear();
|
|
33
|
+
});
|
|
34
|
+
afterAll(()=>{
|
|
35
|
+
schemaFactoryMock.mockReset();
|
|
36
|
+
schemaFactoryImplementation.mockReset();
|
|
37
|
+
isUint8ArrayMock.mockReset();
|
|
38
|
+
});
|
|
39
|
+
describe('happy', ()=>{
|
|
40
|
+
it('should delegate to schemaFactory when binary input metadata is provided', ()=>{
|
|
41
|
+
const result = binary({
|
|
42
|
+
name: 'Attachment'
|
|
43
|
+
});
|
|
44
|
+
expect(schemaFactoryMock).toHaveBeenCalledTimes(1);
|
|
45
|
+
const factoryInput = getFactoryInput();
|
|
46
|
+
expect(factoryInput.name).toBe('Attachment');
|
|
47
|
+
expect(factoryInput.type).toBe('binary');
|
|
48
|
+
expect(factoryInput.doc).toBeUndefined();
|
|
49
|
+
expect(factoryInput.ast(schemaContextMock)).toEqual({
|
|
50
|
+
type: 'binary',
|
|
51
|
+
name: 'Attachment'
|
|
52
|
+
});
|
|
53
|
+
expect(result).toBe(schemaFactoryMock.mock.results[0]?.value);
|
|
54
|
+
});
|
|
55
|
+
it('should pass doc metadata to schemaFactory when doc is provided', ()=>{
|
|
56
|
+
const doc = {
|
|
57
|
+
summary: 'binary doc'
|
|
58
|
+
};
|
|
59
|
+
binary({
|
|
60
|
+
name: 'Payload',
|
|
61
|
+
doc
|
|
62
|
+
});
|
|
63
|
+
const factoryInput = getFactoryInput();
|
|
64
|
+
expect(factoryInput.name).toBe('Payload');
|
|
65
|
+
expect(factoryInput.doc).toEqual(doc);
|
|
66
|
+
expect(factoryInput.ast(schemaContextMock)).toEqual({
|
|
67
|
+
type: 'binary',
|
|
68
|
+
name: 'Payload'
|
|
69
|
+
});
|
|
70
|
+
});
|
|
71
|
+
it('should validate input through isUint8Array when input is Uint8Array', ()=>{
|
|
72
|
+
const input = new Uint8Array([
|
|
73
|
+
9,
|
|
74
|
+
8,
|
|
75
|
+
7
|
|
76
|
+
]);
|
|
77
|
+
binary({
|
|
78
|
+
name: 'Blob'
|
|
79
|
+
});
|
|
80
|
+
const factoryInput = getFactoryInput();
|
|
81
|
+
const parsed = factoryInput.validate(input, schemaContextMock);
|
|
82
|
+
expect(parsed).toBe(input);
|
|
83
|
+
expect(isUint8ArrayMock).toHaveBeenCalledWith(input);
|
|
84
|
+
});
|
|
85
|
+
});
|
|
86
|
+
describe('sad', ()=>{
|
|
87
|
+
it('should throw schema error payload when input is not Uint8Array', ()=>{
|
|
88
|
+
isUint8ArrayMock.mockReturnValue(false);
|
|
89
|
+
binary({
|
|
90
|
+
name: 'Blob'
|
|
91
|
+
});
|
|
92
|
+
const factoryInput = getFactoryInput();
|
|
93
|
+
const thrown = captureThrow(()=>factoryInput.validate('x', schemaContextMock));
|
|
94
|
+
expect(thrown.threw).toBe(true);
|
|
95
|
+
expect(thrown.value).toEqual({
|
|
96
|
+
message: 'Expected Uint8Array',
|
|
97
|
+
code: 'binary.type'
|
|
98
|
+
});
|
|
99
|
+
});
|
|
100
|
+
});
|
|
101
|
+
});
|
package/dist/boolean.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
|
+
boolean: ()=>boolean_boolean
|
|
28
|
+
});
|
|
29
|
+
const external_schemaFactory_cjs_namespaceObject = require("./schemaFactory.cjs");
|
|
30
|
+
const external_typeGuards_cjs_namespaceObject = require("./typeGuards.cjs");
|
|
31
|
+
const boolean_boolean = ({ name = 'boolean', doc } = {})=>(0, external_schemaFactory_cjs_namespaceObject.schemaFactory)({
|
|
32
|
+
name,
|
|
33
|
+
type: 'boolean',
|
|
34
|
+
doc,
|
|
35
|
+
ast: ()=>({
|
|
36
|
+
type: 'boolean',
|
|
37
|
+
name
|
|
38
|
+
}),
|
|
39
|
+
validate: (input)=>{
|
|
40
|
+
if (!(0, external_typeGuards_cjs_namespaceObject.isBoolean)(input)) throw {
|
|
41
|
+
message: 'Data is not a boolean',
|
|
42
|
+
code: 'boolean.type'
|
|
43
|
+
};
|
|
44
|
+
return input;
|
|
45
|
+
}
|
|
46
|
+
});
|
|
47
|
+
exports.boolean = __webpack_exports__.boolean;
|
|
48
|
+
for(var __rspack_i in __webpack_exports__)if (-1 === [
|
|
49
|
+
"boolean"
|
|
50
|
+
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
|
|
51
|
+
Object.defineProperty(exports, '__esModule', {
|
|
52
|
+
value: true
|
|
53
|
+
});
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { SchemaDoc } from './types.js';
|
|
2
|
+
export interface BooleanFactoryInput {
|
|
3
|
+
name?: string;
|
|
4
|
+
doc?: SchemaDoc;
|
|
5
|
+
}
|
|
6
|
+
/**
|
|
7
|
+
* boolean 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/boolean
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* // Creates a boolean schema and validates a true value.
|
|
16
|
+
* const IsActive = boolean();
|
|
17
|
+
* IsActive.parse(true);
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* // Extends boolean validation to also allow undefined.
|
|
21
|
+
* const MaybeIsActive = boolean().optional();
|
|
22
|
+
* MaybeIsActive.parse(undefined);
|
|
23
|
+
*/
|
|
24
|
+
export declare const boolean: ({ name, doc }?: BooleanFactoryInput) => import("./schemaFactory.js").SchemaWithChain<boolean, import("./schemaFactory.js").SchemaFactoryChainDefinition<boolean>>;
|
package/dist/boolean.js
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { schemaFactory } from "./schemaFactory.js";
|
|
2
|
+
import { isBoolean } from "./typeGuards.js";
|
|
3
|
+
const boolean_boolean = ({ name = 'boolean', doc } = {})=>schemaFactory({
|
|
4
|
+
name,
|
|
5
|
+
type: 'boolean',
|
|
6
|
+
doc,
|
|
7
|
+
ast: ()=>({
|
|
8
|
+
type: 'boolean',
|
|
9
|
+
name
|
|
10
|
+
}),
|
|
11
|
+
validate: (input)=>{
|
|
12
|
+
if (!isBoolean(input)) throw {
|
|
13
|
+
message: 'Data is not a boolean',
|
|
14
|
+
code: 'boolean.type'
|
|
15
|
+
};
|
|
16
|
+
return input;
|
|
17
|
+
}
|
|
18
|
+
});
|
|
19
|
+
export { boolean_boolean as boolean };
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __webpack_exports__ = {};
|
|
3
|
+
const external_vitest_namespaceObject = require("vitest");
|
|
4
|
+
const external_boolean_cjs_namespaceObject = require("./boolean.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
|
+
isBoolean: external_vitest_namespaceObject.vi.fn()
|
|
13
|
+
}));
|
|
14
|
+
const schemaFactoryMock = external_vitest_namespaceObject.vi.mocked(external_schemaFactory_cjs_namespaceObject.schemaFactory);
|
|
15
|
+
const isBooleanMock = external_vitest_namespaceObject.vi.mocked(external_typeGuards_cjs_namespaceObject.isBoolean);
|
|
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)('boolean()', ()=>{
|
|
24
|
+
(0, external_vitest_namespaceObject.beforeAll)(()=>{
|
|
25
|
+
schemaFactoryMock.mockImplementation(schemaFactoryImplementation);
|
|
26
|
+
});
|
|
27
|
+
(0, external_vitest_namespaceObject.beforeEach)(()=>{
|
|
28
|
+
schemaFactoryMock.mockClear();
|
|
29
|
+
schemaFactoryImplementation.mockClear();
|
|
30
|
+
isBooleanMock.mockReset();
|
|
31
|
+
isBooleanMock.mockReturnValue(true);
|
|
32
|
+
});
|
|
33
|
+
(0, external_vitest_namespaceObject.afterEach)(()=>{
|
|
34
|
+
isBooleanMock.mockClear();
|
|
35
|
+
});
|
|
36
|
+
(0, external_vitest_namespaceObject.afterAll)(()=>{
|
|
37
|
+
schemaFactoryMock.mockReset();
|
|
38
|
+
schemaFactoryImplementation.mockReset();
|
|
39
|
+
isBooleanMock.mockReset();
|
|
40
|
+
});
|
|
41
|
+
(0, external_vitest_namespaceObject.describe)('happy', ()=>{
|
|
42
|
+
(0, external_vitest_namespaceObject.it)('should delegate to schemaFactory when using default metadata', ()=>{
|
|
43
|
+
const result = (0, external_boolean_cjs_namespaceObject.boolean)();
|
|
44
|
+
(0, external_vitest_namespaceObject.expect)(schemaFactoryMock).toHaveBeenCalledTimes(1);
|
|
45
|
+
const factoryInput = getFactoryInput();
|
|
46
|
+
(0, external_vitest_namespaceObject.expect)(factoryInput.name).toBe('boolean');
|
|
47
|
+
(0, external_vitest_namespaceObject.expect)(factoryInput.type).toBe('boolean');
|
|
48
|
+
(0, external_vitest_namespaceObject.expect)(factoryInput.doc).toBeUndefined();
|
|
49
|
+
(0, external_vitest_namespaceObject.expect)(factoryInput.ast(schemaContextMock)).toEqual({
|
|
50
|
+
type: 'boolean',
|
|
51
|
+
name: 'boolean'
|
|
52
|
+
});
|
|
53
|
+
(0, external_vitest_namespaceObject.expect)(result).toBe(schemaFactoryMock.mock.results[0]?.value);
|
|
54
|
+
});
|
|
55
|
+
(0, external_vitest_namespaceObject.it)('should pass custom metadata to schemaFactory when options are provided', ()=>{
|
|
56
|
+
const doc = {
|
|
57
|
+
summary: 'bool doc'
|
|
58
|
+
};
|
|
59
|
+
(0, external_boolean_cjs_namespaceObject.boolean)({
|
|
60
|
+
name: 'Flag',
|
|
61
|
+
doc
|
|
62
|
+
});
|
|
63
|
+
const factoryInput = getFactoryInput();
|
|
64
|
+
(0, external_vitest_namespaceObject.expect)(factoryInput.name).toBe('Flag');
|
|
65
|
+
(0, external_vitest_namespaceObject.expect)(factoryInput.doc).toEqual(doc);
|
|
66
|
+
(0, external_vitest_namespaceObject.expect)(factoryInput.ast(schemaContextMock)).toEqual({
|
|
67
|
+
type: 'boolean',
|
|
68
|
+
name: 'Flag'
|
|
69
|
+
});
|
|
70
|
+
});
|
|
71
|
+
(0, external_vitest_namespaceObject.it)('should validate input through isBoolean when input is boolean', ()=>{
|
|
72
|
+
(0, external_boolean_cjs_namespaceObject.boolean)();
|
|
73
|
+
const factoryInput = getFactoryInput();
|
|
74
|
+
const parsed = factoryInput.validate(true, schemaContextMock);
|
|
75
|
+
(0, external_vitest_namespaceObject.expect)(parsed).toBe(true);
|
|
76
|
+
(0, external_vitest_namespaceObject.expect)(isBooleanMock).toHaveBeenCalledWith(true);
|
|
77
|
+
});
|
|
78
|
+
});
|
|
79
|
+
(0, external_vitest_namespaceObject.describe)('sad', ()=>{
|
|
80
|
+
(0, external_vitest_namespaceObject.it)('should throw schema error payload when input is not a boolean', ()=>{
|
|
81
|
+
isBooleanMock.mockReturnValue(false);
|
|
82
|
+
(0, external_boolean_cjs_namespaceObject.boolean)();
|
|
83
|
+
const factoryInput = getFactoryInput();
|
|
84
|
+
const thrown = (0, index_cjs_namespaceObject.captureThrow)(()=>factoryInput.validate('x', schemaContextMock));
|
|
85
|
+
(0, external_vitest_namespaceObject.expect)(thrown.threw).toBe(true);
|
|
86
|
+
(0, external_vitest_namespaceObject.expect)(thrown.value).toEqual({
|
|
87
|
+
message: 'Data is not a boolean',
|
|
88
|
+
code: 'boolean.type'
|
|
89
|
+
});
|
|
90
|
+
});
|
|
91
|
+
});
|
|
92
|
+
});
|
|
93
|
+
for(var __rspack_i in __webpack_exports__)exports[__rspack_i] = __webpack_exports__[__rspack_i];
|
|
94
|
+
Object.defineProperty(exports, '__esModule', {
|
|
95
|
+
value: true
|
|
96
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
|
|
2
|
+
import { boolean as external_boolean_js_boolean } from "./boolean.js";
|
|
3
|
+
import { schemaFactory } from "./schemaFactory.js";
|
|
4
|
+
import { isBoolean } 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
|
+
isBoolean: vi.fn()
|
|
11
|
+
}));
|
|
12
|
+
const schemaFactoryMock = vi.mocked(schemaFactory);
|
|
13
|
+
const isBooleanMock = vi.mocked(isBoolean);
|
|
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('boolean()', ()=>{
|
|
22
|
+
beforeAll(()=>{
|
|
23
|
+
schemaFactoryMock.mockImplementation(schemaFactoryImplementation);
|
|
24
|
+
});
|
|
25
|
+
beforeEach(()=>{
|
|
26
|
+
schemaFactoryMock.mockClear();
|
|
27
|
+
schemaFactoryImplementation.mockClear();
|
|
28
|
+
isBooleanMock.mockReset();
|
|
29
|
+
isBooleanMock.mockReturnValue(true);
|
|
30
|
+
});
|
|
31
|
+
afterEach(()=>{
|
|
32
|
+
isBooleanMock.mockClear();
|
|
33
|
+
});
|
|
34
|
+
afterAll(()=>{
|
|
35
|
+
schemaFactoryMock.mockReset();
|
|
36
|
+
schemaFactoryImplementation.mockReset();
|
|
37
|
+
isBooleanMock.mockReset();
|
|
38
|
+
});
|
|
39
|
+
describe('happy', ()=>{
|
|
40
|
+
it('should delegate to schemaFactory when using default metadata', ()=>{
|
|
41
|
+
const result = external_boolean_js_boolean();
|
|
42
|
+
expect(schemaFactoryMock).toHaveBeenCalledTimes(1);
|
|
43
|
+
const factoryInput = getFactoryInput();
|
|
44
|
+
expect(factoryInput.name).toBe('boolean');
|
|
45
|
+
expect(factoryInput.type).toBe('boolean');
|
|
46
|
+
expect(factoryInput.doc).toBeUndefined();
|
|
47
|
+
expect(factoryInput.ast(schemaContextMock)).toEqual({
|
|
48
|
+
type: 'boolean',
|
|
49
|
+
name: 'boolean'
|
|
50
|
+
});
|
|
51
|
+
expect(result).toBe(schemaFactoryMock.mock.results[0]?.value);
|
|
52
|
+
});
|
|
53
|
+
it('should pass custom metadata to schemaFactory when options are provided', ()=>{
|
|
54
|
+
const doc = {
|
|
55
|
+
summary: 'bool doc'
|
|
56
|
+
};
|
|
57
|
+
external_boolean_js_boolean({
|
|
58
|
+
name: 'Flag',
|
|
59
|
+
doc
|
|
60
|
+
});
|
|
61
|
+
const factoryInput = getFactoryInput();
|
|
62
|
+
expect(factoryInput.name).toBe('Flag');
|
|
63
|
+
expect(factoryInput.doc).toEqual(doc);
|
|
64
|
+
expect(factoryInput.ast(schemaContextMock)).toEqual({
|
|
65
|
+
type: 'boolean',
|
|
66
|
+
name: 'Flag'
|
|
67
|
+
});
|
|
68
|
+
});
|
|
69
|
+
it('should validate input through isBoolean when input is boolean', ()=>{
|
|
70
|
+
external_boolean_js_boolean();
|
|
71
|
+
const factoryInput = getFactoryInput();
|
|
72
|
+
const parsed = factoryInput.validate(true, schemaContextMock);
|
|
73
|
+
expect(parsed).toBe(true);
|
|
74
|
+
expect(isBooleanMock).toHaveBeenCalledWith(true);
|
|
75
|
+
});
|
|
76
|
+
});
|
|
77
|
+
describe('sad', ()=>{
|
|
78
|
+
it('should throw schema error payload when input is not a boolean', ()=>{
|
|
79
|
+
isBooleanMock.mockReturnValue(false);
|
|
80
|
+
external_boolean_js_boolean();
|
|
81
|
+
const factoryInput = getFactoryInput();
|
|
82
|
+
const thrown = captureThrow(()=>factoryInput.validate('x', schemaContextMock));
|
|
83
|
+
expect(thrown.threw).toBe(true);
|
|
84
|
+
expect(thrown.value).toEqual({
|
|
85
|
+
message: 'Data is not a boolean',
|
|
86
|
+
code: 'boolean.type'
|
|
87
|
+
});
|
|
88
|
+
});
|
|
89
|
+
});
|
|
90
|
+
});
|
package/dist/context.cjs
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
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
|
+
createBuilder: ()=>createBuilder,
|
|
28
|
+
createSchemaContext: ()=>createSchemaContext,
|
|
29
|
+
createState: ()=>createState,
|
|
30
|
+
isNormalizedRequest: ()=>isNormalizedRequest,
|
|
31
|
+
normalizeBuildContext: ()=>normalizeBuildContext,
|
|
32
|
+
normalizeRequestContext: ()=>normalizeRequestContext
|
|
33
|
+
});
|
|
34
|
+
const createBuilder = ()=>{
|
|
35
|
+
const nodes = [];
|
|
36
|
+
return {
|
|
37
|
+
add: (node)=>{
|
|
38
|
+
nodes.push(node);
|
|
39
|
+
return node;
|
|
40
|
+
},
|
|
41
|
+
getAll: ()=>nodes
|
|
42
|
+
};
|
|
43
|
+
};
|
|
44
|
+
const createState = ()=>{
|
|
45
|
+
const store = new Map();
|
|
46
|
+
return {
|
|
47
|
+
get: (key)=>store.get(key),
|
|
48
|
+
set: (key, value)=>{
|
|
49
|
+
store.set(key, value);
|
|
50
|
+
},
|
|
51
|
+
update: (key, updater)=>{
|
|
52
|
+
const next = updater(store.get(key));
|
|
53
|
+
store.set(key, next);
|
|
54
|
+
},
|
|
55
|
+
snapshot: ()=>Array.from(store.entries()).reduce((acc, [key, value])=>{
|
|
56
|
+
acc[key] = value;
|
|
57
|
+
return acc;
|
|
58
|
+
}, {})
|
|
59
|
+
};
|
|
60
|
+
};
|
|
61
|
+
const normalizeBuildContext = (input = {})=>({
|
|
62
|
+
buildId: input.buildId ?? `${Date.now()}-${Math.random().toString(16).slice(2)}`,
|
|
63
|
+
builder: input.builder ?? createBuilder(),
|
|
64
|
+
parentNode: input.parentNode,
|
|
65
|
+
schemaPath: input.schemaPath ?? [],
|
|
66
|
+
buildOptions: input.buildOptions ?? {}
|
|
67
|
+
});
|
|
68
|
+
const isNormalizedRequest = (input)=>true === input.normalized;
|
|
69
|
+
const normalizeRequestContext = (input = {})=>{
|
|
70
|
+
if (isNormalizedRequest(input)) return input;
|
|
71
|
+
return {
|
|
72
|
+
normalized: true,
|
|
73
|
+
requestId: input.requestId ?? `${Date.now()}-${Math.random().toString(16).slice(2)}`,
|
|
74
|
+
timestamp: input.timestamp ?? Date.now(),
|
|
75
|
+
correlationId: input.correlationId,
|
|
76
|
+
sourceId: input.sourceId,
|
|
77
|
+
userId: input.userId,
|
|
78
|
+
tenantId: input.tenantId,
|
|
79
|
+
metadata: input.metadata,
|
|
80
|
+
state: createState(),
|
|
81
|
+
publisher: input.publisher,
|
|
82
|
+
onPublishError: input.onPublishError,
|
|
83
|
+
logger: input.logger
|
|
84
|
+
};
|
|
85
|
+
};
|
|
86
|
+
const createSchemaContext = (input = {})=>{
|
|
87
|
+
let buildContext = input.build ? normalizeBuildContext(input.build) : void 0;
|
|
88
|
+
let requestContext = input.request ? normalizeRequestContext(input.request) : void 0;
|
|
89
|
+
const state = requestContext?.state ?? createState();
|
|
90
|
+
return {
|
|
91
|
+
getBuildContext: ()=>buildContext,
|
|
92
|
+
setBuildContext: (next)=>{
|
|
93
|
+
buildContext = next;
|
|
94
|
+
},
|
|
95
|
+
getRequestContext: ()=>requestContext,
|
|
96
|
+
get request () {
|
|
97
|
+
return requestContext;
|
|
98
|
+
},
|
|
99
|
+
setRequestContext: (next)=>{
|
|
100
|
+
requestContext = next ? normalizeRequestContext(next) : void 0;
|
|
101
|
+
if (requestContext && requestContext.state !== state) requestContext = {
|
|
102
|
+
...requestContext,
|
|
103
|
+
state
|
|
104
|
+
};
|
|
105
|
+
},
|
|
106
|
+
state
|
|
107
|
+
};
|
|
108
|
+
};
|
|
109
|
+
exports.createBuilder = __webpack_exports__.createBuilder;
|
|
110
|
+
exports.createSchemaContext = __webpack_exports__.createSchemaContext;
|
|
111
|
+
exports.createState = __webpack_exports__.createState;
|
|
112
|
+
exports.isNormalizedRequest = __webpack_exports__.isNormalizedRequest;
|
|
113
|
+
exports.normalizeBuildContext = __webpack_exports__.normalizeBuildContext;
|
|
114
|
+
exports.normalizeRequestContext = __webpack_exports__.normalizeRequestContext;
|
|
115
|
+
for(var __rspack_i in __webpack_exports__)if (-1 === [
|
|
116
|
+
"createBuilder",
|
|
117
|
+
"createSchemaContext",
|
|
118
|
+
"createState",
|
|
119
|
+
"isNormalizedRequest",
|
|
120
|
+
"normalizeBuildContext",
|
|
121
|
+
"normalizeRequestContext"
|
|
122
|
+
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
|
|
123
|
+
Object.defineProperty(exports, '__esModule', {
|
|
124
|
+
value: true
|
|
125
|
+
});
|