@dxos/effect 0.8.1 → 0.8.2-main.2f9c567
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 +66 -64
- package/dist/lib/browser/index.mjs.map +3 -3
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/node/index.cjs +18 -16
- package/dist/lib/node/index.cjs.map +3 -3
- package/dist/lib/node/meta.json +1 -1
- package/dist/lib/node-esm/index.mjs +66 -64
- 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/package.json +6 -6
- package/src/ast.test.ts +40 -40
- 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,UAAW,OAAO,KAAG,KAAK,IAAI,QAEpD,CAAC;AAEF;;;;;;;;;;;;;;;;;;GAkBG;AACH,eAAO,MAAM,cAAc,SAAU,CAAC,MAAM,GAAG,MAAM,CAAC,EAAE,KAAG,QAa1D,CAAC;AAEF;;;GAGG;AACH,eAAO,MAAM,wBAAwB,eAAgB,MAAM,KAAG,QAK7D,CAAC;AAEF;;;GAGG;AACH,eAAO,MAAM,aAAa,SAAU,QAAQ,KAAG,MAAM,EAUpD,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,QAAQ,WAAY,GAAG,QAAQ,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,UACrB,uBAAuB,MAC9B,CAAC,SAAS,MAAM,CAAC,SAAS,CAAC,GAAG,QAAQ,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"}
|
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.2f9c567",
|
|
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.2f9c567",
|
|
28
|
+
"@dxos/node-std": "0.8.2-main.2f9c567",
|
|
29
|
+
"@dxos/util": "0.8.2-main.2f9c567"
|
|
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.2f9c567"
|
|
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,33 @@ 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
|
+
[SchemaAST.TitleAnnotationId]: '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({ [SchemaAST.TitleAnnotationId]: 'new title' }),
|
|
100
|
+
p2: TestSchema.annotations({ [SchemaAST.TitleAnnotationId]: 'new title' }).pipe(Schema.optional),
|
|
101
|
+
p3: Schema.optional(TestSchema.annotations({ [SchemaAST.TitleAnnotationId]: '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
113
|
const annotation = findAnnotation(
|
|
114
|
-
|
|
115
|
-
|
|
114
|
+
Schema.String.annotations({ [SchemaAST.TitleAnnotationId]: 'test' }).ast,
|
|
115
|
+
SchemaAST.TitleAnnotationId,
|
|
116
116
|
);
|
|
117
117
|
expect(annotation).to.eq('test');
|
|
118
118
|
|
|
119
|
-
const annotationIds = [
|
|
120
|
-
const schemas = [
|
|
119
|
+
const annotationIds = [SchemaAST.TitleAnnotationId, SchemaAST.DescriptionAnnotationId];
|
|
120
|
+
const schemas = [Schema.Object, Schema.String, Schema.Number, Schema.Boolean];
|
|
121
121
|
for (const schema of schemas) {
|
|
122
122
|
for (const annotationId of annotationIds) {
|
|
123
123
|
const annotation = findAnnotation(schema.ast, annotationId);
|
|
@@ -127,12 +127,12 @@ describe('AST', () => {
|
|
|
127
127
|
});
|
|
128
128
|
|
|
129
129
|
test('visit', ({ expect }) => {
|
|
130
|
-
const TestSchema =
|
|
131
|
-
name:
|
|
132
|
-
emails:
|
|
133
|
-
address:
|
|
134
|
-
|
|
135
|
-
zip:
|
|
130
|
+
const TestSchema = Schema.Struct({
|
|
131
|
+
name: Schema.NonEmptyString,
|
|
132
|
+
emails: Schema.optional(Schema.mutable(Schema.Array(Schema.String))),
|
|
133
|
+
address: Schema.optional(
|
|
134
|
+
Schema.Struct({
|
|
135
|
+
zip: Schema.String,
|
|
136
136
|
}),
|
|
137
137
|
),
|
|
138
138
|
});
|
|
@@ -142,12 +142,12 @@ describe('AST', () => {
|
|
|
142
142
|
});
|
|
143
143
|
|
|
144
144
|
test('discriminated unions', ({ expect }) => {
|
|
145
|
-
const TestUnionSchema =
|
|
146
|
-
|
|
147
|
-
|
|
145
|
+
const TestUnionSchema = Schema.Union(
|
|
146
|
+
Schema.Struct({ kind: Schema.Literal('a'), label: Schema.String }),
|
|
147
|
+
Schema.Struct({ kind: Schema.Literal('b'), count: Schema.Number, active: Schema.Boolean }),
|
|
148
148
|
);
|
|
149
149
|
|
|
150
|
-
type TestUnionType =
|
|
150
|
+
type TestUnionType = Schema.Schema.Type<typeof TestUnionSchema>;
|
|
151
151
|
|
|
152
152
|
{
|
|
153
153
|
expect(isOption(TestUnionSchema.ast)).to.be.false;
|
|
@@ -158,7 +158,7 @@ describe('AST', () => {
|
|
|
158
158
|
}
|
|
159
159
|
|
|
160
160
|
{
|
|
161
|
-
invariant(
|
|
161
|
+
invariant(SchemaAST.isUnion(TestUnionSchema.ast));
|
|
162
162
|
const [a, b] = TestUnionSchema.ast.types;
|
|
163
163
|
|
|
164
164
|
const obj1: TestUnionType = {
|
|
@@ -175,8 +175,8 @@ describe('AST', () => {
|
|
|
175
175
|
expect(getDiscriminatedType(TestUnionSchema.ast, obj1)?.toJSON()).to.deep.eq(a.toJSON());
|
|
176
176
|
expect(getDiscriminatedType(TestUnionSchema.ast, obj2)?.toJSON()).to.deep.eq(b.toJSON());
|
|
177
177
|
expect(getDiscriminatedType(TestUnionSchema.ast)?.toJSON()).to.deep.eq(
|
|
178
|
-
|
|
179
|
-
kind:
|
|
178
|
+
Schema.Struct({
|
|
179
|
+
kind: Schema.Literal('a', 'b'),
|
|
180
180
|
}).ast.toJSON(),
|
|
181
181
|
);
|
|
182
182
|
}
|