@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,74 @@
|
|
|
1
|
+
import { vi } from "vitest";
|
|
2
|
+
const createSchemaStateMock = (overrides = {})=>({
|
|
3
|
+
get: vi.fn(()=>void 0),
|
|
4
|
+
set: vi.fn(()=>void 0),
|
|
5
|
+
update: vi.fn(()=>void 0),
|
|
6
|
+
snapshot: vi.fn(()=>({})),
|
|
7
|
+
...overrides
|
|
8
|
+
});
|
|
9
|
+
const createSchemaContextMock = (overrides = {})=>{
|
|
10
|
+
const state = overrides.state ?? createSchemaStateMock();
|
|
11
|
+
const getBuildContext = vi.fn(()=>overrides.buildContext);
|
|
12
|
+
const setBuildContext = vi.fn(()=>void 0);
|
|
13
|
+
const getRequestContext = vi.fn(()=>overrides.requestContext);
|
|
14
|
+
const setRequestContext = vi.fn((_input)=>void 0);
|
|
15
|
+
return {
|
|
16
|
+
getBuildContext,
|
|
17
|
+
setBuildContext,
|
|
18
|
+
getRequestContext,
|
|
19
|
+
setRequestContext,
|
|
20
|
+
get request () {
|
|
21
|
+
return overrides.requestContext;
|
|
22
|
+
},
|
|
23
|
+
state,
|
|
24
|
+
...overrides
|
|
25
|
+
};
|
|
26
|
+
};
|
|
27
|
+
const createDefaultAstNode = (name, type)=>({
|
|
28
|
+
name,
|
|
29
|
+
type
|
|
30
|
+
});
|
|
31
|
+
const createBaseSchemaMock = (overrides = {})=>{
|
|
32
|
+
const name = overrides.name ?? 'schema.mock';
|
|
33
|
+
const type = overrides.type ?? 'schema.mock';
|
|
34
|
+
const astNode = overrides.astNode ?? createDefaultAstNode(name, type);
|
|
35
|
+
const outputValue = overrides.outputValue;
|
|
36
|
+
const validateResult = overrides.validateResult ?? {
|
|
37
|
+
ok: true,
|
|
38
|
+
value: outputValue
|
|
39
|
+
};
|
|
40
|
+
const schema = {};
|
|
41
|
+
schema.name = name;
|
|
42
|
+
schema.type = type;
|
|
43
|
+
schema.ast = vi.fn(()=>astNode);
|
|
44
|
+
schema.validate = vi.fn(()=>validateResult);
|
|
45
|
+
schema.parse = vi.fn(()=>outputValue);
|
|
46
|
+
schema.typed = vi.fn((input)=>input);
|
|
47
|
+
schema.optional = vi.fn(()=>schema);
|
|
48
|
+
schema.nullable = vi.fn(()=>schema);
|
|
49
|
+
schema.describe = vi.fn(()=>schema);
|
|
50
|
+
schema.refine = vi.fn(()=>schema);
|
|
51
|
+
schema.before = vi.fn(()=>schema);
|
|
52
|
+
schema.after = vi.fn(()=>schema);
|
|
53
|
+
schema.and = vi.fn(()=>schema);
|
|
54
|
+
return {
|
|
55
|
+
...schema,
|
|
56
|
+
...overrides
|
|
57
|
+
};
|
|
58
|
+
};
|
|
59
|
+
const createSchemaWithChainMock = (input = {})=>{
|
|
60
|
+
const schema = createBaseSchemaMock({
|
|
61
|
+
name: input.name,
|
|
62
|
+
type: input.type
|
|
63
|
+
});
|
|
64
|
+
(input.chainKeys ?? []).forEach((chainKey)=>{
|
|
65
|
+
schema[chainKey] = vi.fn(()=>schema);
|
|
66
|
+
});
|
|
67
|
+
return input.overrides ? Object.assign(schema, input.overrides) : schema;
|
|
68
|
+
};
|
|
69
|
+
const createSchemaFactoryMock = ()=>vi.fn((factoryInput)=>createSchemaWithChainMock({
|
|
70
|
+
name: factoryInput.name,
|
|
71
|
+
type: factoryInput.type,
|
|
72
|
+
chainKeys: Object.keys(factoryInput.chain ?? {})
|
|
73
|
+
}));
|
|
74
|
+
export { createBaseSchemaMock, createSchemaContextMock, createSchemaFactoryMock, createSchemaStateMock, createSchemaWithChainMock };
|
package/dist/tuple.cjs
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
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
|
+
tuple: ()=>tuple
|
|
28
|
+
});
|
|
29
|
+
const external_schemaFactory_cjs_namespaceObject = require("./schemaFactory.cjs");
|
|
30
|
+
const external_typeGuards_cjs_namespaceObject = require("./typeGuards.cjs");
|
|
31
|
+
const tuple = ({ name, items, doc })=>(0, external_schemaFactory_cjs_namespaceObject.schemaFactory)({
|
|
32
|
+
name,
|
|
33
|
+
type: 'tuple',
|
|
34
|
+
doc,
|
|
35
|
+
ast: (ctx)=>{
|
|
36
|
+
const build = ctx.getBuildContext();
|
|
37
|
+
return {
|
|
38
|
+
type: 'tuple',
|
|
39
|
+
name,
|
|
40
|
+
children: items.map((item)=>item.ast(build ?? void 0))
|
|
41
|
+
};
|
|
42
|
+
},
|
|
43
|
+
validate: (input, ctx)=>{
|
|
44
|
+
if (!(0, external_typeGuards_cjs_namespaceObject.isArray)()(input)) throw {
|
|
45
|
+
message: 'Expected tuple',
|
|
46
|
+
code: 'tuple.type'
|
|
47
|
+
};
|
|
48
|
+
const result = items.map((schema, index)=>schema.parse(input[index], ctx));
|
|
49
|
+
return result;
|
|
50
|
+
}
|
|
51
|
+
});
|
|
52
|
+
exports.tuple = __webpack_exports__.tuple;
|
|
53
|
+
for(var __rspack_i in __webpack_exports__)if (-1 === [
|
|
54
|
+
"tuple"
|
|
55
|
+
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
|
|
56
|
+
Object.defineProperty(exports, '__esModule', {
|
|
57
|
+
value: true
|
|
58
|
+
});
|
package/dist/tuple.d.ts
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { Schema, SchemaDoc } from './types.js';
|
|
2
|
+
type AnySchema = Schema<any>;
|
|
3
|
+
export interface TupleSchemaInput<TItems extends readonly AnySchema[]> {
|
|
4
|
+
name: string;
|
|
5
|
+
items: TItems;
|
|
6
|
+
doc?: SchemaDoc;
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* tuple is part of the public LIVON API.
|
|
10
|
+
*
|
|
11
|
+
* @remarks
|
|
12
|
+
* Parameter and return types are defined in the TypeScript signature.
|
|
13
|
+
*
|
|
14
|
+
* @see https://live-input-vector-output-node.github.io/livon-ts/docs/schema/tuple
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* // Creates a tuple schema for numeric x/y coordinates.
|
|
18
|
+
* const Coordinates = tuple({
|
|
19
|
+
* name: 'coordinates',
|
|
20
|
+
* items: [number(), number()] as const,
|
|
21
|
+
* });
|
|
22
|
+
* Coordinates.parse([10, 20]);
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
* // Extends tuple validation to also allow undefined.
|
|
26
|
+
* const MaybeCoordinates = tuple({
|
|
27
|
+
* name: 'coordinates',
|
|
28
|
+
* items: [number(), number()] as const,
|
|
29
|
+
* }).optional();
|
|
30
|
+
* MaybeCoordinates.parse(undefined);
|
|
31
|
+
*/
|
|
32
|
+
export declare const tuple: <TItems extends readonly AnySchema[]>({ name, items, doc, }: TupleSchemaInput<TItems>) => import("./schemaFactory.js").SchemaWithChain<{ [K in keyof TItems]: ReturnType<TItems[K]["parse"]>; }, import("./schemaFactory.js").SchemaFactoryChainDefinition<{ [K in keyof TItems]: ReturnType<TItems[K]["parse"]>; }>>;
|
|
33
|
+
export {};
|
package/dist/tuple.js
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { schemaFactory } from "./schemaFactory.js";
|
|
2
|
+
import { isArray } from "./typeGuards.js";
|
|
3
|
+
const tuple = ({ name, items, doc })=>schemaFactory({
|
|
4
|
+
name,
|
|
5
|
+
type: 'tuple',
|
|
6
|
+
doc,
|
|
7
|
+
ast: (ctx)=>{
|
|
8
|
+
const build = ctx.getBuildContext();
|
|
9
|
+
return {
|
|
10
|
+
type: 'tuple',
|
|
11
|
+
name,
|
|
12
|
+
children: items.map((item)=>item.ast(build ?? void 0))
|
|
13
|
+
};
|
|
14
|
+
},
|
|
15
|
+
validate: (input, ctx)=>{
|
|
16
|
+
if (!isArray()(input)) throw {
|
|
17
|
+
message: 'Expected tuple',
|
|
18
|
+
code: 'tuple.type'
|
|
19
|
+
};
|
|
20
|
+
const result = items.map((schema, index)=>schema.parse(input[index], ctx));
|
|
21
|
+
return result;
|
|
22
|
+
}
|
|
23
|
+
});
|
|
24
|
+
export { tuple };
|
|
@@ -0,0 +1,162 @@
|
|
|
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_tuple_cjs_namespaceObject = require("./tuple.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-tuple',
|
|
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 firstSchemaMock = (0, index_cjs_namespaceObject.createBaseSchemaMock)({
|
|
32
|
+
name: 'first',
|
|
33
|
+
parse: external_vitest_namespaceObject.vi.fn((value)=>`first:${String(value)}`),
|
|
34
|
+
ast: external_vitest_namespaceObject.vi.fn(()=>({
|
|
35
|
+
type: 'string',
|
|
36
|
+
name: 'First'
|
|
37
|
+
}))
|
|
38
|
+
});
|
|
39
|
+
const secondSchemaMock = (0, index_cjs_namespaceObject.createBaseSchemaMock)({
|
|
40
|
+
name: 'second',
|
|
41
|
+
parse: external_vitest_namespaceObject.vi.fn((value)=>Number(value)),
|
|
42
|
+
ast: external_vitest_namespaceObject.vi.fn(()=>({
|
|
43
|
+
type: 'number',
|
|
44
|
+
name: 'Second'
|
|
45
|
+
}))
|
|
46
|
+
});
|
|
47
|
+
const getFactoryInput = ()=>{
|
|
48
|
+
const call = schemaFactoryMock.mock.calls[0];
|
|
49
|
+
if (!call) throw new Error('schemaFactory should be called once before reading input');
|
|
50
|
+
return call[0];
|
|
51
|
+
};
|
|
52
|
+
(0, external_vitest_namespaceObject.describe)('tuple()', ()=>{
|
|
53
|
+
(0, external_vitest_namespaceObject.beforeAll)(()=>{
|
|
54
|
+
schemaFactoryMock.mockImplementation(schemaFactoryImplementation);
|
|
55
|
+
isArrayMock.mockImplementation(()=>isArrayGuard);
|
|
56
|
+
});
|
|
57
|
+
(0, external_vitest_namespaceObject.beforeEach)(()=>{
|
|
58
|
+
schemaFactoryMock.mockClear();
|
|
59
|
+
schemaFactoryImplementation.mockClear();
|
|
60
|
+
isArrayMock.mockClear();
|
|
61
|
+
isArrayGuardSpy.mockClear();
|
|
62
|
+
external_vitest_namespaceObject.vi.mocked(firstSchemaMock.parse).mockClear();
|
|
63
|
+
external_vitest_namespaceObject.vi.mocked(firstSchemaMock.ast).mockClear();
|
|
64
|
+
external_vitest_namespaceObject.vi.mocked(secondSchemaMock.parse).mockClear();
|
|
65
|
+
external_vitest_namespaceObject.vi.mocked(secondSchemaMock.ast).mockClear();
|
|
66
|
+
});
|
|
67
|
+
(0, external_vitest_namespaceObject.afterEach)(()=>{
|
|
68
|
+
external_vitest_namespaceObject.vi.mocked(firstSchemaMock.parse).mockClear();
|
|
69
|
+
external_vitest_namespaceObject.vi.mocked(secondSchemaMock.parse).mockClear();
|
|
70
|
+
});
|
|
71
|
+
(0, external_vitest_namespaceObject.afterAll)(()=>{
|
|
72
|
+
schemaFactoryMock.mockReset();
|
|
73
|
+
schemaFactoryImplementation.mockReset();
|
|
74
|
+
isArrayMock.mockReset();
|
|
75
|
+
});
|
|
76
|
+
(0, external_vitest_namespaceObject.describe)('happy', ()=>{
|
|
77
|
+
(0, external_vitest_namespaceObject.it)('should delegate to schemaFactory when tuple schema is created', ()=>{
|
|
78
|
+
const result = (0, external_tuple_cjs_namespaceObject.tuple)({
|
|
79
|
+
name: 'Coordinates',
|
|
80
|
+
items: [
|
|
81
|
+
firstSchemaMock,
|
|
82
|
+
secondSchemaMock
|
|
83
|
+
]
|
|
84
|
+
});
|
|
85
|
+
(0, external_vitest_namespaceObject.expect)(schemaFactoryMock).toHaveBeenCalledTimes(1);
|
|
86
|
+
const factoryInput = getFactoryInput();
|
|
87
|
+
(0, external_vitest_namespaceObject.expect)(factoryInput.name).toBe('Coordinates');
|
|
88
|
+
(0, external_vitest_namespaceObject.expect)(factoryInput.type).toBe('tuple');
|
|
89
|
+
(0, external_vitest_namespaceObject.expect)(result).toBe(schemaFactoryMock.mock.results[0]?.value);
|
|
90
|
+
});
|
|
91
|
+
(0, external_vitest_namespaceObject.it)('should build ast from tuple item schemas when build context is available', ()=>{
|
|
92
|
+
(0, external_tuple_cjs_namespaceObject.tuple)({
|
|
93
|
+
name: 'Coordinates',
|
|
94
|
+
items: [
|
|
95
|
+
firstSchemaMock,
|
|
96
|
+
secondSchemaMock
|
|
97
|
+
]
|
|
98
|
+
});
|
|
99
|
+
const factoryInput = getFactoryInput();
|
|
100
|
+
const ast = factoryInput.ast(schemaContextMock);
|
|
101
|
+
(0, external_vitest_namespaceObject.expect)(ast).toEqual({
|
|
102
|
+
type: 'tuple',
|
|
103
|
+
name: 'Coordinates',
|
|
104
|
+
children: [
|
|
105
|
+
{
|
|
106
|
+
type: 'string',
|
|
107
|
+
name: 'First'
|
|
108
|
+
},
|
|
109
|
+
{
|
|
110
|
+
type: 'number',
|
|
111
|
+
name: 'Second'
|
|
112
|
+
}
|
|
113
|
+
]
|
|
114
|
+
});
|
|
115
|
+
(0, external_vitest_namespaceObject.expect)(firstSchemaMock.ast).toHaveBeenCalledWith(buildContextMock);
|
|
116
|
+
(0, external_vitest_namespaceObject.expect)(secondSchemaMock.ast).toHaveBeenCalledWith(buildContextMock);
|
|
117
|
+
});
|
|
118
|
+
(0, external_vitest_namespaceObject.it)('should parse tuple elements when input is an array', ()=>{
|
|
119
|
+
(0, external_tuple_cjs_namespaceObject.tuple)({
|
|
120
|
+
name: 'Coordinates',
|
|
121
|
+
items: [
|
|
122
|
+
firstSchemaMock,
|
|
123
|
+
secondSchemaMock
|
|
124
|
+
]
|
|
125
|
+
});
|
|
126
|
+
const factoryInput = getFactoryInput();
|
|
127
|
+
const parsed = factoryInput.validate([
|
|
128
|
+
'x',
|
|
129
|
+
'2'
|
|
130
|
+
], schemaContextMock);
|
|
131
|
+
(0, external_vitest_namespaceObject.expect)(parsed).toEqual([
|
|
132
|
+
'first:x',
|
|
133
|
+
2
|
|
134
|
+
]);
|
|
135
|
+
(0, external_vitest_namespaceObject.expect)(firstSchemaMock.parse).toHaveBeenCalledWith('x', schemaContextMock);
|
|
136
|
+
(0, external_vitest_namespaceObject.expect)(secondSchemaMock.parse).toHaveBeenCalledWith('2', schemaContextMock);
|
|
137
|
+
});
|
|
138
|
+
});
|
|
139
|
+
(0, external_vitest_namespaceObject.describe)('sad', ()=>{
|
|
140
|
+
(0, external_vitest_namespaceObject.it)('should throw tuple.type when input is not an array', ()=>{
|
|
141
|
+
isArrayGuardSpy.mockReturnValueOnce(false);
|
|
142
|
+
(0, external_tuple_cjs_namespaceObject.tuple)({
|
|
143
|
+
name: 'Coordinates',
|
|
144
|
+
items: [
|
|
145
|
+
firstSchemaMock,
|
|
146
|
+
secondSchemaMock
|
|
147
|
+
]
|
|
148
|
+
});
|
|
149
|
+
const factoryInput = getFactoryInput();
|
|
150
|
+
const thrown = (0, index_cjs_namespaceObject.captureThrow)(()=>factoryInput.validate('x', schemaContextMock));
|
|
151
|
+
(0, external_vitest_namespaceObject.expect)(thrown.threw).toBe(true);
|
|
152
|
+
(0, external_vitest_namespaceObject.expect)(thrown.value).toEqual({
|
|
153
|
+
message: 'Expected tuple',
|
|
154
|
+
code: 'tuple.type'
|
|
155
|
+
});
|
|
156
|
+
});
|
|
157
|
+
});
|
|
158
|
+
});
|
|
159
|
+
for(var __rspack_i in __webpack_exports__)exports[__rspack_i] = __webpack_exports__[__rspack_i];
|
|
160
|
+
Object.defineProperty(exports, '__esModule', {
|
|
161
|
+
value: true
|
|
162
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
|
|
2
|
+
import { schemaFactory } from "./schemaFactory.js";
|
|
3
|
+
import { tuple } from "./tuple.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-tuple',
|
|
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 firstSchemaMock = createBaseSchemaMock({
|
|
30
|
+
name: 'first',
|
|
31
|
+
parse: vi.fn((value)=>`first:${String(value)}`),
|
|
32
|
+
ast: vi.fn(()=>({
|
|
33
|
+
type: 'string',
|
|
34
|
+
name: 'First'
|
|
35
|
+
}))
|
|
36
|
+
});
|
|
37
|
+
const secondSchemaMock = createBaseSchemaMock({
|
|
38
|
+
name: 'second',
|
|
39
|
+
parse: vi.fn((value)=>Number(value)),
|
|
40
|
+
ast: vi.fn(()=>({
|
|
41
|
+
type: 'number',
|
|
42
|
+
name: 'Second'
|
|
43
|
+
}))
|
|
44
|
+
});
|
|
45
|
+
const getFactoryInput = ()=>{
|
|
46
|
+
const call = schemaFactoryMock.mock.calls[0];
|
|
47
|
+
if (!call) throw new Error('schemaFactory should be called once before reading input');
|
|
48
|
+
return call[0];
|
|
49
|
+
};
|
|
50
|
+
describe('tuple()', ()=>{
|
|
51
|
+
beforeAll(()=>{
|
|
52
|
+
schemaFactoryMock.mockImplementation(schemaFactoryImplementation);
|
|
53
|
+
isArrayMock.mockImplementation(()=>isArrayGuard);
|
|
54
|
+
});
|
|
55
|
+
beforeEach(()=>{
|
|
56
|
+
schemaFactoryMock.mockClear();
|
|
57
|
+
schemaFactoryImplementation.mockClear();
|
|
58
|
+
isArrayMock.mockClear();
|
|
59
|
+
isArrayGuardSpy.mockClear();
|
|
60
|
+
vi.mocked(firstSchemaMock.parse).mockClear();
|
|
61
|
+
vi.mocked(firstSchemaMock.ast).mockClear();
|
|
62
|
+
vi.mocked(secondSchemaMock.parse).mockClear();
|
|
63
|
+
vi.mocked(secondSchemaMock.ast).mockClear();
|
|
64
|
+
});
|
|
65
|
+
afterEach(()=>{
|
|
66
|
+
vi.mocked(firstSchemaMock.parse).mockClear();
|
|
67
|
+
vi.mocked(secondSchemaMock.parse).mockClear();
|
|
68
|
+
});
|
|
69
|
+
afterAll(()=>{
|
|
70
|
+
schemaFactoryMock.mockReset();
|
|
71
|
+
schemaFactoryImplementation.mockReset();
|
|
72
|
+
isArrayMock.mockReset();
|
|
73
|
+
});
|
|
74
|
+
describe('happy', ()=>{
|
|
75
|
+
it('should delegate to schemaFactory when tuple schema is created', ()=>{
|
|
76
|
+
const result = tuple({
|
|
77
|
+
name: 'Coordinates',
|
|
78
|
+
items: [
|
|
79
|
+
firstSchemaMock,
|
|
80
|
+
secondSchemaMock
|
|
81
|
+
]
|
|
82
|
+
});
|
|
83
|
+
expect(schemaFactoryMock).toHaveBeenCalledTimes(1);
|
|
84
|
+
const factoryInput = getFactoryInput();
|
|
85
|
+
expect(factoryInput.name).toBe('Coordinates');
|
|
86
|
+
expect(factoryInput.type).toBe('tuple');
|
|
87
|
+
expect(result).toBe(schemaFactoryMock.mock.results[0]?.value);
|
|
88
|
+
});
|
|
89
|
+
it('should build ast from tuple item schemas when build context is available', ()=>{
|
|
90
|
+
tuple({
|
|
91
|
+
name: 'Coordinates',
|
|
92
|
+
items: [
|
|
93
|
+
firstSchemaMock,
|
|
94
|
+
secondSchemaMock
|
|
95
|
+
]
|
|
96
|
+
});
|
|
97
|
+
const factoryInput = getFactoryInput();
|
|
98
|
+
const ast = factoryInput.ast(schemaContextMock);
|
|
99
|
+
expect(ast).toEqual({
|
|
100
|
+
type: 'tuple',
|
|
101
|
+
name: 'Coordinates',
|
|
102
|
+
children: [
|
|
103
|
+
{
|
|
104
|
+
type: 'string',
|
|
105
|
+
name: 'First'
|
|
106
|
+
},
|
|
107
|
+
{
|
|
108
|
+
type: 'number',
|
|
109
|
+
name: 'Second'
|
|
110
|
+
}
|
|
111
|
+
]
|
|
112
|
+
});
|
|
113
|
+
expect(firstSchemaMock.ast).toHaveBeenCalledWith(buildContextMock);
|
|
114
|
+
expect(secondSchemaMock.ast).toHaveBeenCalledWith(buildContextMock);
|
|
115
|
+
});
|
|
116
|
+
it('should parse tuple elements when input is an array', ()=>{
|
|
117
|
+
tuple({
|
|
118
|
+
name: 'Coordinates',
|
|
119
|
+
items: [
|
|
120
|
+
firstSchemaMock,
|
|
121
|
+
secondSchemaMock
|
|
122
|
+
]
|
|
123
|
+
});
|
|
124
|
+
const factoryInput = getFactoryInput();
|
|
125
|
+
const parsed = factoryInput.validate([
|
|
126
|
+
'x',
|
|
127
|
+
'2'
|
|
128
|
+
], schemaContextMock);
|
|
129
|
+
expect(parsed).toEqual([
|
|
130
|
+
'first:x',
|
|
131
|
+
2
|
|
132
|
+
]);
|
|
133
|
+
expect(firstSchemaMock.parse).toHaveBeenCalledWith('x', schemaContextMock);
|
|
134
|
+
expect(secondSchemaMock.parse).toHaveBeenCalledWith('2', schemaContextMock);
|
|
135
|
+
});
|
|
136
|
+
});
|
|
137
|
+
describe('sad', ()=>{
|
|
138
|
+
it('should throw tuple.type when input is not an array', ()=>{
|
|
139
|
+
isArrayGuardSpy.mockReturnValueOnce(false);
|
|
140
|
+
tuple({
|
|
141
|
+
name: 'Coordinates',
|
|
142
|
+
items: [
|
|
143
|
+
firstSchemaMock,
|
|
144
|
+
secondSchemaMock
|
|
145
|
+
]
|
|
146
|
+
});
|
|
147
|
+
const factoryInput = getFactoryInput();
|
|
148
|
+
const thrown = captureThrow(()=>factoryInput.validate('x', schemaContextMock));
|
|
149
|
+
expect(thrown.threw).toBe(true);
|
|
150
|
+
expect(thrown.value).toEqual({
|
|
151
|
+
message: 'Expected tuple',
|
|
152
|
+
code: 'tuple.type'
|
|
153
|
+
});
|
|
154
|
+
});
|
|
155
|
+
});
|
|
156
|
+
});
|
|
@@ -0,0 +1,60 @@
|
|
|
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
|
+
isArray: ()=>isArray,
|
|
28
|
+
isBoolean: ()=>isBoolean,
|
|
29
|
+
isDate: ()=>isDate,
|
|
30
|
+
isNumber: ()=>isNumber,
|
|
31
|
+
isRecord: ()=>isRecord,
|
|
32
|
+
isString: ()=>isString,
|
|
33
|
+
isUint8Array: ()=>isUint8Array
|
|
34
|
+
});
|
|
35
|
+
const isString = (input)=>'string' == typeof input;
|
|
36
|
+
const isNumber = (input)=>'number' == typeof input && !Number.isNaN(input);
|
|
37
|
+
const isBoolean = (input)=>'boolean' == typeof input;
|
|
38
|
+
const isDate = (input)=>input instanceof Date && !Number.isNaN(input.getTime());
|
|
39
|
+
const isUint8Array = (input)=>input instanceof Uint8Array;
|
|
40
|
+
const isRecord = (input)=>null !== input && 'object' == typeof input && !Array.isArray(input);
|
|
41
|
+
const isArray = ()=>(input)=>Array.isArray(input);
|
|
42
|
+
exports.isArray = __webpack_exports__.isArray;
|
|
43
|
+
exports.isBoolean = __webpack_exports__.isBoolean;
|
|
44
|
+
exports.isDate = __webpack_exports__.isDate;
|
|
45
|
+
exports.isNumber = __webpack_exports__.isNumber;
|
|
46
|
+
exports.isRecord = __webpack_exports__.isRecord;
|
|
47
|
+
exports.isString = __webpack_exports__.isString;
|
|
48
|
+
exports.isUint8Array = __webpack_exports__.isUint8Array;
|
|
49
|
+
for(var __rspack_i in __webpack_exports__)if (-1 === [
|
|
50
|
+
"isArray",
|
|
51
|
+
"isBoolean",
|
|
52
|
+
"isDate",
|
|
53
|
+
"isNumber",
|
|
54
|
+
"isRecord",
|
|
55
|
+
"isString",
|
|
56
|
+
"isUint8Array"
|
|
57
|
+
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
|
|
58
|
+
Object.defineProperty(exports, '__esModule', {
|
|
59
|
+
value: true
|
|
60
|
+
});
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
export interface TypeGuard<T> {
|
|
2
|
+
(input: unknown): input is T;
|
|
3
|
+
}
|
|
4
|
+
/**
|
|
5
|
+
* isString is part of the public LIVON API.
|
|
6
|
+
*
|
|
7
|
+
* @remarks
|
|
8
|
+
* Parameter and return types are defined in the TypeScript signature.
|
|
9
|
+
*
|
|
10
|
+
* @see https://live-input-vector-output-node.github.io/livon-ts/docs/schema/type-guards
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* const result = isString(undefined as never);
|
|
14
|
+
*/
|
|
15
|
+
export declare const isString: (input: unknown) => input is string;
|
|
16
|
+
/**
|
|
17
|
+
* isNumber is part of the public LIVON API.
|
|
18
|
+
*
|
|
19
|
+
* @remarks
|
|
20
|
+
* Parameter and return types are defined in the TypeScript signature.
|
|
21
|
+
*
|
|
22
|
+
* @see https://live-input-vector-output-node.github.io/livon-ts/docs/schema/type-guards
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
* const result = isNumber(undefined as never);
|
|
26
|
+
*/
|
|
27
|
+
export declare const isNumber: (input: unknown) => input is number;
|
|
28
|
+
/**
|
|
29
|
+
* isBoolean is part of the public LIVON API.
|
|
30
|
+
*
|
|
31
|
+
* @remarks
|
|
32
|
+
* Parameter and return types are defined in the TypeScript signature.
|
|
33
|
+
*
|
|
34
|
+
* @see https://live-input-vector-output-node.github.io/livon-ts/docs/schema/type-guards
|
|
35
|
+
*
|
|
36
|
+
* @example
|
|
37
|
+
* const result = isBoolean(undefined as never);
|
|
38
|
+
*/
|
|
39
|
+
export declare const isBoolean: (input: unknown) => input is boolean;
|
|
40
|
+
/**
|
|
41
|
+
* isDate is part of the public LIVON API.
|
|
42
|
+
*
|
|
43
|
+
* @remarks
|
|
44
|
+
* Parameter and return types are defined in the TypeScript signature.
|
|
45
|
+
*
|
|
46
|
+
* @see https://live-input-vector-output-node.github.io/livon-ts/docs/schema/type-guards
|
|
47
|
+
*
|
|
48
|
+
* @example
|
|
49
|
+
* const result = isDate(undefined as never);
|
|
50
|
+
*/
|
|
51
|
+
export declare const isDate: (input: unknown) => input is Date;
|
|
52
|
+
/**
|
|
53
|
+
* isUint8Array is part of the public LIVON API.
|
|
54
|
+
*
|
|
55
|
+
* @remarks
|
|
56
|
+
* Parameter and return types are defined in the TypeScript signature.
|
|
57
|
+
*
|
|
58
|
+
* @see https://live-input-vector-output-node.github.io/livon-ts/docs/schema/type-guards
|
|
59
|
+
*
|
|
60
|
+
* @example
|
|
61
|
+
* const result = isUint8Array(undefined as never);
|
|
62
|
+
*/
|
|
63
|
+
export declare const isUint8Array: (input: unknown) => input is Uint8Array;
|
|
64
|
+
export interface UnknownRecord {
|
|
65
|
+
[key: string]: unknown;
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* isRecord is part of the public LIVON API.
|
|
69
|
+
*
|
|
70
|
+
* @remarks
|
|
71
|
+
* Parameter and return types are defined in the TypeScript signature.
|
|
72
|
+
*
|
|
73
|
+
* @see https://live-input-vector-output-node.github.io/livon-ts/docs/schema/type-guards
|
|
74
|
+
*
|
|
75
|
+
* @example
|
|
76
|
+
* const result = isRecord(undefined as never);
|
|
77
|
+
*/
|
|
78
|
+
export declare const isRecord: (input: unknown) => input is UnknownRecord;
|
|
79
|
+
export interface ArrayTypeGuard<T> {
|
|
80
|
+
(input: unknown): input is readonly T[];
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* isArray is part of the public LIVON API.
|
|
84
|
+
*
|
|
85
|
+
* @remarks
|
|
86
|
+
* Parameter and return types are defined in the TypeScript signature.
|
|
87
|
+
*
|
|
88
|
+
* @see https://live-input-vector-output-node.github.io/livon-ts/docs/schema/type-guards
|
|
89
|
+
*
|
|
90
|
+
* @example
|
|
91
|
+
* const result = isArray(undefined as never);
|
|
92
|
+
*/
|
|
93
|
+
export declare const isArray: <T>() => ArrayTypeGuard<T>;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
const isString = (input)=>'string' == typeof input;
|
|
2
|
+
const isNumber = (input)=>'number' == typeof input && !Number.isNaN(input);
|
|
3
|
+
const isBoolean = (input)=>'boolean' == typeof input;
|
|
4
|
+
const isDate = (input)=>input instanceof Date && !Number.isNaN(input.getTime());
|
|
5
|
+
const isUint8Array = (input)=>input instanceof Uint8Array;
|
|
6
|
+
const isRecord = (input)=>null !== input && 'object' == typeof input && !Array.isArray(input);
|
|
7
|
+
const isArray = ()=>(input)=>Array.isArray(input);
|
|
8
|
+
export { isArray, isBoolean, isDate, isNumber, isRecord, isString, isUint8Array };
|