@dxos/effect 0.8.1-staging.9eaf14f → 0.8.2-main.10c050d
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/dist/lib/browser/index.mjs +70 -68
- package/dist/lib/browser/index.mjs.map +3 -3
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/node/index.cjs +22 -20
- package/dist/lib/node/index.cjs.map +3 -3
- package/dist/lib/node/meta.json +1 -1
- package/dist/lib/node-esm/index.mjs +70 -68
- package/dist/lib/node-esm/index.mjs.map +3 -3
- package/dist/lib/node-esm/meta.json +1 -1
- package/dist/types/src/ast.d.ts +19 -19
- package/dist/types/src/ast.d.ts.map +1 -1
- package/dist/types/src/jsonPath.d.ts +3 -3
- package/dist/types/src/jsonPath.d.ts.map +1 -1
- package/dist/types/src/url.d.ts +4 -4
- package/dist/types/src/url.d.ts.map +1 -1
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +6 -6
- package/src/ast.test.ts +39 -42
- package/src/ast.ts +84 -70
- package/src/jsonPath.ts +7 -5
- package/src/url.test.ts +8 -8
- package/src/url.ts +7 -7
package/dist/types/src/ast.d.ts
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import { SchemaAST
|
|
1
|
+
import { SchemaAST, Schema } from 'effect';
|
|
2
2
|
import { type JsonPath, type JsonProp } from './jsonPath';
|
|
3
3
|
export type SimpleType = 'object' | 'string' | 'number' | 'boolean' | 'enum' | 'literal';
|
|
4
4
|
/**
|
|
5
5
|
* Get the base type; e.g., traverse through refinements.
|
|
6
6
|
*/
|
|
7
|
-
export declare const getSimpleType: (node:
|
|
8
|
-
export declare const isSimpleType: (node:
|
|
7
|
+
export declare const getSimpleType: (node: SchemaAST.AST) => SimpleType | undefined;
|
|
8
|
+
export declare const isSimpleType: (node: SchemaAST.AST) => boolean;
|
|
9
9
|
export declare namespace SimpleType {
|
|
10
10
|
/**
|
|
11
11
|
* Returns the default empty value for a given SimpleType.
|
|
@@ -25,8 +25,8 @@ export declare enum VisitResult {
|
|
|
25
25
|
EXIT = 2
|
|
26
26
|
}
|
|
27
27
|
export type Path = (string | number)[];
|
|
28
|
-
export type TestFn = (node:
|
|
29
|
-
export type VisitorFn = (node:
|
|
28
|
+
export type TestFn = (node: SchemaAST.AST, path: Path, depth: number) => VisitResult | boolean | undefined;
|
|
29
|
+
export type VisitorFn = (node: SchemaAST.AST, path: Path, depth: number) => void;
|
|
30
30
|
/**
|
|
31
31
|
* Visit leaf nodes.
|
|
32
32
|
* Refs:
|
|
@@ -34,52 +34,52 @@ export type VisitorFn = (node: AST.AST, path: Path, depth: number) => void;
|
|
|
34
34
|
* - https://github.com/syntax-tree/unist-util-is?tab=readme-ov-file#test
|
|
35
35
|
*/
|
|
36
36
|
export declare const visit: {
|
|
37
|
-
(node:
|
|
38
|
-
(node:
|
|
37
|
+
(node: SchemaAST.AST, visitor: VisitorFn): void;
|
|
38
|
+
(node: SchemaAST.AST, test: TestFn, visitor: VisitorFn): void;
|
|
39
39
|
};
|
|
40
40
|
/**
|
|
41
41
|
* Recursively descend into AST to find first node that passes the test.
|
|
42
42
|
*/
|
|
43
|
-
export declare const findNode: (node:
|
|
43
|
+
export declare const findNode: (node: SchemaAST.AST, test: (node: SchemaAST.AST) => boolean) => SchemaAST.AST | undefined;
|
|
44
44
|
/**
|
|
45
45
|
* Get the AST node for the given property (dot-path).
|
|
46
46
|
*/
|
|
47
|
-
export declare const findProperty: (schema:
|
|
47
|
+
export declare const findProperty: (schema: Schema.Schema.AnyNoContext, path: JsonPath | JsonProp) => SchemaAST.AST | undefined;
|
|
48
48
|
/**
|
|
49
49
|
* Get annotation or return undefined.
|
|
50
50
|
* @param annotationId
|
|
51
51
|
* @param noDefault If true, then return undefined for effect library defined values.
|
|
52
52
|
*/
|
|
53
|
-
export declare const getAnnotation: <T>(annotationId: symbol, noDefault?: boolean) => (node:
|
|
53
|
+
export declare const getAnnotation: <T>(annotationId: symbol, noDefault?: boolean) => (node: SchemaAST.AST) => T | undefined;
|
|
54
54
|
/**
|
|
55
55
|
* Recursively descend into AST to find first matching annotations.
|
|
56
56
|
* Optionally skips default annotations for basic types (e.g., 'a string').
|
|
57
57
|
*/
|
|
58
|
-
export declare const findAnnotation: <T>(node:
|
|
58
|
+
export declare const findAnnotation: <T>(node: SchemaAST.AST, annotationId: symbol, noDefault?: boolean) => T | undefined;
|
|
59
59
|
/**
|
|
60
|
-
* Effect
|
|
60
|
+
* Effect Schema.optional creates a union type with undefined as the second type.
|
|
61
61
|
*/
|
|
62
|
-
export declare const isOption: (node:
|
|
62
|
+
export declare const isOption: (node: SchemaAST.AST) => boolean;
|
|
63
63
|
/**
|
|
64
64
|
* Determines if the node is a union of literal types.
|
|
65
65
|
*/
|
|
66
|
-
export declare const isLiteralUnion: (node:
|
|
66
|
+
export declare const isLiteralUnion: (node: SchemaAST.AST) => boolean;
|
|
67
67
|
/**
|
|
68
68
|
* Determines if the node is a discriminated union.
|
|
69
69
|
*/
|
|
70
|
-
export declare const isDiscriminatedUnion: (node:
|
|
70
|
+
export declare const isDiscriminatedUnion: (node: SchemaAST.AST) => boolean;
|
|
71
71
|
/**
|
|
72
72
|
* Get the discriminating properties for the given union type.
|
|
73
73
|
*/
|
|
74
|
-
export declare const getDiscriminatingProps: (node:
|
|
74
|
+
export declare const getDiscriminatingProps: (node: SchemaAST.AST) => string[] | undefined;
|
|
75
75
|
/**
|
|
76
76
|
* Get the discriminated type for the given value.
|
|
77
77
|
*/
|
|
78
|
-
export declare const getDiscriminatedType: (node:
|
|
78
|
+
export declare const getDiscriminatedType: (node: SchemaAST.AST, value?: Record<string, any>) => SchemaAST.AST | undefined;
|
|
79
79
|
/**
|
|
80
80
|
* Maps AST nodes.
|
|
81
|
-
* The user is responsible for recursively calling {@link mapAst} on the
|
|
81
|
+
* The user is responsible for recursively calling {@link mapAst} on the SchemaAST.
|
|
82
82
|
* NOTE: Will evaluate suspended ASTs.
|
|
83
83
|
*/
|
|
84
|
-
export declare const mapAst: (ast:
|
|
84
|
+
export declare const mapAst: (ast: SchemaAST.AST, f: (ast: SchemaAST.AST, key: keyof any | undefined) => SchemaAST.AST) => SchemaAST.AST;
|
|
85
85
|
//# sourceMappingURL=ast.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ast.d.ts","sourceRoot":"","sources":["../../../src/ast.ts"],"names":[],"mappings":"AAIA,OAAO,EAAgB,SAAS,
|
|
1
|
+
{"version":3,"file":"ast.d.ts","sourceRoot":"","sources":["../../../src/ast.ts"],"names":[],"mappings":"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"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Schema
|
|
1
|
+
import { Schema } from 'effect';
|
|
2
2
|
export type JsonProp = string & {
|
|
3
3
|
__JsonPath: true;
|
|
4
4
|
__JsonProp: true;
|
|
@@ -9,8 +9,8 @@ export type JsonPath = string & {
|
|
|
9
9
|
/**
|
|
10
10
|
* https://www.ietf.org/archive/id/draft-goessner-dispatch-jsonpath-00.html
|
|
11
11
|
*/
|
|
12
|
-
export declare const JsonPath:
|
|
13
|
-
export declare const JsonProp:
|
|
12
|
+
export declare const JsonPath: Schema.Schema<JsonPath>;
|
|
13
|
+
export declare const JsonProp: Schema.Schema<JsonProp>;
|
|
14
14
|
export declare const isJsonPath: (value: unknown) => value is JsonPath;
|
|
15
15
|
/**
|
|
16
16
|
* Creates a JsonPath from an array of path segments.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"jsonPath.d.ts","sourceRoot":"","sources":["../../../src/jsonPath.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,MAAM,
|
|
1
|
+
{"version":3,"file":"jsonPath.d.ts","sourceRoot":"","sources":["../../../src/jsonPath.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,MAAM,EAAU,MAAM,QAAQ,CAAC;AAKxC,MAAM,MAAM,QAAQ,GAAG,MAAM,GAAG;IAAE,UAAU,EAAE,IAAI,CAAC;IAAC,UAAU,EAAE,IAAI,CAAA;CAAE,CAAC;AACvE,MAAM,MAAM,QAAQ,GAAG,MAAM,GAAG;IAAE,UAAU,EAAE,IAAI,CAAA;CAAE,CAAC;AAKrD;;GAEG;AACH,eAAO,MAAM,QAAQ,EAGR,MAAM,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC;AACrC,eAAO,MAAM,QAAQ,EAAoE,MAAM,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC;AAEjH,eAAO,MAAM,UAAU,GAAI,OAAO,OAAO,KAAG,KAAK,IAAI,QAEpD,CAAC;AAEF;;;;;;;;;;;;;;;;;;GAkBG;AACH,eAAO,MAAM,cAAc,GAAI,MAAM,CAAC,MAAM,GAAG,MAAM,CAAC,EAAE,KAAG,QAa1D,CAAC;AAEF;;;GAGG;AACH,eAAO,MAAM,wBAAwB,GAAI,YAAY,MAAM,KAAG,QAK7D,CAAC;AAEF;;;GAGG;AACH,eAAO,MAAM,aAAa,GAAI,MAAM,QAAQ,KAAG,MAAM,EAUpD,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,QAAQ,GAAI,QAAQ,GAAG,EAAE,MAAM,QAAQ,KAAG,GAGtD,CAAC"}
|
package/dist/types/src/url.d.ts
CHANGED
|
@@ -1,16 +1,16 @@
|
|
|
1
|
-
import { SchemaAST
|
|
1
|
+
import { SchemaAST, type Schema, Option } from 'effect';
|
|
2
2
|
type ParamKeyAnnotationValue = {
|
|
3
3
|
key: string;
|
|
4
4
|
};
|
|
5
|
-
export declare const getParamKeyAnnotation: (annotated:
|
|
6
|
-
export declare const ParamKeyAnnotation: (value: ParamKeyAnnotationValue) => <S extends
|
|
5
|
+
export declare const getParamKeyAnnotation: (annotated: SchemaAST.Annotated) => Option.Option<ParamKeyAnnotationValue>;
|
|
6
|
+
export declare const ParamKeyAnnotation: (value: ParamKeyAnnotationValue) => <S extends Schema.Annotable.All>(self: S) => Schema.Annotable.Self<S>;
|
|
7
7
|
/**
|
|
8
8
|
* HTTP params parser.
|
|
9
9
|
* Supports custom key serialization.
|
|
10
10
|
*/
|
|
11
11
|
export declare class UrlParser<T extends Record<string, any>> {
|
|
12
12
|
private readonly _schema;
|
|
13
|
-
constructor(_schema:
|
|
13
|
+
constructor(_schema: Schema.Struct<T>);
|
|
14
14
|
/**
|
|
15
15
|
* Parse URL params.
|
|
16
16
|
*/
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"url.d.ts","sourceRoot":"","sources":["../../../src/url.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,SAAS,
|
|
1
|
+
{"version":3,"file":"url.d.ts","sourceRoot":"","sources":["../../../src/url.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,SAAS,EAAE,KAAK,MAAM,EAAE,MAAM,EAAQ,MAAM,QAAQ,CAAC;AAM9D,KAAK,uBAAuB,GAAG;IAAE,GAAG,EAAE,MAAM,CAAA;CAAE,CAAC;AAE/C,eAAO,MAAM,qBAAqB,EAAE,CAAC,SAAS,EAAE,SAAS,CAAC,SAAS,KAAK,MAAM,CAAC,MAAM,CAAC,uBAAuB,CACrC,CAAC;AAEzE,eAAO,MAAM,kBAAkB,GAC5B,OAAO,uBAAuB,MAC9B,CAAC,SAAS,MAAM,CAAC,SAAS,CAAC,GAAG,EAAE,MAAM,CAAC,KAAG,MAAM,CAAC,SAAS,CAAC,IAAI,CAAC,CAAC,CACb,CAAC;AAExD;;;GAGG;AACH,qBAAa,SAAS,CAAC,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC;IACtC,OAAO,CAAC,QAAQ,CAAC,OAAO;gBAAP,OAAO,EAAE,MAAM,CAAC,MAAM,CAAC,CAAC,CAAC;IAEtD;;OAEG;IACH,KAAK,CAAC,IAAI,EAAE,MAAM,GAAG,CAAC;IAsBtB;;OAEG;IACH,MAAM,CAAC,IAAI,EAAE,MAAM,EAAE,MAAM,EAAE,CAAC,GAAG,GAAG;CAoBrC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":"5.
|
|
1
|
+
{"version":"5.8.3"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dxos/effect",
|
|
3
|
-
"version": "0.8.
|
|
3
|
+
"version": "0.8.2-main.10c050d",
|
|
4
4
|
"description": "Effect utils.",
|
|
5
5
|
"homepage": "https://dxos.org",
|
|
6
6
|
"bugs": "https://github.com/dxos/dxos/issues",
|
|
@@ -24,13 +24,13 @@
|
|
|
24
24
|
],
|
|
25
25
|
"dependencies": {
|
|
26
26
|
"jsonpath-plus": "10.2.0",
|
|
27
|
-
"@dxos/invariant": "0.8.
|
|
28
|
-
"@dxos/node-std": "0.8.
|
|
29
|
-
"@dxos/util": "0.8.
|
|
27
|
+
"@dxos/invariant": "0.8.2-main.10c050d",
|
|
28
|
+
"@dxos/node-std": "0.8.2-main.10c050d",
|
|
29
|
+
"@dxos/util": "0.8.2-main.10c050d"
|
|
30
30
|
},
|
|
31
31
|
"devDependencies": {
|
|
32
|
-
"effect": "3.
|
|
33
|
-
"@dxos/log": "0.8.
|
|
32
|
+
"effect": "3.14.21",
|
|
33
|
+
"@dxos/log": "0.8.2-main.10c050d"
|
|
34
34
|
},
|
|
35
35
|
"peerDependencies": {
|
|
36
36
|
"effect": "^3.13.3"
|
package/src/ast.test.ts
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
// Copyright 2024 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import { SchemaAST
|
|
5
|
+
import { SchemaAST, Schema } from 'effect';
|
|
6
6
|
import { describe, test } from 'vitest';
|
|
7
7
|
|
|
8
8
|
import { invariant } from '@dxos/invariant';
|
|
@@ -21,8 +21,8 @@ import {
|
|
|
21
21
|
} from './ast';
|
|
22
22
|
import { type JsonPath, type JsonProp } from './jsonPath';
|
|
23
23
|
|
|
24
|
-
const ZipCode =
|
|
25
|
-
|
|
24
|
+
const ZipCode = Schema.String.pipe(
|
|
25
|
+
Schema.pattern(/^\d{5}$/, {
|
|
26
26
|
typeId: Symbol.for('@example/schema/ZipCode'),
|
|
27
27
|
identifier: 'ZipCode',
|
|
28
28
|
title: 'ZIP code',
|
|
@@ -30,24 +30,24 @@ const ZipCode = S.String.pipe(
|
|
|
30
30
|
}),
|
|
31
31
|
);
|
|
32
32
|
|
|
33
|
-
const LatLng =
|
|
34
|
-
lat:
|
|
35
|
-
lng:
|
|
33
|
+
const LatLng = Schema.Struct({
|
|
34
|
+
lat: Schema.Number,
|
|
35
|
+
lng: Schema.Number,
|
|
36
36
|
});
|
|
37
37
|
|
|
38
|
-
const Contact =
|
|
39
|
-
name:
|
|
40
|
-
address:
|
|
38
|
+
const Contact = Schema.Struct({
|
|
39
|
+
name: Schema.String,
|
|
40
|
+
address: Schema.Struct({
|
|
41
41
|
zip: ZipCode,
|
|
42
|
-
location:
|
|
42
|
+
location: Schema.optional(LatLng),
|
|
43
43
|
}),
|
|
44
44
|
});
|
|
45
45
|
|
|
46
|
-
const getTitle = getAnnotation(
|
|
46
|
+
const getTitle = getAnnotation(SchemaAST.TitleAnnotationId);
|
|
47
47
|
|
|
48
48
|
describe('AST', () => {
|
|
49
49
|
test('validation', ({ expect }) => {
|
|
50
|
-
const validate =
|
|
50
|
+
const validate = Schema.validateSync(ZipCode);
|
|
51
51
|
validate('11205');
|
|
52
52
|
|
|
53
53
|
expect(() => validate(null)).to.throw();
|
|
@@ -57,9 +57,9 @@ describe('AST', () => {
|
|
|
57
57
|
});
|
|
58
58
|
|
|
59
59
|
test('findNode', ({ expect }) => {
|
|
60
|
-
const TestSchema =
|
|
61
|
-
name:
|
|
62
|
-
}).pipe(
|
|
60
|
+
const TestSchema = Schema.Struct({
|
|
61
|
+
name: Schema.optional(Schema.String),
|
|
62
|
+
}).pipe(Schema.mutable);
|
|
63
63
|
|
|
64
64
|
const prop = findProperty(TestSchema, 'name' as JsonProp);
|
|
65
65
|
invariant(prop);
|
|
@@ -82,7 +82,7 @@ describe('AST', () => {
|
|
|
82
82
|
{
|
|
83
83
|
const prop = findProperty(Contact, 'address.location.lat' as JsonPath);
|
|
84
84
|
invariant(prop);
|
|
85
|
-
expect(
|
|
85
|
+
expect(SchemaAST.isNumberKeyword(prop)).to.be.true;
|
|
86
86
|
}
|
|
87
87
|
{
|
|
88
88
|
const prop = findProperty(Contact, 'address.city' as JsonPath);
|
|
@@ -91,33 +91,30 @@ describe('AST', () => {
|
|
|
91
91
|
});
|
|
92
92
|
|
|
93
93
|
test('findAnnotation', ({ expect }) => {
|
|
94
|
-
const TestSchema =
|
|
95
|
-
|
|
94
|
+
const TestSchema = Schema.NonEmptyString.pipe(Schema.pattern(/^\d{5}$/)).annotations({
|
|
95
|
+
title: 'original title',
|
|
96
96
|
});
|
|
97
97
|
|
|
98
|
-
const ContactSchema =
|
|
99
|
-
p1: TestSchema.annotations({
|
|
100
|
-
p2: TestSchema.annotations({
|
|
101
|
-
p3:
|
|
98
|
+
const ContactSchema = Schema.Struct({
|
|
99
|
+
p1: TestSchema.annotations({ title: 'new title' }),
|
|
100
|
+
p2: TestSchema.annotations({ title: 'new title' }).pipe(Schema.optional),
|
|
101
|
+
p3: Schema.optional(TestSchema.annotations({ title: 'new title' })),
|
|
102
102
|
});
|
|
103
103
|
|
|
104
104
|
for (const p of ['p1', 'p2', 'p3']) {
|
|
105
105
|
const prop = findProperty(ContactSchema, p as JsonPath);
|
|
106
106
|
invariant(prop);
|
|
107
|
-
const value = findAnnotation(prop,
|
|
107
|
+
const value = findAnnotation(prop, SchemaAST.TitleAnnotationId);
|
|
108
108
|
expect(value, `invalid title for ${p}`).to.eq('new title');
|
|
109
109
|
}
|
|
110
110
|
});
|
|
111
111
|
|
|
112
112
|
test('findAnnotation skips defaults', ({ expect }) => {
|
|
113
|
-
const annotation = findAnnotation(
|
|
114
|
-
S.String.annotations({ [AST.TitleAnnotationId]: 'test' }).ast,
|
|
115
|
-
AST.TitleAnnotationId,
|
|
116
|
-
);
|
|
113
|
+
const annotation = findAnnotation(Schema.String.annotations({ title: 'test' }).ast, SchemaAST.TitleAnnotationId);
|
|
117
114
|
expect(annotation).to.eq('test');
|
|
118
115
|
|
|
119
|
-
const annotationIds = [
|
|
120
|
-
const schemas = [
|
|
116
|
+
const annotationIds = [SchemaAST.TitleAnnotationId, SchemaAST.DescriptionAnnotationId];
|
|
117
|
+
const schemas = [Schema.Object, Schema.String, Schema.Number, Schema.Boolean];
|
|
121
118
|
for (const schema of schemas) {
|
|
122
119
|
for (const annotationId of annotationIds) {
|
|
123
120
|
const annotation = findAnnotation(schema.ast, annotationId);
|
|
@@ -127,12 +124,12 @@ describe('AST', () => {
|
|
|
127
124
|
});
|
|
128
125
|
|
|
129
126
|
test('visit', ({ expect }) => {
|
|
130
|
-
const TestSchema =
|
|
131
|
-
name:
|
|
132
|
-
emails:
|
|
133
|
-
address:
|
|
134
|
-
|
|
135
|
-
zip:
|
|
127
|
+
const TestSchema = Schema.Struct({
|
|
128
|
+
name: Schema.NonEmptyString,
|
|
129
|
+
emails: Schema.optional(Schema.mutable(Schema.Array(Schema.String))),
|
|
130
|
+
address: Schema.optional(
|
|
131
|
+
Schema.Struct({
|
|
132
|
+
zip: Schema.String,
|
|
136
133
|
}),
|
|
137
134
|
),
|
|
138
135
|
});
|
|
@@ -142,12 +139,12 @@ describe('AST', () => {
|
|
|
142
139
|
});
|
|
143
140
|
|
|
144
141
|
test('discriminated unions', ({ expect }) => {
|
|
145
|
-
const TestUnionSchema =
|
|
146
|
-
|
|
147
|
-
|
|
142
|
+
const TestUnionSchema = Schema.Union(
|
|
143
|
+
Schema.Struct({ kind: Schema.Literal('a'), label: Schema.String }),
|
|
144
|
+
Schema.Struct({ kind: Schema.Literal('b'), count: Schema.Number, active: Schema.Boolean }),
|
|
148
145
|
);
|
|
149
146
|
|
|
150
|
-
type TestUnionType =
|
|
147
|
+
type TestUnionType = Schema.Schema.Type<typeof TestUnionSchema>;
|
|
151
148
|
|
|
152
149
|
{
|
|
153
150
|
expect(isOption(TestUnionSchema.ast)).to.be.false;
|
|
@@ -158,7 +155,7 @@ describe('AST', () => {
|
|
|
158
155
|
}
|
|
159
156
|
|
|
160
157
|
{
|
|
161
|
-
invariant(
|
|
158
|
+
invariant(SchemaAST.isUnion(TestUnionSchema.ast));
|
|
162
159
|
const [a, b] = TestUnionSchema.ast.types;
|
|
163
160
|
|
|
164
161
|
const obj1: TestUnionType = {
|
|
@@ -175,8 +172,8 @@ describe('AST', () => {
|
|
|
175
172
|
expect(getDiscriminatedType(TestUnionSchema.ast, obj1)?.toJSON()).to.deep.eq(a.toJSON());
|
|
176
173
|
expect(getDiscriminatedType(TestUnionSchema.ast, obj2)?.toJSON()).to.deep.eq(b.toJSON());
|
|
177
174
|
expect(getDiscriminatedType(TestUnionSchema.ast)?.toJSON()).to.deep.eq(
|
|
178
|
-
|
|
179
|
-
kind:
|
|
175
|
+
Schema.Struct({
|
|
176
|
+
kind: Schema.Literal('a', 'b'),
|
|
180
177
|
}).ast.toJSON(),
|
|
181
178
|
);
|
|
182
179
|
}
|