@dxos/echo 0.8.4-main.a4bbb77 → 0.8.4-main.ae835ea
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/chunk-JUTFUT73.mjs +175 -0
- package/dist/lib/browser/chunk-JUTFUT73.mjs.map +7 -0
- package/dist/lib/browser/chunk-KEBVBR65.mjs +3727 -0
- package/dist/lib/browser/chunk-KEBVBR65.mjs.map +7 -0
- package/dist/lib/browser/{chunk-I4YEWYJA.mjs → chunk-WO76TDT6.mjs} +572 -640
- package/dist/lib/browser/chunk-WO76TDT6.mjs.map +7 -0
- package/dist/lib/browser/index.mjs +12 -7
- package/dist/lib/browser/internal/index.mjs +336 -0
- package/dist/lib/browser/internal/index.mjs.map +7 -0
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/browser/query/index.mjs +15 -0
- package/dist/lib/browser/query/index.mjs.map +7 -0
- package/dist/lib/browser/testing/index.mjs +245 -32
- package/dist/lib/browser/testing/index.mjs.map +4 -4
- package/dist/lib/node-esm/chunk-DWZYQ2UR.mjs +3727 -0
- package/dist/lib/node-esm/chunk-DWZYQ2UR.mjs.map +7 -0
- package/dist/lib/node-esm/chunk-KBD3A2I6.mjs +175 -0
- package/dist/lib/node-esm/chunk-KBD3A2I6.mjs.map +7 -0
- package/dist/lib/node-esm/{chunk-AWWFAF75.mjs → chunk-ZSSII3JW.mjs} +572 -640
- package/dist/lib/node-esm/chunk-ZSSII3JW.mjs.map +7 -0
- package/dist/lib/node-esm/index.mjs +12 -7
- package/dist/lib/node-esm/internal/index.mjs +336 -0
- package/dist/lib/node-esm/internal/index.mjs.map +7 -0
- package/dist/lib/node-esm/meta.json +1 -1
- package/dist/lib/node-esm/query/index.mjs +15 -0
- package/dist/lib/node-esm/query/index.mjs.map +7 -0
- package/dist/lib/node-esm/testing/index.mjs +245 -32
- package/dist/lib/node-esm/testing/index.mjs.map +4 -4
- package/dist/types/src/Obj.d.ts +16 -4
- package/dist/types/src/Obj.d.ts.map +1 -1
- package/dist/types/src/Ref.d.ts +1 -1
- package/dist/types/src/Ref.d.ts.map +1 -1
- package/dist/types/src/Relation.d.ts +2 -2
- package/dist/types/src/Relation.d.ts.map +1 -1
- package/dist/types/src/Type.d.ts +16 -17
- package/dist/types/src/Type.d.ts.map +1 -1
- package/dist/types/src/index.d.ts +2 -2
- package/dist/types/src/index.d.ts.map +1 -1
- package/dist/types/src/internal/ast/annotation-helper.d.ts +8 -0
- package/dist/types/src/internal/ast/annotation-helper.d.ts.map +1 -0
- package/dist/types/src/internal/ast/annotations.d.ts +131 -0
- package/dist/types/src/internal/ast/annotations.d.ts.map +1 -0
- package/dist/types/src/internal/ast/annotations.test.d.ts +2 -0
- package/dist/types/src/internal/ast/annotations.test.d.ts.map +1 -0
- package/dist/types/src/internal/ast/entity-kind.d.ts +10 -0
- package/dist/types/src/internal/ast/entity-kind.d.ts.map +1 -0
- package/dist/types/src/internal/ast/index.d.ts +5 -0
- package/dist/types/src/internal/ast/index.d.ts.map +1 -0
- package/dist/types/src/internal/ast/types.d.ts +6 -0
- package/dist/types/src/internal/ast/types.d.ts.map +1 -0
- package/dist/types/src/internal/formats/date.d.ts +63 -0
- package/dist/types/src/internal/formats/date.d.ts.map +1 -0
- package/dist/types/src/internal/formats/date.test.d.ts +2 -0
- package/dist/types/src/internal/formats/date.test.d.ts.map +1 -0
- package/dist/types/src/internal/formats/format.d.ts +30 -0
- package/dist/types/src/internal/formats/format.d.ts.map +1 -0
- package/dist/types/src/internal/formats/format.test.d.ts +2 -0
- package/dist/types/src/internal/formats/format.test.d.ts.map +1 -0
- package/dist/types/src/internal/formats/index.d.ts +8 -0
- package/dist/types/src/internal/formats/index.d.ts.map +1 -0
- package/dist/types/src/internal/formats/number.d.ts +31 -0
- package/dist/types/src/internal/formats/number.d.ts.map +1 -0
- package/dist/types/src/internal/formats/object.d.ts +35 -0
- package/dist/types/src/internal/formats/object.d.ts.map +1 -0
- package/dist/types/src/internal/formats/select.d.ts +11 -0
- package/dist/types/src/internal/formats/select.d.ts.map +1 -0
- package/dist/types/src/internal/formats/string.d.ts +38 -0
- package/dist/types/src/internal/formats/string.d.ts.map +1 -0
- package/dist/types/src/internal/formats/types.d.ts +68 -0
- package/dist/types/src/internal/formats/types.d.ts.map +1 -0
- package/dist/types/src/internal/index.d.ts +15 -0
- package/dist/types/src/internal/index.d.ts.map +1 -0
- package/dist/types/src/internal/json/annotations.d.ts +19 -0
- package/dist/types/src/internal/json/annotations.d.ts.map +1 -0
- package/dist/types/src/internal/json/effect-schema.test.d.ts +2 -0
- package/dist/types/src/internal/json/effect-schema.test.d.ts.map +1 -0
- package/dist/types/src/internal/json/index.d.ts +2 -0
- package/dist/types/src/internal/json/index.d.ts.map +1 -0
- package/dist/types/src/internal/json/json-schema.d.ts +28 -0
- package/dist/types/src/internal/json/json-schema.d.ts.map +1 -0
- package/dist/types/src/internal/json/json-schema.test.d.ts +2 -0
- package/dist/types/src/internal/json/json-schema.test.d.ts.map +1 -0
- package/dist/types/src/internal/json-schema/index.d.ts +3 -0
- package/dist/types/src/internal/json-schema/index.d.ts.map +1 -0
- package/dist/types/src/internal/json-schema/json-schema-normalize.d.ts +7 -0
- package/dist/types/src/internal/json-schema/json-schema-normalize.d.ts.map +1 -0
- package/dist/types/src/internal/json-schema/json-schema-type.d.ts +250 -0
- package/dist/types/src/internal/json-schema/json-schema-type.d.ts.map +1 -0
- package/dist/types/src/internal/object/accessors.d.ts +37 -0
- package/dist/types/src/internal/object/accessors.d.ts.map +1 -0
- package/dist/types/src/internal/object/common.d.ts +18 -0
- package/dist/types/src/internal/object/common.d.ts.map +1 -0
- package/dist/types/src/internal/object/create.d.ts +40 -0
- package/dist/types/src/internal/object/create.d.ts.map +1 -0
- package/dist/types/src/internal/object/create.test.d.ts +2 -0
- package/dist/types/src/internal/object/create.test.d.ts.map +1 -0
- package/dist/types/src/internal/object/deleted.d.ts +6 -0
- package/dist/types/src/internal/object/deleted.d.ts.map +1 -0
- package/dist/types/src/internal/object/entity.d.ts +33 -0
- package/dist/types/src/internal/object/entity.d.ts.map +1 -0
- package/dist/types/src/internal/object/expando.d.ts +14 -0
- package/dist/types/src/internal/object/expando.d.ts.map +1 -0
- package/dist/types/src/internal/object/ids.d.ts +6 -0
- package/dist/types/src/internal/object/ids.d.ts.map +1 -0
- package/dist/types/src/internal/object/index.d.ts +16 -0
- package/dist/types/src/internal/object/index.d.ts.map +1 -0
- package/dist/types/src/internal/object/inspect.d.ts +2 -0
- package/dist/types/src/internal/object/inspect.d.ts.map +1 -0
- package/dist/types/src/internal/object/json-serializer.d.ts +32 -0
- package/dist/types/src/internal/object/json-serializer.d.ts.map +1 -0
- package/dist/types/src/internal/object/json-serializer.test.d.ts +2 -0
- package/dist/types/src/internal/object/json-serializer.test.d.ts.map +1 -0
- package/dist/types/src/internal/object/meta.d.ts +31 -0
- package/dist/types/src/internal/object/meta.d.ts.map +1 -0
- package/dist/types/src/internal/object/model.d.ts +117 -0
- package/dist/types/src/internal/object/model.d.ts.map +1 -0
- package/dist/types/src/internal/object/relation.d.ts +17 -0
- package/dist/types/src/internal/object/relation.d.ts.map +1 -0
- package/dist/types/src/internal/object/schema-validator.d.ts +15 -0
- package/dist/types/src/internal/object/schema-validator.d.ts.map +1 -0
- package/dist/types/src/internal/object/schema-validator.test.d.ts +2 -0
- package/dist/types/src/internal/object/schema-validator.test.d.ts.map +1 -0
- package/dist/types/src/internal/object/typed-object.d.ts +31 -0
- package/dist/types/src/internal/object/typed-object.d.ts.map +1 -0
- package/dist/types/src/internal/object/typed-object.test.d.ts +2 -0
- package/dist/types/src/internal/object/typed-object.test.d.ts.map +1 -0
- package/dist/types/src/internal/object/typename.d.ts +15 -0
- package/dist/types/src/internal/object/typename.d.ts.map +1 -0
- package/dist/types/src/internal/object/version.d.ts +14 -0
- package/dist/types/src/internal/object/version.d.ts.map +1 -0
- package/dist/types/src/internal/projection/compose.d.ts +6 -0
- package/dist/types/src/internal/projection/compose.d.ts.map +1 -0
- package/dist/types/src/internal/projection/compose.test.d.ts +2 -0
- package/dist/types/src/internal/projection/compose.test.d.ts.map +1 -0
- package/dist/types/src/internal/projection/index.d.ts +2 -0
- package/dist/types/src/internal/projection/index.d.ts.map +1 -0
- package/dist/types/src/internal/proxy/handler.test.d.ts +2 -0
- package/dist/types/src/internal/proxy/handler.test.d.ts.map +1 -0
- package/dist/types/src/internal/proxy/reactive-object.d.ts +15 -0
- package/dist/types/src/internal/proxy/reactive-object.d.ts.map +1 -0
- package/dist/types/src/internal/proxy/schema.test.d.ts +2 -0
- package/dist/types/src/internal/proxy/schema.test.d.ts.map +1 -0
- package/dist/types/src/internal/proxy/typed-handler.d.ts +44 -0
- package/dist/types/src/internal/proxy/typed-handler.d.ts.map +1 -0
- package/dist/types/src/internal/proxy/typed-handler.test.d.ts +2 -0
- package/dist/types/src/internal/proxy/typed-handler.test.d.ts.map +1 -0
- package/dist/types/src/internal/proxy/typed-object.test.d.ts +2 -0
- package/dist/types/src/internal/proxy/typed-object.test.d.ts.map +1 -0
- package/dist/types/src/internal/query/index.d.ts +2 -0
- package/dist/types/src/internal/query/index.d.ts.map +1 -0
- package/dist/types/src/internal/query/query.d.ts +17 -0
- package/dist/types/src/internal/query/query.d.ts.map +1 -0
- package/dist/types/src/internal/ref/index.d.ts +3 -0
- package/dist/types/src/internal/ref/index.d.ts.map +1 -0
- package/dist/types/src/internal/ref/ref-array.d.ts +21 -0
- package/dist/types/src/internal/ref/ref-array.d.ts.map +1 -0
- package/dist/types/src/internal/ref/ref.d.ts +206 -0
- package/dist/types/src/internal/ref/ref.d.ts.map +1 -0
- package/dist/types/src/internal/ref/ref.test.d.ts +2 -0
- package/dist/types/src/internal/ref/ref.test.d.ts.map +1 -0
- package/dist/types/src/internal/schema/echo-schema.d.ts +168 -0
- package/dist/types/src/internal/schema/echo-schema.d.ts.map +1 -0
- package/dist/types/src/internal/schema/index.d.ts +7 -0
- package/dist/types/src/internal/schema/index.d.ts.map +1 -0
- package/dist/types/src/internal/schema/manipulation.d.ts +10 -0
- package/dist/types/src/internal/schema/manipulation.d.ts.map +1 -0
- package/dist/types/src/internal/schema/runtime-schema-registry.d.ts +18 -0
- package/dist/types/src/internal/schema/runtime-schema-registry.d.ts.map +1 -0
- package/dist/types/src/internal/schema/snapshot.d.ts +6 -0
- package/dist/types/src/internal/schema/snapshot.d.ts.map +1 -0
- package/dist/types/src/internal/schema/stored-schema.d.ts +13 -0
- package/dist/types/src/internal/schema/stored-schema.d.ts.map +1 -0
- package/dist/types/src/internal/testing/index.d.ts +3 -0
- package/dist/types/src/internal/testing/index.d.ts.map +1 -0
- package/dist/types/src/internal/testing/types.d.ts +455 -0
- package/dist/types/src/internal/testing/types.d.ts.map +1 -0
- package/dist/types/src/internal/testing/utils.d.ts +10 -0
- package/dist/types/src/internal/testing/utils.d.ts.map +1 -0
- package/dist/types/src/internal/types/index.d.ts +3 -0
- package/dist/types/src/internal/types/index.d.ts.map +1 -0
- package/dist/types/src/internal/types/types.d.ts +79 -0
- package/dist/types/src/internal/types/types.d.ts.map +1 -0
- package/dist/types/src/internal/types/types.test.d.ts +2 -0
- package/dist/types/src/internal/types/types.test.d.ts.map +1 -0
- package/dist/types/src/internal/types/util.d.ts +5 -0
- package/dist/types/src/internal/types/util.d.ts.map +1 -0
- package/dist/types/src/query/index.d.ts +2 -1
- package/dist/types/src/query/index.d.ts.map +1 -1
- package/dist/types/src/query/{dsl.d.ts → query.d.ts} +24 -21
- package/dist/types/src/query/query.d.ts.map +1 -0
- package/dist/types/src/query/query.test.d.ts +2 -0
- package/dist/types/src/query/query.test.d.ts.map +1 -0
- package/dist/types/src/query/tag.d.ts +17 -0
- package/dist/types/src/query/tag.d.ts.map +1 -0
- package/dist/types/src/testing/echo-schema.d.ts +7 -0
- package/dist/types/src/testing/echo-schema.d.ts.map +1 -0
- package/dist/types/src/testing/index.d.ts +2 -0
- package/dist/types/src/testing/index.d.ts.map +1 -1
- package/dist/types/src/testing/types.d.ts +18 -18
- package/dist/types/src/testing/types.d.ts.map +1 -1
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +27 -16
- package/src/Obj.ts +40 -10
- package/src/Ref.ts +1 -2
- package/src/Relation.ts +4 -3
- package/src/Type.ts +28 -29
- package/src/index.ts +2 -2
- package/src/internal/ast/annotation-helper.ts +22 -0
- package/src/internal/ast/annotations.test.ts +98 -0
- package/src/internal/ast/annotations.ts +226 -0
- package/src/internal/ast/entity-kind.ts +15 -0
- package/src/internal/ast/index.ts +8 -0
- package/src/internal/ast/types.ts +17 -0
- package/src/internal/formats/date.test.ts +56 -0
- package/src/internal/formats/date.ts +217 -0
- package/src/internal/formats/format.test.ts +77 -0
- package/src/internal/formats/format.ts +52 -0
- package/src/internal/formats/index.ts +12 -0
- package/src/internal/formats/number.ts +89 -0
- package/src/internal/formats/object.ts +80 -0
- package/src/internal/formats/select.ts +16 -0
- package/src/internal/formats/string.ts +76 -0
- package/src/internal/formats/types.ts +175 -0
- package/src/internal/index.ts +22 -0
- package/src/internal/json/annotations.ts +50 -0
- package/src/internal/json/effect-schema.test.ts +143 -0
- package/src/internal/json/index.ts +5 -0
- package/src/internal/json/json-schema.test.ts +857 -0
- package/src/internal/json/json-schema.ts +519 -0
- package/src/internal/json-schema/index.ts +6 -0
- package/src/internal/json-schema/json-schema-normalize.ts +109 -0
- package/src/internal/json-schema/json-schema-type.ts +403 -0
- package/src/internal/object/accessors.ts +153 -0
- package/src/internal/object/common.ts +76 -0
- package/src/internal/object/create.test.ts +118 -0
- package/src/internal/object/create.ts +96 -0
- package/src/internal/object/deleted.ts +19 -0
- package/src/internal/object/entity.ts +248 -0
- package/src/internal/object/expando.ts +21 -0
- package/src/internal/object/ids.ts +12 -0
- package/src/internal/object/index.ts +19 -0
- package/src/internal/object/inspect.ts +48 -0
- package/src/internal/object/json-serializer.test.ts +99 -0
- package/src/internal/object/json-serializer.ts +225 -0
- package/src/internal/object/meta.ts +61 -0
- package/src/internal/object/model.ts +170 -0
- package/src/internal/object/relation.ts +24 -0
- package/src/internal/object/schema-validator.test.ts +186 -0
- package/src/internal/object/schema-validator.ts +241 -0
- package/src/internal/object/typed-object.test.ts +34 -0
- package/src/internal/object/typed-object.ts +88 -0
- package/src/internal/object/typename.ts +61 -0
- package/src/internal/object/version.ts +22 -0
- package/src/internal/projection/compose.test.ts +43 -0
- package/src/internal/projection/compose.ts +36 -0
- package/src/internal/projection/index.ts +5 -0
- package/src/internal/proxy/handler.test.ts +163 -0
- package/src/internal/proxy/reactive-object.ts +108 -0
- package/src/internal/proxy/schema.test.ts +136 -0
- package/src/internal/proxy/typed-handler.test.ts +102 -0
- package/src/internal/proxy/typed-handler.ts +228 -0
- package/src/internal/proxy/typed-object.test.ts +100 -0
- package/src/internal/query/index.ts +5 -0
- package/src/internal/query/query.ts +23 -0
- package/src/internal/ref/index.ts +6 -0
- package/src/internal/ref/ref-array.ts +39 -0
- package/src/internal/ref/ref.test.ts +100 -0
- package/src/internal/ref/ref.ts +521 -0
- package/src/internal/schema/echo-schema.ts +383 -0
- package/src/internal/schema/index.ts +10 -0
- package/src/internal/schema/manipulation.ts +92 -0
- package/src/internal/schema/runtime-schema-registry.ts +78 -0
- package/src/internal/schema/snapshot.ts +25 -0
- package/src/internal/schema/stored-schema.ts +26 -0
- package/src/internal/testing/index.ts +6 -0
- package/src/internal/testing/types.ts +211 -0
- package/src/internal/testing/utils.ts +54 -0
- package/src/internal/types/index.ts +6 -0
- package/src/internal/types/types.test.ts +48 -0
- package/src/internal/types/types.ts +176 -0
- package/src/internal/types/util.ts +9 -0
- package/src/query/index.ts +2 -1
- package/src/query/query.test.ts +401 -0
- package/src/query/{dsl.ts → query.ts} +68 -38
- package/src/query/tag.ts +37 -0
- package/src/test/api.test.ts +2 -2
- package/src/testing/echo-schema.ts +39 -0
- package/src/testing/index.ts +2 -0
- package/src/testing/types.ts +1 -1
- package/dist/lib/browser/chunk-I4YEWYJA.mjs.map +0 -7
- package/dist/lib/node-esm/chunk-AWWFAF75.mjs.map +0 -7
- package/dist/types/src/experimental/database.d.ts +0 -8
- package/dist/types/src/experimental/database.d.ts.map +0 -1
- package/dist/types/src/experimental/index.d.ts +0 -1
- package/dist/types/src/experimental/index.d.ts.map +0 -1
- package/dist/types/src/experimental/queue.d.ts +0 -8
- package/dist/types/src/experimental/queue.d.ts.map +0 -1
- package/dist/types/src/experimental/space.d.ts +0 -8
- package/dist/types/src/experimental/space.d.ts.map +0 -1
- package/dist/types/src/query/dsl.d.ts.map +0 -1
- package/dist/types/src/query/dsl.test.d.ts +0 -2
- package/dist/types/src/query/dsl.test.d.ts.map +0 -1
- package/src/experimental/database.ts +0 -11
- package/src/experimental/index.ts +0 -7
- package/src/experimental/queue.ts +0 -11
- package/src/experimental/space.ts +0 -11
- package/src/query/dsl.test.ts +0 -362
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2024 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import * as Schema from 'effect/Schema';
|
|
6
|
+
|
|
7
|
+
import { FormatAnnotation, FormatEnum } from './types';
|
|
8
|
+
|
|
9
|
+
const encodeMultipleOf = (divisor: number) => 1 / Math.pow(10, divisor);
|
|
10
|
+
|
|
11
|
+
const encodeMultiple =
|
|
12
|
+
<A extends number>(divisor?: number) =>
|
|
13
|
+
<I, R>(self: Schema.Schema<A, I, R>) =>
|
|
14
|
+
divisor === undefined || divisor === 0 ? self : self.pipe(Schema.multipleOf(encodeMultipleOf(divisor)));
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Convert number of digits to multipleOf annotation.
|
|
18
|
+
*/
|
|
19
|
+
export const DecimalPrecision = Schema.transform(Schema.Number, Schema.Number, {
|
|
20
|
+
strict: true,
|
|
21
|
+
encode: (value) => encodeMultipleOf(value),
|
|
22
|
+
decode: (value) => Math.log10(1 / value),
|
|
23
|
+
}).annotations({
|
|
24
|
+
title: 'Number of digits',
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
export const CurrencyAnnotationId = Symbol.for('@dxos/schema/annotation/Currency');
|
|
28
|
+
|
|
29
|
+
export type CurrencyAnnotation = {
|
|
30
|
+
decimals?: number;
|
|
31
|
+
code?: string;
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* ISO 4217 currency code.
|
|
36
|
+
*/
|
|
37
|
+
export const Currency = ({ decimals, code }: CurrencyAnnotation = { decimals: 2 }) =>
|
|
38
|
+
Schema.Number.pipe(
|
|
39
|
+
encodeMultiple(decimals),
|
|
40
|
+
FormatAnnotation.set(FormatEnum.Currency),
|
|
41
|
+
Schema.annotations({
|
|
42
|
+
title: 'Currency',
|
|
43
|
+
description: 'Currency value',
|
|
44
|
+
...(code ? { [CurrencyAnnotationId]: code.toUpperCase() } : {}),
|
|
45
|
+
}),
|
|
46
|
+
);
|
|
47
|
+
|
|
48
|
+
export type PercentAnnotation = {
|
|
49
|
+
decimals?: number;
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Integer.
|
|
54
|
+
*/
|
|
55
|
+
export const Integer = () =>
|
|
56
|
+
Schema.Number.pipe(
|
|
57
|
+
Schema.int(),
|
|
58
|
+
FormatAnnotation.set(FormatEnum.Integer),
|
|
59
|
+
Schema.annotations({
|
|
60
|
+
title: 'Integer',
|
|
61
|
+
description: 'Integer value',
|
|
62
|
+
}),
|
|
63
|
+
);
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Percent.
|
|
67
|
+
*/
|
|
68
|
+
// TODO(burdon): Define min/max (e.g., 0, 1).
|
|
69
|
+
export const Percent = ({ decimals }: PercentAnnotation = { decimals: 2 }) =>
|
|
70
|
+
Schema.Number.pipe(
|
|
71
|
+
encodeMultiple(decimals),
|
|
72
|
+
FormatAnnotation.set(FormatEnum.Percent),
|
|
73
|
+
Schema.annotations({
|
|
74
|
+
title: 'Percent',
|
|
75
|
+
description: 'Percentage value',
|
|
76
|
+
}),
|
|
77
|
+
);
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Unix timestamp.
|
|
81
|
+
* https://en.wikipedia.org/wiki/Unix_time
|
|
82
|
+
*/
|
|
83
|
+
export const Timestamp = Schema.Number.pipe(
|
|
84
|
+
FormatAnnotation.set(FormatEnum.Timestamp),
|
|
85
|
+
Schema.annotations({
|
|
86
|
+
title: 'Timestamp',
|
|
87
|
+
description: 'Unix timestamp',
|
|
88
|
+
}),
|
|
89
|
+
);
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import * as Schema from 'effect/Schema';
|
|
6
|
+
|
|
7
|
+
import { clamp } from '@dxos/util';
|
|
8
|
+
|
|
9
|
+
import { FormatAnnotation, FormatEnum } from './types';
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* GeoJSON Format
|
|
13
|
+
* https://datatracker.ietf.org/doc/html/rfc7946
|
|
14
|
+
* https://datatracker.ietf.org/doc/html/rfc7946#section-3.1.1
|
|
15
|
+
* https://en.wikipedia.org/wiki/Geographic_coordinate_system
|
|
16
|
+
* https://geojson.org
|
|
17
|
+
* {
|
|
18
|
+
* "type": "Point",
|
|
19
|
+
* "coordinates": [0, 51.47] // [longitude, latitude]
|
|
20
|
+
* }
|
|
21
|
+
* Note: optional third element for altitude.
|
|
22
|
+
*/
|
|
23
|
+
export const GeoPoint = Schema.Tuple(
|
|
24
|
+
Schema.Number.pipe(Schema.annotations({ title: 'Longitude' }), Schema.clamp(-180, 180), Schema.multipleOf(0.00001)),
|
|
25
|
+
Schema.Number.pipe(Schema.annotations({ title: 'Latitude' }), Schema.clamp(-90, 90), Schema.multipleOf(0.00001)),
|
|
26
|
+
Schema.optionalElement(Schema.Number).annotations({
|
|
27
|
+
title: 'Height ASL (m)',
|
|
28
|
+
}),
|
|
29
|
+
).pipe(
|
|
30
|
+
FormatAnnotation.set(FormatEnum.GeoPoint),
|
|
31
|
+
Schema.annotations({
|
|
32
|
+
title: 'GeoPoint',
|
|
33
|
+
description: 'GeoJSON Position',
|
|
34
|
+
}),
|
|
35
|
+
);
|
|
36
|
+
|
|
37
|
+
export type GeoPoint = Schema.Schema.Type<typeof GeoPoint>;
|
|
38
|
+
|
|
39
|
+
export type GeoLocation = {
|
|
40
|
+
longitude: number;
|
|
41
|
+
latitude: number;
|
|
42
|
+
height?: number;
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Geolocation utilities for working with GeoPoint format.
|
|
47
|
+
*/
|
|
48
|
+
export namespace GeoLocation {
|
|
49
|
+
/**
|
|
50
|
+
* Convert latitude and longitude to GeoPoint (GeoJSON format [longitude, latitude, height?]).
|
|
51
|
+
* Clamps values to valid ranges: latitude [-90, 90], longitude [-180, 180].
|
|
52
|
+
*/
|
|
53
|
+
export const toGeoPoint = ({ longitude, latitude, height }: GeoLocation): GeoPoint => {
|
|
54
|
+
// TODO(ZaymonFC): Use schema validation instead of doing this manually.
|
|
55
|
+
const clampedLongitude = clamp(longitude, -180, 180);
|
|
56
|
+
const clampedLatitude = clamp(latitude, -90, 90);
|
|
57
|
+
return height !== undefined ? [clampedLongitude, clampedLatitude, height] : [clampedLongitude, clampedLatitude];
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Extract latitude and longitude from GeoPoint (GeoJSON format [longitude, latitude, height?]).
|
|
62
|
+
*/
|
|
63
|
+
export const fromGeoPoint = (geoPoint: GeoPoint | undefined): GeoLocation => {
|
|
64
|
+
if (!geoPoint) {
|
|
65
|
+
return { longitude: 0, latitude: 0 };
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const result: GeoLocation = {
|
|
69
|
+
longitude: geoPoint[0],
|
|
70
|
+
latitude: geoPoint[1],
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
// Add height if defined.
|
|
74
|
+
if (geoPoint[2] !== undefined) {
|
|
75
|
+
result.height = geoPoint[2];
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
return result;
|
|
79
|
+
};
|
|
80
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import * as Schema from 'effect/Schema';
|
|
6
|
+
|
|
7
|
+
/** Schema for a single select option. Used to define choices in a {single|multi}-select field. */
|
|
8
|
+
export const SelectOptionSchema = Schema.Struct({
|
|
9
|
+
/** Stable identifier for the option. */
|
|
10
|
+
id: Schema.NonEmptyString,
|
|
11
|
+
title: Schema.String,
|
|
12
|
+
/** Color palette used for visual styling. */
|
|
13
|
+
color: Schema.String,
|
|
14
|
+
}).pipe(Schema.mutable);
|
|
15
|
+
|
|
16
|
+
export type SelectOption = Schema.Schema.Type<typeof SelectOptionSchema>;
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2024 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import * as Schema from 'effect/Schema';
|
|
6
|
+
import * as SchemaAST from 'effect/SchemaAST';
|
|
7
|
+
|
|
8
|
+
import { FormatAnnotation, FormatEnum } from './types';
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Email address (RFC 5321)
|
|
12
|
+
* https://datatracker.ietf.org/doc/html/rfc5321#section-4.1.2
|
|
13
|
+
*/
|
|
14
|
+
export const Email = Schema.String.pipe(
|
|
15
|
+
Schema.pattern(/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/),
|
|
16
|
+
FormatAnnotation.set(FormatEnum.Email),
|
|
17
|
+
Schema.annotations({
|
|
18
|
+
title: 'Email',
|
|
19
|
+
description: 'Email address',
|
|
20
|
+
}),
|
|
21
|
+
);
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
*
|
|
25
|
+
*/
|
|
26
|
+
// TODO(burdon): Implement.
|
|
27
|
+
export const Formula = Schema.String.pipe(FormatAnnotation.set(FormatEnum.Formula));
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
*
|
|
31
|
+
*/
|
|
32
|
+
// TODO(burdon): Implement.
|
|
33
|
+
export const Hostname = Schema.String.pipe(FormatAnnotation.set(FormatEnum.Hostname));
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
*
|
|
37
|
+
*/
|
|
38
|
+
// TODO(burdon): Implement.
|
|
39
|
+
export const JSON = Schema.String.pipe(FormatAnnotation.set(FormatEnum.JSON));
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
*
|
|
43
|
+
*/
|
|
44
|
+
// TODO(burdon): Implement.
|
|
45
|
+
export const Markdown = Schema.String.pipe(FormatAnnotation.set(FormatEnum.Markdown));
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Regex
|
|
49
|
+
* https://json-schema.org/understanding-json-schema/reference/regular_expressions
|
|
50
|
+
* https://ecma-international.org/publications-and-standards/standards/ecma-262
|
|
51
|
+
*/
|
|
52
|
+
// TODO(burdon): Implement.
|
|
53
|
+
export const Regex = Schema.String.pipe(FormatAnnotation.set(FormatEnum.Regex));
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* https://datatracker.ietf.org/doc/html/rfc3986#section-1.1.3
|
|
57
|
+
*/
|
|
58
|
+
export const URL = Schema.String.pipe(
|
|
59
|
+
Schema.pattern(/^(\w+?:\/\/)?([\da-z.-]+)\.([a-z.]{2,6})([/\w .-]*)*\/?$/i),
|
|
60
|
+
FormatAnnotation.set(FormatEnum.URL),
|
|
61
|
+
Schema.annotations({
|
|
62
|
+
title: 'URL',
|
|
63
|
+
description: 'URL',
|
|
64
|
+
}),
|
|
65
|
+
);
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* UUID (RFC 4122)
|
|
69
|
+
* https://datatracker.ietf.org/doc/html/rfc4122
|
|
70
|
+
*/
|
|
71
|
+
export const UUID = Schema.UUID.pipe(
|
|
72
|
+
FormatAnnotation.set(FormatEnum.UUID),
|
|
73
|
+
Schema.annotations({
|
|
74
|
+
[SchemaAST.ExamplesAnnotationId]: ['3e4666bf-d5e5-4aa7-b8ce-cefe41c7568a'],
|
|
75
|
+
}),
|
|
76
|
+
);
|
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2024 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import * as Function from 'effect/Function';
|
|
6
|
+
import type * as JSONSchema from 'effect/JSONSchema';
|
|
7
|
+
import * as Option from 'effect/Option';
|
|
8
|
+
import * as SchemaAST from 'effect/SchemaAST';
|
|
9
|
+
|
|
10
|
+
import { createAnnotationHelper } from '../ast';
|
|
11
|
+
import { type JsonSchemaType } from '../json-schema';
|
|
12
|
+
|
|
13
|
+
// TODO(burdon): Rename PropertyType.
|
|
14
|
+
export type ScalarType =
|
|
15
|
+
| JSONSchema.JsonSchema7Object
|
|
16
|
+
| JSONSchema.JsonSchema7String
|
|
17
|
+
| JSONSchema.JsonSchema7Number
|
|
18
|
+
| JSONSchema.JsonSchema7Boolean
|
|
19
|
+
| JSONSchema.JsonSchema7Ref;
|
|
20
|
+
|
|
21
|
+
// TODO(burdon): Rename ValueType and change to disciminated union.
|
|
22
|
+
// export type ValueType = 'array' | 'object' | 'string' | 'number' | 'boolean' | 'ref';
|
|
23
|
+
export enum TypeEnum {
|
|
24
|
+
Array = 'array', // TODO(burdon): Remove?
|
|
25
|
+
Object = 'object',
|
|
26
|
+
String = 'string',
|
|
27
|
+
Number = 'number',
|
|
28
|
+
Boolean = 'boolean',
|
|
29
|
+
Ref = 'ref',
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
// TODO(burdon): Ref?
|
|
33
|
+
export const getTypeEnum = (property: JsonSchemaType): TypeEnum | undefined => {
|
|
34
|
+
switch (property.type) {
|
|
35
|
+
case 'array':
|
|
36
|
+
return TypeEnum.Array;
|
|
37
|
+
case 'object':
|
|
38
|
+
return TypeEnum.Object;
|
|
39
|
+
case 'string':
|
|
40
|
+
return TypeEnum.String;
|
|
41
|
+
case 'number':
|
|
42
|
+
return TypeEnum.Number;
|
|
43
|
+
case 'boolean':
|
|
44
|
+
return TypeEnum.Boolean;
|
|
45
|
+
default:
|
|
46
|
+
return undefined;
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* https://json-schema.org/understanding-json-schema/reference/schema
|
|
52
|
+
* https://json-schema.org/understanding-json-schema/reference/string#built-in-formats
|
|
53
|
+
*/
|
|
54
|
+
export const FormatAnnotationId = Symbol.for('@dxos/schema/annotation/Format');
|
|
55
|
+
|
|
56
|
+
export const FormatAnnotation = createAnnotationHelper<FormatEnum>(FormatAnnotationId);
|
|
57
|
+
|
|
58
|
+
export const getFormatAnnotation = (node: SchemaAST.AST): FormatEnum | undefined =>
|
|
59
|
+
Function.pipe(SchemaAST.getAnnotation<FormatEnum>(FormatAnnotationId)(node), Option.getOrUndefined);
|
|
60
|
+
|
|
61
|
+
// TODO(burdon): Rename to FormatType and change to discriminated union.
|
|
62
|
+
export enum FormatEnum {
|
|
63
|
+
None = 'none',
|
|
64
|
+
String = 'string',
|
|
65
|
+
Number = 'number',
|
|
66
|
+
Boolean = 'boolean',
|
|
67
|
+
Ref = 'ref',
|
|
68
|
+
|
|
69
|
+
//
|
|
70
|
+
// { type: 'string' }
|
|
71
|
+
//
|
|
72
|
+
|
|
73
|
+
DID = 'did', // Users, etc.
|
|
74
|
+
DXN = 'dxn',
|
|
75
|
+
Email = 'email',
|
|
76
|
+
Formula = 'formula', // Spreadsheet formula.
|
|
77
|
+
Hostname = 'hostname',
|
|
78
|
+
JSON = 'json',
|
|
79
|
+
Markdown = 'markdown',
|
|
80
|
+
Regex = 'regex',
|
|
81
|
+
SingleSelect = 'single-select',
|
|
82
|
+
MultiSelect = 'multi-select',
|
|
83
|
+
URL = 'url',
|
|
84
|
+
UUID = 'uuid',
|
|
85
|
+
|
|
86
|
+
//
|
|
87
|
+
// { type: 'number' }
|
|
88
|
+
//
|
|
89
|
+
|
|
90
|
+
Currency = 'currency',
|
|
91
|
+
Integer = 'integer',
|
|
92
|
+
Percent = 'percent',
|
|
93
|
+
Timestamp = 'timestamp',
|
|
94
|
+
|
|
95
|
+
//
|
|
96
|
+
// { type: 'date' }
|
|
97
|
+
//
|
|
98
|
+
|
|
99
|
+
DateTime = 'date-time',
|
|
100
|
+
Date = 'date',
|
|
101
|
+
Time = 'time',
|
|
102
|
+
Duration = 'duration',
|
|
103
|
+
|
|
104
|
+
//
|
|
105
|
+
// { type: 'object' }
|
|
106
|
+
//
|
|
107
|
+
|
|
108
|
+
GeoPoint = 'lnglat',
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
export const FormatEnums = Object.values(FormatEnum).sort();
|
|
112
|
+
|
|
113
|
+
export const PropertyKind = {
|
|
114
|
+
type: TypeEnum,
|
|
115
|
+
format: FormatEnum,
|
|
116
|
+
};
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Default formats
|
|
120
|
+
*/
|
|
121
|
+
export const typeToFormat: Partial<Record<TypeEnum, FormatEnum>> = {
|
|
122
|
+
[TypeEnum.String]: FormatEnum.String,
|
|
123
|
+
[TypeEnum.Number]: FormatEnum.Number,
|
|
124
|
+
[TypeEnum.Boolean]: FormatEnum.Boolean,
|
|
125
|
+
};
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Map of format to type.
|
|
129
|
+
*/
|
|
130
|
+
export const formatToType: Record<FormatEnum, TypeEnum> = {
|
|
131
|
+
[FormatEnum.None]: undefined as any,
|
|
132
|
+
[FormatEnum.String]: TypeEnum.String,
|
|
133
|
+
[FormatEnum.Number]: TypeEnum.Number,
|
|
134
|
+
[FormatEnum.Boolean]: TypeEnum.Boolean,
|
|
135
|
+
[FormatEnum.Ref]: TypeEnum.Ref,
|
|
136
|
+
|
|
137
|
+
// Strings
|
|
138
|
+
[FormatEnum.DID]: TypeEnum.String,
|
|
139
|
+
[FormatEnum.DXN]: TypeEnum.String,
|
|
140
|
+
[FormatEnum.Email]: TypeEnum.String,
|
|
141
|
+
[FormatEnum.Formula]: TypeEnum.String,
|
|
142
|
+
[FormatEnum.Hostname]: TypeEnum.String,
|
|
143
|
+
[FormatEnum.JSON]: TypeEnum.String,
|
|
144
|
+
[FormatEnum.Markdown]: TypeEnum.String,
|
|
145
|
+
[FormatEnum.Regex]: TypeEnum.String,
|
|
146
|
+
[FormatEnum.URL]: TypeEnum.String,
|
|
147
|
+
[FormatEnum.UUID]: TypeEnum.String,
|
|
148
|
+
[FormatEnum.SingleSelect]: TypeEnum.String,
|
|
149
|
+
[FormatEnum.MultiSelect]: TypeEnum.Object,
|
|
150
|
+
|
|
151
|
+
// Dates
|
|
152
|
+
[FormatEnum.Date]: TypeEnum.String,
|
|
153
|
+
[FormatEnum.DateTime]: TypeEnum.String,
|
|
154
|
+
[FormatEnum.Duration]: TypeEnum.String,
|
|
155
|
+
[FormatEnum.Time]: TypeEnum.String,
|
|
156
|
+
|
|
157
|
+
// Numbers
|
|
158
|
+
[FormatEnum.Currency]: TypeEnum.Number,
|
|
159
|
+
[FormatEnum.Integer]: TypeEnum.Number,
|
|
160
|
+
[FormatEnum.Percent]: TypeEnum.Number,
|
|
161
|
+
[FormatEnum.Timestamp]: TypeEnum.Number,
|
|
162
|
+
|
|
163
|
+
// Objects
|
|
164
|
+
[FormatEnum.GeoPoint]: TypeEnum.Array,
|
|
165
|
+
};
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* Allowed value options for select.
|
|
169
|
+
*/
|
|
170
|
+
export const OptionsAnnotationId = Symbol.for('@dxos/schema/annotation/Options');
|
|
171
|
+
|
|
172
|
+
export const getOptionsAnnotation = (node: SchemaAST.AST): OptionsAnnotationType[] | undefined =>
|
|
173
|
+
Function.pipe(SchemaAST.getAnnotation<OptionsAnnotationType[]>(OptionsAnnotationId)(node), Option.getOrUndefined);
|
|
174
|
+
|
|
175
|
+
export type OptionsAnnotationType = string | number;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2024 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
export { JsonPath, splitJsonPath, JsonProp } from '@dxos/effect';
|
|
6
|
+
|
|
7
|
+
// TODO(dmaretskyi): Remove.
|
|
8
|
+
export { ObjectId } from '@dxos/keys';
|
|
9
|
+
export { ForeignKey } from '@dxos/echo-protocol';
|
|
10
|
+
|
|
11
|
+
export * from './ast';
|
|
12
|
+
export * from './formats';
|
|
13
|
+
export * from './json';
|
|
14
|
+
export * from './json-schema';
|
|
15
|
+
export * from './object';
|
|
16
|
+
export * from './types';
|
|
17
|
+
export { defineHiddenProperty } from '@dxos/live-object';
|
|
18
|
+
export * from './ref';
|
|
19
|
+
export * from './projection';
|
|
20
|
+
export * from './schema';
|
|
21
|
+
// TODO(dmaretskyi): Remove
|
|
22
|
+
export { live } from './proxy/reactive-object';
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import * as SchemaAST from 'effect/SchemaAST';
|
|
6
|
+
|
|
7
|
+
import { GeneratorAnnotationId, LabelAnnotationId, PropertyMetaAnnotationId } from '../ast';
|
|
8
|
+
import { CurrencyAnnotationId, FormatAnnotationId } from '../formats';
|
|
9
|
+
import { type JsonSchemaEchoAnnotations, type JsonSchemaType } from '../json-schema';
|
|
10
|
+
|
|
11
|
+
//
|
|
12
|
+
// This file configures annotations for JSON encoding/decoding.
|
|
13
|
+
//
|
|
14
|
+
|
|
15
|
+
// Go on the root level.
|
|
16
|
+
type RootJsonSchemaProperty = keyof JsonSchemaType;
|
|
17
|
+
|
|
18
|
+
// Go on the namespaced `annotations` property.
|
|
19
|
+
type NamespacedJsonSchemaProperty = keyof JsonSchemaEchoAnnotations;
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* List of annotations for JSON encoding/decoding.
|
|
23
|
+
* Omits default effect-schema annotations since they are encoded with default serializer.
|
|
24
|
+
*/
|
|
25
|
+
// TODO(burdon): Reconcile with `EchoAnnotations`.
|
|
26
|
+
export const CustomAnnotations: Partial<Record<RootJsonSchemaProperty, symbol>> = {
|
|
27
|
+
format: FormatAnnotationId,
|
|
28
|
+
currency: CurrencyAnnotationId,
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* List of annotations for JSON decoding only.
|
|
33
|
+
* Includes default effect annotations.
|
|
34
|
+
*/
|
|
35
|
+
export const DecodedAnnotations: Partial<Record<RootJsonSchemaProperty, symbol>> = {
|
|
36
|
+
title: SchemaAST.TitleAnnotationId,
|
|
37
|
+
description: SchemaAST.DescriptionAnnotationId,
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Annotations that go into ECHO namespace in json-schema.
|
|
42
|
+
*/
|
|
43
|
+
// TODO(dmaretskyi): Consider removing ECHO namespace and putting them at the top level.
|
|
44
|
+
// TODO(dmaretskyi): Move to format.ts when circular imports are solved
|
|
45
|
+
export const EchoAnnotations: Partial<Record<NamespacedJsonSchemaProperty, symbol>> = {
|
|
46
|
+
// TODO(dmaretskyi): `FieldLookupAnnotationId` might go here, but lets remove it entirely and use LabelAnnotation instead.
|
|
47
|
+
meta: PropertyMetaAnnotationId,
|
|
48
|
+
generator: GeneratorAnnotationId,
|
|
49
|
+
labelProp: LabelAnnotationId,
|
|
50
|
+
};
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2024 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import * as JSONSchema from 'effect/JSONSchema';
|
|
6
|
+
import * as Option from 'effect/Option';
|
|
7
|
+
import * as Schema from 'effect/Schema';
|
|
8
|
+
import * as SchemaAST from 'effect/SchemaAST';
|
|
9
|
+
import { expect, test } from 'vitest';
|
|
10
|
+
|
|
11
|
+
import { log } from '@dxos/log';
|
|
12
|
+
|
|
13
|
+
test('json-schema annotations for filter refinement get combined', () => {
|
|
14
|
+
const type = Schema.Number.annotations({
|
|
15
|
+
jsonSchema: { foo: 'foo' },
|
|
16
|
+
}).pipe(Schema.filter(() => true, { jsonSchema: { bar: 'bar' } }));
|
|
17
|
+
|
|
18
|
+
const jsonSchema = JSONSchema.make(type);
|
|
19
|
+
expect(jsonSchema).toEqual({
|
|
20
|
+
$schema: 'http://json-schema.org/draft-07/schema#',
|
|
21
|
+
foo: 'foo',
|
|
22
|
+
bar: 'bar',
|
|
23
|
+
type: 'number',
|
|
24
|
+
});
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
test('json-schema annotations on types do not override the default serialization', () => {
|
|
28
|
+
const type = Schema.Number.annotations({
|
|
29
|
+
jsonSchema: { foo: 'foo' },
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
const jsonSchema = JSONSchema.make(type);
|
|
33
|
+
expect(jsonSchema).toEqual({
|
|
34
|
+
$schema: 'http://json-schema.org/draft-07/schema#',
|
|
35
|
+
foo: 'foo',
|
|
36
|
+
type: 'number',
|
|
37
|
+
});
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
// pass
|
|
41
|
+
test('number with title and description annotations', () => {
|
|
42
|
+
const number = Schema.Number.annotations({
|
|
43
|
+
title: 'My Title',
|
|
44
|
+
description: 'My Description',
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
expect(JSONSchema.make(number)).toEqual({
|
|
48
|
+
$schema: 'http://json-schema.org/draft-07/schema#',
|
|
49
|
+
type: 'number',
|
|
50
|
+
title: 'My Title',
|
|
51
|
+
description: 'My Description',
|
|
52
|
+
});
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
// pass
|
|
56
|
+
test('date with title and description annotations', () => {
|
|
57
|
+
const date = Schema.Date.annotations({
|
|
58
|
+
title: 'My Title',
|
|
59
|
+
description: 'My Description',
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
expect(JSONSchema.make(date)).toEqual({
|
|
63
|
+
$schema: 'http://json-schema.org/draft-07/schema#',
|
|
64
|
+
$defs: {
|
|
65
|
+
DateFromString: {
|
|
66
|
+
description: 'a string to be decoded into a Date',
|
|
67
|
+
type: 'string',
|
|
68
|
+
},
|
|
69
|
+
},
|
|
70
|
+
$ref: '#/$defs/DateFromString',
|
|
71
|
+
});
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
// fail
|
|
75
|
+
test('declare', () => {
|
|
76
|
+
class MyType {}
|
|
77
|
+
const type = Schema.declare<MyType>((x) => x instanceof MyType, {
|
|
78
|
+
jsonSchema: {
|
|
79
|
+
type: 'my-type',
|
|
80
|
+
},
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
expect(JSONSchema.make(type)).toEqual({
|
|
84
|
+
$schema: 'http://json-schema.org/draft-07/schema#',
|
|
85
|
+
type: 'my-type',
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
expect(type.pipe(Schema.is)(new MyType())).toBe(true);
|
|
89
|
+
expect(type.pipe(Schema.is)({})).toBe(false);
|
|
90
|
+
|
|
91
|
+
const withAnnotations = type.annotations({
|
|
92
|
+
title: 'My Title',
|
|
93
|
+
description: 'My Description',
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
expect(JSONSchema.make(withAnnotations)).toEqual({
|
|
97
|
+
$schema: 'http://json-schema.org/draft-07/schema#',
|
|
98
|
+
type: 'my-type',
|
|
99
|
+
title: 'My Title',
|
|
100
|
+
description: 'My Description',
|
|
101
|
+
});
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
// pass
|
|
105
|
+
test('declare with refinement', () => {
|
|
106
|
+
class MyType {}
|
|
107
|
+
const type = Schema.declare<MyType>((x) => x instanceof MyType, {
|
|
108
|
+
jsonSchema: {
|
|
109
|
+
type: 'my-type',
|
|
110
|
+
},
|
|
111
|
+
}).pipe(Schema.filter(() => true, { jsonSchema: {} }));
|
|
112
|
+
|
|
113
|
+
const named = type.annotations({
|
|
114
|
+
title: 'My Title',
|
|
115
|
+
description: 'My Description',
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
expect(JSONSchema.make(named)).toEqual({
|
|
119
|
+
$schema: 'http://json-schema.org/draft-07/schema#',
|
|
120
|
+
type: 'my-type',
|
|
121
|
+
title: 'My Title',
|
|
122
|
+
description: 'My Description',
|
|
123
|
+
});
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
test("default title annotations don't get serialized", () => {
|
|
127
|
+
const schema = Schema.String;
|
|
128
|
+
|
|
129
|
+
expect(SchemaAST.getTitleAnnotation(schema.ast).pipe(Option.getOrUndefined)).toEqual('string');
|
|
130
|
+
expect(SchemaAST.getDescriptionAnnotation(schema.ast).pipe(Option.getOrUndefined)).toEqual('a string');
|
|
131
|
+
|
|
132
|
+
expect(JSONSchema.make(schema)).toEqual({
|
|
133
|
+
$schema: 'http://json-schema.org/draft-07/schema#',
|
|
134
|
+
type: 'string',
|
|
135
|
+
});
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
test.skip('ast comparison', () => {
|
|
139
|
+
log.info('ast', {
|
|
140
|
+
default: Schema.String.ast,
|
|
141
|
+
annotated: Schema.String.annotations({ title: 'Custom title', description: 'Custom description' }).ast,
|
|
142
|
+
});
|
|
143
|
+
});
|