@dxos/echo 0.8.4-main.72ec0f3 → 0.8.4-main.7ace549
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-BIDAASFK.mjs → chunk-7GH6RXJ3.mjs} +2246 -2290
- package/dist/lib/browser/chunk-7GH6RXJ3.mjs.map +7 -0
- package/dist/lib/browser/chunk-E4UTVJNF.mjs +1111 -0
- package/dist/lib/browser/chunk-E4UTVJNF.mjs.map +7 -0
- package/dist/lib/browser/index.mjs +18 -8
- package/dist/lib/browser/internal/index.mjs +45 -45
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/browser/testing/index.mjs +252 -191
- package/dist/lib/browser/testing/index.mjs.map +4 -4
- package/dist/lib/node-esm/chunk-JE5RXM2I.mjs +1111 -0
- package/dist/lib/node-esm/chunk-JE5RXM2I.mjs.map +7 -0
- package/dist/lib/node-esm/{chunk-3SVRRCUU.mjs → chunk-M4B6BMD2.mjs} +2246 -2290
- package/dist/lib/node-esm/chunk-M4B6BMD2.mjs.map +7 -0
- package/dist/lib/node-esm/index.mjs +18 -8
- package/dist/lib/node-esm/internal/index.mjs +45 -45
- package/dist/lib/node-esm/meta.json +1 -1
- package/dist/lib/node-esm/testing/index.mjs +252 -191
- package/dist/lib/node-esm/testing/index.mjs.map +4 -4
- package/dist/types/src/Annotation.d.ts +2 -0
- package/dist/types/src/Annotation.d.ts.map +1 -0
- package/dist/types/src/Database.d.ts +137 -0
- package/dist/types/src/Database.d.ts.map +1 -0
- package/dist/types/src/Entity.d.ts +36 -0
- package/dist/types/src/Entity.d.ts.map +1 -0
- package/dist/types/src/Format.d.ts +4 -0
- package/dist/types/src/Format.d.ts.map +1 -0
- package/dist/types/src/JsonSchema.d.ts +9 -0
- package/dist/types/src/JsonSchema.d.ts.map +1 -0
- package/dist/types/src/Key.d.ts +1 -0
- package/dist/types/src/Key.d.ts.map +1 -1
- package/dist/types/src/Obj.d.ts +79 -70
- package/dist/types/src/Obj.d.ts.map +1 -1
- package/dist/types/src/Ref.d.ts +10 -10
- package/dist/types/src/Ref.d.ts.map +1 -1
- package/dist/types/src/Relation.d.ts +14 -11
- package/dist/types/src/Relation.d.ts.map +1 -1
- package/dist/types/src/Tag.d.ts +1 -1
- package/dist/types/src/Tag.d.ts.map +1 -1
- package/dist/types/src/Type.d.ts +39 -49
- package/dist/types/src/Type.d.ts.map +1 -1
- package/dist/types/src/index.d.ts +6 -3
- package/dist/types/src/index.d.ts.map +1 -1
- package/dist/types/src/internal/annotations/annotations.d.ts +174 -0
- package/dist/types/src/internal/annotations/annotations.d.ts.map +1 -0
- package/dist/types/src/internal/annotations/annotations.test.d.ts.map +1 -0
- package/dist/types/src/internal/annotations/index.d.ts +3 -0
- package/dist/types/src/internal/annotations/index.d.ts.map +1 -0
- package/dist/types/src/internal/annotations/util.d.ts +26 -0
- package/dist/types/src/internal/annotations/util.d.ts.map +1 -0
- package/dist/types/src/internal/entities/entity.d.ts +10 -0
- package/dist/types/src/internal/entities/entity.d.ts.map +1 -0
- package/dist/types/src/internal/{object → entities}/expando.d.ts +3 -1
- package/dist/types/src/internal/entities/expando.d.ts.map +1 -0
- package/dist/types/src/internal/entities/index.d.ts +6 -0
- package/dist/types/src/internal/entities/index.d.ts.map +1 -0
- package/dist/types/src/internal/entities/model.d.ts +70 -0
- package/dist/types/src/internal/entities/model.d.ts.map +1 -0
- package/dist/types/src/internal/entities/object.d.ts +11 -0
- package/dist/types/src/internal/entities/object.d.ts.map +1 -0
- package/dist/types/src/internal/entities/relation.d.ts +55 -0
- package/dist/types/src/internal/entities/relation.d.ts.map +1 -0
- package/dist/types/src/internal/entities/util.d.ts +2 -0
- package/dist/types/src/internal/entities/util.d.ts.map +1 -0
- package/dist/types/src/internal/formats/format.d.ts +2 -0
- package/dist/types/src/internal/formats/format.d.ts.map +1 -1
- package/dist/types/src/internal/formats/types.d.ts +7 -7
- package/dist/types/src/internal/formats/types.d.ts.map +1 -1
- package/dist/types/src/internal/index.d.ts +6 -10
- package/dist/types/src/internal/index.d.ts.map +1 -1
- package/dist/types/src/internal/json-schema/annotations.d.ts.map +1 -0
- package/dist/types/src/internal/json-schema/effect-schema.test.d.ts.map +1 -0
- package/dist/types/src/internal/json-schema/index.d.ts +2 -0
- package/dist/types/src/internal/json-schema/index.d.ts.map +1 -1
- package/dist/types/src/internal/json-schema/json-schema-type.d.ts.map +1 -1
- package/dist/types/src/internal/{json → json-schema}/json-schema.d.ts +2 -1
- package/dist/types/src/internal/json-schema/json-schema.d.ts.map +1 -0
- package/dist/types/src/internal/json-schema/json-schema.test.d.ts.map +1 -0
- package/dist/types/src/internal/object/common.d.ts.map +1 -1
- package/dist/types/src/internal/object/{create.d.ts → create-object.d.ts} +8 -9
- package/dist/types/src/internal/object/create-object.d.ts.map +1 -0
- package/dist/types/src/internal/object/create-object.test.d.ts +2 -0
- package/dist/types/src/internal/object/create-object.test.d.ts.map +1 -0
- package/dist/types/src/internal/object/deleted.d.ts +2 -2
- package/dist/types/src/internal/object/deleted.d.ts.map +1 -1
- package/dist/types/src/internal/object/ids.d.ts.map +1 -1
- package/dist/types/src/internal/object/index.d.ts +3 -11
- package/dist/types/src/internal/object/index.d.ts.map +1 -1
- package/dist/types/src/internal/object/json-serializer.d.ts +1 -2
- package/dist/types/src/internal/object/json-serializer.d.ts.map +1 -1
- package/dist/types/src/internal/object/schema-validator.d.ts.map +1 -1
- package/dist/types/src/internal/object/typed-object.d.ts +1 -1
- package/dist/types/src/internal/object/typed-object.d.ts.map +1 -1
- package/dist/types/src/internal/proxy/index.d.ts +3 -0
- package/dist/types/src/internal/proxy/index.d.ts.map +1 -0
- package/dist/types/src/internal/proxy/make-object.d.ts +16 -0
- package/dist/types/src/internal/proxy/make-object.d.ts.map +1 -0
- package/dist/types/src/internal/proxy/typed-handler.d.ts +1 -1
- package/dist/types/src/internal/proxy/typed-handler.d.ts.map +1 -1
- package/dist/types/src/internal/ref/ref.d.ts +15 -12
- package/dist/types/src/internal/ref/ref.d.ts.map +1 -1
- package/dist/types/src/internal/schema/compose.d.ts.map +1 -0
- package/dist/types/src/internal/schema/compose.test.d.ts.map +1 -0
- package/dist/types/src/internal/schema/echo-schema.d.ts +9 -9
- package/dist/types/src/internal/schema/echo-schema.d.ts.map +1 -1
- package/dist/types/src/internal/schema/index.d.ts +3 -4
- package/dist/types/src/internal/schema/index.d.ts.map +1 -1
- package/dist/types/src/internal/schema/persistent-schema.d.ts +18 -0
- package/dist/types/src/internal/schema/persistent-schema.d.ts.map +1 -0
- package/dist/types/src/internal/schema/runtime-schema-registry.d.ts +1 -1
- package/dist/types/src/internal/schema/runtime-schema-registry.d.ts.map +1 -1
- package/dist/types/src/internal/types/base.d.ts +37 -0
- package/dist/types/src/internal/types/base.d.ts.map +1 -0
- package/dist/types/src/internal/{ast/entity-kind.d.ts → types/entity.d.ts} +3 -1
- package/dist/types/src/internal/types/entity.d.ts.map +1 -0
- package/dist/types/src/internal/types/index.d.ts +5 -2
- package/dist/types/src/internal/types/index.d.ts.map +1 -1
- package/dist/types/src/internal/{object → types}/meta.d.ts +20 -11
- package/dist/types/src/internal/types/meta.d.ts.map +1 -0
- package/dist/types/src/internal/types/typename.d.ts +13 -0
- package/dist/types/src/internal/types/typename.d.ts.map +1 -0
- package/dist/types/src/internal/{object → types}/version.d.ts +3 -2
- package/dist/types/src/internal/types/version.d.ts.map +1 -0
- package/dist/types/src/query/filter.d.ts +167 -0
- package/dist/types/src/query/filter.d.ts.map +1 -0
- package/dist/types/src/query/index.d.ts +3 -0
- package/dist/types/src/query/index.d.ts.map +1 -1
- package/dist/types/src/query/order.d.ts +12 -0
- package/dist/types/src/query/order.d.ts.map +1 -0
- package/dist/types/src/query/query.d.ts +9 -145
- package/dist/types/src/query/query.d.ts.map +1 -1
- package/dist/types/src/query/testing.d.ts +51 -0
- package/dist/types/src/query/testing.d.ts.map +1 -0
- package/dist/types/src/{internal/query/query.d.ts → query/types.d.ts} +1 -1
- package/dist/types/src/query/types.d.ts.map +1 -0
- package/dist/types/src/query/util.d.ts +8 -0
- package/dist/types/src/query/util.d.ts.map +1 -0
- package/dist/types/src/{test → testing}/api.test.d.ts.map +1 -1
- package/dist/types/src/testing/index.d.ts +3 -3
- package/dist/types/src/testing/index.d.ts.map +1 -1
- package/dist/types/src/testing/test-data.d.ts +18 -0
- package/dist/types/src/testing/test-data.d.ts.map +1 -0
- package/dist/types/src/testing/{types.d.ts → test-schema.d.ts} +166 -57
- package/dist/types/src/testing/test-schema.d.ts.map +1 -0
- package/dist/types/src/{internal/testing/utils.d.ts → testing/util.d.ts} +11 -5
- package/dist/types/src/testing/util.d.ts.map +1 -0
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +46 -38
- package/src/Annotation.ts +17 -0
- package/src/Database.ts +189 -0
- package/src/Entity.ts +51 -0
- package/src/Format.ts +11 -0
- package/src/JsonSchema.ts +16 -0
- package/src/Key.ts +3 -0
- package/src/Obj.ts +248 -181
- package/src/Ref.ts +9 -9
- package/src/Relation.ts +58 -44
- package/src/Tag.ts +2 -1
- package/src/Type.ts +96 -83
- package/src/index.ts +9 -4
- package/src/internal/README.md +83 -0
- package/src/internal/{ast → annotations}/annotations.test.ts +13 -15
- package/src/internal/annotations/annotations.ts +463 -0
- package/src/internal/annotations/index.ts +6 -0
- package/src/internal/annotations/util.ts +70 -0
- package/src/internal/entities/entity.ts +109 -0
- package/src/internal/{object → entities}/expando.ts +7 -5
- package/src/internal/entities/index.ts +9 -0
- package/src/internal/{object → entities}/model.ts +49 -90
- package/src/internal/entities/object.ts +45 -0
- package/src/internal/entities/relation.ts +155 -0
- package/src/internal/entities/util.ts +33 -0
- package/src/internal/formats/date.test.ts +1 -1
- package/src/internal/formats/date.ts +5 -5
- package/src/internal/formats/format.test.ts +5 -5
- package/src/internal/formats/format.ts +3 -0
- package/src/internal/formats/number.ts +5 -5
- package/src/internal/formats/object.ts +2 -2
- package/src/internal/formats/string.ts +9 -9
- package/src/internal/formats/types.ts +44 -39
- package/src/internal/index.ts +29 -13
- package/src/internal/{json → json-schema}/annotations.ts +1 -1
- package/src/internal/json-schema/index.ts +2 -0
- package/src/internal/json-schema/json-schema-type.ts +4 -3
- package/src/internal/{json → json-schema}/json-schema.test.ts +48 -38
- package/src/internal/{json → json-schema}/json-schema.ts +16 -7
- package/src/internal/object/common.ts +3 -4
- package/src/internal/object/{create.test.ts → create-object.test.ts} +30 -32
- package/src/internal/object/{create.ts → create-object.ts} +33 -34
- package/src/internal/object/deleted.ts +6 -6
- package/src/internal/object/ids.ts +1 -1
- package/src/internal/object/index.ts +3 -11
- package/src/internal/object/inspect.ts +3 -5
- package/src/internal/object/json-serializer.test.ts +33 -38
- package/src/internal/object/json-serializer.ts +43 -38
- package/src/internal/object/schema-validator.ts +3 -2
- package/src/internal/object/typed-object.test.ts +1 -1
- package/src/internal/object/typed-object.ts +11 -5
- package/src/internal/proxy/handler.test.ts +30 -20
- package/src/internal/proxy/index.ts +6 -0
- package/src/internal/proxy/{reactive-object.ts → make-object.ts} +19 -14
- package/src/internal/proxy/schema.test.ts +5 -4
- package/src/internal/proxy/typed-handler.test.ts +28 -28
- package/src/internal/proxy/typed-handler.ts +8 -3
- package/src/internal/proxy/typed-object.test.ts +16 -11
- package/src/internal/ref/ref.test.ts +8 -7
- package/src/internal/ref/ref.ts +24 -20
- package/src/internal/{projection → schema}/compose.test.ts +5 -6
- package/src/internal/{projection → schema}/compose.ts +2 -1
- package/src/internal/schema/echo-schema.ts +30 -28
- package/src/internal/schema/index.ts +3 -4
- package/src/internal/schema/manipulation.ts +1 -1
- package/src/internal/schema/persistent-schema.ts +28 -0
- package/src/internal/schema/runtime-schema-registry.ts +4 -4
- package/src/internal/types/base.ts +58 -0
- package/src/internal/{ast/entity-kind.ts → types/entity.ts} +8 -0
- package/src/internal/types/index.ts +5 -2
- package/src/internal/{object → types}/meta.ts +32 -17
- package/src/internal/types/typename.ts +45 -0
- package/src/internal/types/version.ts +20 -0
- package/src/query/filter.ts +455 -0
- package/src/query/index.ts +4 -1
- package/src/query/order.ts +34 -0
- package/src/query/query.test.ts +48 -115
- package/src/query/query.ts +16 -502
- package/src/query/testing.ts +64 -0
- package/src/query/util.ts +25 -0
- package/src/testing/api.test.ts +100 -0
- package/src/testing/index.ts +3 -3
- package/src/testing/test-data.ts +130 -0
- package/src/testing/test-schema.ts +213 -0
- package/src/{internal/testing/utils.ts → testing/util.ts} +38 -14
- package/dist/lib/browser/chunk-BIDAASFK.mjs.map +0 -7
- package/dist/lib/browser/chunk-ZDLCWGEW.mjs +0 -410
- package/dist/lib/browser/chunk-ZDLCWGEW.mjs.map +0 -7
- package/dist/lib/browser/chunk-ZFRJKT4A.mjs +0 -585
- package/dist/lib/browser/chunk-ZFRJKT4A.mjs.map +0 -7
- package/dist/lib/browser/query/index.mjs +0 -13
- package/dist/lib/browser/query/index.mjs.map +0 -7
- package/dist/lib/node-esm/chunk-3SVRRCUU.mjs.map +0 -7
- package/dist/lib/node-esm/chunk-CGDHRZWH.mjs +0 -585
- package/dist/lib/node-esm/chunk-CGDHRZWH.mjs.map +0 -7
- package/dist/lib/node-esm/chunk-HWS6VBQC.mjs +0 -410
- package/dist/lib/node-esm/chunk-HWS6VBQC.mjs.map +0 -7
- package/dist/lib/node-esm/query/index.mjs +0 -13
- package/dist/lib/node-esm/query/index.mjs.map +0 -7
- package/dist/types/src/internal/ast/annotation-helper.d.ts +0 -8
- package/dist/types/src/internal/ast/annotation-helper.d.ts.map +0 -1
- package/dist/types/src/internal/ast/annotations.d.ts +0 -131
- package/dist/types/src/internal/ast/annotations.d.ts.map +0 -1
- package/dist/types/src/internal/ast/annotations.test.d.ts.map +0 -1
- package/dist/types/src/internal/ast/entity-kind.d.ts.map +0 -1
- package/dist/types/src/internal/ast/index.d.ts +0 -5
- package/dist/types/src/internal/ast/index.d.ts.map +0 -1
- package/dist/types/src/internal/ast/types.d.ts +0 -6
- package/dist/types/src/internal/ast/types.d.ts.map +0 -1
- package/dist/types/src/internal/json/annotations.d.ts.map +0 -1
- package/dist/types/src/internal/json/effect-schema.test.d.ts.map +0 -1
- package/dist/types/src/internal/json/index.d.ts +0 -2
- package/dist/types/src/internal/json/index.d.ts.map +0 -1
- package/dist/types/src/internal/json/json-schema.d.ts.map +0 -1
- package/dist/types/src/internal/json/json-schema.test.d.ts.map +0 -1
- package/dist/types/src/internal/object/accessors.d.ts +0 -37
- package/dist/types/src/internal/object/accessors.d.ts.map +0 -1
- package/dist/types/src/internal/object/create.d.ts.map +0 -1
- package/dist/types/src/internal/object/create.test.d.ts +0 -2
- package/dist/types/src/internal/object/create.test.d.ts.map +0 -1
- package/dist/types/src/internal/object/entity.d.ts +0 -33
- package/dist/types/src/internal/object/entity.d.ts.map +0 -1
- package/dist/types/src/internal/object/expando.d.ts.map +0 -1
- package/dist/types/src/internal/object/meta.d.ts.map +0 -1
- package/dist/types/src/internal/object/model.d.ts +0 -117
- package/dist/types/src/internal/object/model.d.ts.map +0 -1
- package/dist/types/src/internal/object/relation.d.ts +0 -17
- package/dist/types/src/internal/object/relation.d.ts.map +0 -1
- package/dist/types/src/internal/object/typename.d.ts +0 -15
- package/dist/types/src/internal/object/typename.d.ts.map +0 -1
- package/dist/types/src/internal/object/version.d.ts.map +0 -1
- package/dist/types/src/internal/projection/compose.d.ts.map +0 -1
- package/dist/types/src/internal/projection/compose.test.d.ts.map +0 -1
- package/dist/types/src/internal/projection/index.d.ts +0 -2
- package/dist/types/src/internal/projection/index.d.ts.map +0 -1
- package/dist/types/src/internal/proxy/reactive-object.d.ts +0 -15
- package/dist/types/src/internal/proxy/reactive-object.d.ts.map +0 -1
- package/dist/types/src/internal/query/index.d.ts +0 -2
- package/dist/types/src/internal/query/index.d.ts.map +0 -1
- package/dist/types/src/internal/query/query.d.ts.map +0 -1
- package/dist/types/src/internal/schema/stored-schema.d.ts +0 -13
- package/dist/types/src/internal/schema/stored-schema.d.ts.map +0 -1
- package/dist/types/src/internal/testing/index.d.ts +0 -3
- package/dist/types/src/internal/testing/index.d.ts.map +0 -1
- package/dist/types/src/internal/testing/types.d.ts +0 -381
- package/dist/types/src/internal/testing/types.d.ts.map +0 -1
- package/dist/types/src/internal/testing/utils.d.ts.map +0 -1
- package/dist/types/src/internal/types/types.d.ts +0 -79
- package/dist/types/src/internal/types/types.d.ts.map +0 -1
- package/dist/types/src/internal/types/types.test.d.ts +0 -2
- package/dist/types/src/internal/types/types.test.d.ts.map +0 -1
- package/dist/types/src/internal/types/util.d.ts +0 -5
- package/dist/types/src/internal/types/util.d.ts.map +0 -1
- package/dist/types/src/testing/echo-schema.d.ts +0 -7
- package/dist/types/src/testing/echo-schema.d.ts.map +0 -1
- package/dist/types/src/testing/types.d.ts.map +0 -1
- package/src/internal/ast/annotation-helper.ts +0 -22
- package/src/internal/ast/annotations.ts +0 -226
- package/src/internal/ast/index.ts +0 -8
- package/src/internal/ast/types.ts +0 -17
- package/src/internal/json/index.ts +0 -5
- package/src/internal/object/accessors.ts +0 -153
- package/src/internal/object/entity.ts +0 -248
- package/src/internal/object/relation.ts +0 -24
- package/src/internal/object/typename.ts +0 -61
- package/src/internal/object/version.ts +0 -22
- package/src/internal/projection/index.ts +0 -5
- package/src/internal/query/index.ts +0 -5
- package/src/internal/schema/stored-schema.ts +0 -26
- package/src/internal/testing/index.ts +0 -6
- package/src/internal/testing/types.ts +0 -144
- package/src/internal/types/types.test.ts +0 -48
- package/src/internal/types/types.ts +0 -176
- package/src/internal/types/util.ts +0 -9
- package/src/test/api.test.ts +0 -180
- package/src/testing/echo-schema.ts +0 -39
- package/src/testing/types.ts +0 -108
- /package/dist/types/src/internal/{ast → annotations}/annotations.test.d.ts +0 -0
- /package/dist/types/src/internal/{json → json-schema}/annotations.d.ts +0 -0
- /package/dist/types/src/internal/{json → json-schema}/effect-schema.test.d.ts +0 -0
- /package/dist/types/src/internal/{json → json-schema}/json-schema.test.d.ts +0 -0
- /package/dist/types/src/internal/{projection → schema}/compose.d.ts +0 -0
- /package/dist/types/src/internal/{projection → schema}/compose.test.d.ts +0 -0
- /package/dist/types/src/{test → testing}/api.test.d.ts +0 -0
- /package/src/internal/{json → json-schema}/effect-schema.test.ts +0 -0
- /package/src/{internal/query/query.ts → query/types.ts} +0 -0
|
@@ -5,9 +5,9 @@
|
|
|
5
5
|
import * as Schema from 'effect/Schema';
|
|
6
6
|
import { describe, test } from 'vitest';
|
|
7
7
|
|
|
8
|
-
import {
|
|
8
|
+
import { EchoObjectSchema } from '../entities';
|
|
9
9
|
|
|
10
|
-
import { LabelAnnotation,
|
|
10
|
+
import { LabelAnnotation, TypenameSchema, VersionSchema, getLabel } from './annotations';
|
|
11
11
|
|
|
12
12
|
// TODO(dmaretskyi): Use one of the testing schemas.
|
|
13
13
|
const TestObject = Schema.Struct({
|
|
@@ -16,40 +16,38 @@ const TestObject = Schema.Struct({
|
|
|
16
16
|
other: Schema.String,
|
|
17
17
|
}).pipe(LabelAnnotation.set(['name', 'fallbackName']));
|
|
18
18
|
|
|
19
|
-
const a: Schema.Struct<typeof TestObject.fields> = TestObject;
|
|
20
|
-
console.log(a);
|
|
21
|
-
|
|
22
19
|
type TestObject = Schema.Schema.Type<typeof TestObject>;
|
|
23
20
|
|
|
24
21
|
const TestEchoSchema = TestObject.pipe(
|
|
25
|
-
|
|
22
|
+
EchoObjectSchema({
|
|
26
23
|
typename: 'dxos.org/type/Test',
|
|
27
24
|
version: '0.1.0',
|
|
28
25
|
}),
|
|
29
26
|
);
|
|
27
|
+
|
|
30
28
|
type TestEchoSchema = Schema.Schema.Type<typeof TestEchoSchema>;
|
|
31
29
|
|
|
32
30
|
describe('annotations', () => {
|
|
33
31
|
describe('Typename', () => {
|
|
34
32
|
test('should validate typename', ({ expect }) => {
|
|
35
33
|
// Valid.
|
|
36
|
-
expect(
|
|
37
|
-
expect(
|
|
38
|
-
expect(
|
|
34
|
+
expect(TypenameSchema.make('dxos.org/type/foo')).to.exist;
|
|
35
|
+
expect(TypenameSchema.make('dxos.org/type/foo-bar')).to.exist;
|
|
36
|
+
expect(TypenameSchema.make('dxos.org/type/foo_bar')).to.exist;
|
|
39
37
|
|
|
40
38
|
// Invalid.
|
|
41
|
-
expect(() =>
|
|
42
|
-
expect(() =>
|
|
43
|
-
expect(() =>
|
|
39
|
+
expect(() => TypenameSchema.make('dxn:dxos.org')).to.throw();
|
|
40
|
+
expect(() => TypenameSchema.make('2dxos.org')).to.throw();
|
|
41
|
+
expect(() => TypenameSchema.make('dxos org')).to.throw();
|
|
44
42
|
});
|
|
45
43
|
|
|
46
44
|
test('should validate version', ({ expect }) => {
|
|
47
45
|
// Valid.
|
|
48
|
-
expect(
|
|
46
|
+
expect(VersionSchema.make('0.1.0')).to.exist;
|
|
49
47
|
|
|
50
48
|
// Invalid.
|
|
51
|
-
expect(() =>
|
|
52
|
-
expect(() =>
|
|
49
|
+
expect(() => VersionSchema.make('0.1.x')).to.throw();
|
|
50
|
+
expect(() => VersionSchema.make('0.1.0-alpha')).to.throw();
|
|
53
51
|
});
|
|
54
52
|
});
|
|
55
53
|
|
|
@@ -0,0 +1,463 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2024 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import * as Function from 'effect/Function';
|
|
6
|
+
import * as Option from 'effect/Option';
|
|
7
|
+
import * as Schema from 'effect/Schema';
|
|
8
|
+
import * as SchemaAST from 'effect/SchemaAST';
|
|
9
|
+
|
|
10
|
+
import { Reference } from '@dxos/echo-protocol';
|
|
11
|
+
import { type JsonPath, getField } from '@dxos/effect';
|
|
12
|
+
import { assertArgument, invariant } from '@dxos/invariant';
|
|
13
|
+
import { DXN } from '@dxos/keys';
|
|
14
|
+
import { type Primitive } from '@dxos/util';
|
|
15
|
+
|
|
16
|
+
import { type AnyProperties, EntityKind, TypeId, getSchema } from '../types';
|
|
17
|
+
|
|
18
|
+
import { createAnnotationHelper } from './util';
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* @internal
|
|
22
|
+
*/
|
|
23
|
+
export const FIELD_PATH_ANNOTATION = 'path';
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Sets the path for the field.
|
|
27
|
+
* @param path Data source path in the json path format. This is the field path in the source object.
|
|
28
|
+
*/
|
|
29
|
+
// TODO(burdon): Field, vs. path vs. property.
|
|
30
|
+
export const FieldPath = (path: string) => PropertyMeta(FIELD_PATH_ANNOTATION, path);
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* ECHO identifier (for a stored schema).
|
|
34
|
+
* Must be a `dxn:echo:` URI.
|
|
35
|
+
*/
|
|
36
|
+
export const TypeIdentifierAnnotationId = Symbol.for('@dxos/schema/annotation/TypeIdentifier');
|
|
37
|
+
|
|
38
|
+
export const getTypeIdentifierAnnotation = (schema: Schema.Schema.All) =>
|
|
39
|
+
Function.flow(
|
|
40
|
+
SchemaAST.getAnnotation<string>(TypeIdentifierAnnotationId),
|
|
41
|
+
Option.getOrElse(() => undefined),
|
|
42
|
+
)(schema.ast);
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Fully qualified globally unique typename.
|
|
46
|
+
* Example: `dxos.org/type/Person`
|
|
47
|
+
*/
|
|
48
|
+
// TODO(burdon): Reconcile with short DXN format.
|
|
49
|
+
// TODO(burdon): Change type => schema throughout.
|
|
50
|
+
export const TypenameSchema = Schema.String.pipe(Schema.pattern(/^[a-zA-Z]\w+\.[a-zA-Z]\w{1,}\/[\w/_-]+$/)).annotations(
|
|
51
|
+
{
|
|
52
|
+
description: 'Fully qualified globally unique typename',
|
|
53
|
+
example: 'dxos.org/type/Person',
|
|
54
|
+
},
|
|
55
|
+
);
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Semantic version format: `major.minor.patch`
|
|
59
|
+
* Example: `1.0.0`
|
|
60
|
+
*/
|
|
61
|
+
export const VersionSchema = Schema.String.pipe(Schema.pattern(/^\d+.\d+.\d+$/)).annotations({
|
|
62
|
+
description: 'Semantic version format: `major.minor.patch`',
|
|
63
|
+
example: '1.0.0',
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
export const TypeMeta = Schema.Struct({
|
|
67
|
+
typename: TypenameSchema,
|
|
68
|
+
version: VersionSchema,
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
export interface TypeMeta extends Schema.Schema.Type<typeof TypeMeta> {}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Entity type.
|
|
75
|
+
*/
|
|
76
|
+
export const TypeAnnotationId = Symbol.for('@dxos/schema/annotation/Type');
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Payload stored under {@link TypeAnnotationId}.
|
|
80
|
+
*/
|
|
81
|
+
export const TypeAnnotation = Schema.extend(
|
|
82
|
+
TypeMeta,
|
|
83
|
+
Schema.Struct({
|
|
84
|
+
kind: Schema.Enums(EntityKind),
|
|
85
|
+
// kind: Schema.Enums(EntityKind),
|
|
86
|
+
// typename: Typename,
|
|
87
|
+
// version: VersionSchema,
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* If this is a relation, the schema of the source object.
|
|
91
|
+
* Must be present if entity kind is {@link EntityKind.Relation}.
|
|
92
|
+
*/
|
|
93
|
+
sourceSchema: Schema.optional(DXN.Schema),
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* If this is a relation, the schema of the target object.
|
|
97
|
+
* Must be present if entity kind is {@link EntityKind.Relation}.
|
|
98
|
+
*/
|
|
99
|
+
targetSchema: Schema.optional(DXN.Schema),
|
|
100
|
+
}),
|
|
101
|
+
);
|
|
102
|
+
|
|
103
|
+
export interface TypeAnnotation extends Schema.Schema.Type<typeof TypeAnnotation> {}
|
|
104
|
+
|
|
105
|
+
// export type TypeMeta = Pick<TypeAnnotation, 'typename' | 'version'>;
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* @returns {@link TypeAnnotation} from a schema.
|
|
109
|
+
* Schema must have been created with {@link TypedObject} or {@link TypedLink} or manually assigned an appropriate annotation.
|
|
110
|
+
*/
|
|
111
|
+
export const getTypeAnnotation = (schema: Schema.Schema.All): TypeAnnotation | undefined => {
|
|
112
|
+
assertArgument(schema != null && schema.ast != null, 'schema', 'invalid schema');
|
|
113
|
+
return Function.flow(
|
|
114
|
+
SchemaAST.getAnnotation<TypeAnnotation>(TypeAnnotationId),
|
|
115
|
+
Option.getOrElse(() => undefined),
|
|
116
|
+
)(schema.ast);
|
|
117
|
+
};
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* @returns {@link EntityKind} from a schema.
|
|
121
|
+
*/
|
|
122
|
+
export const getEntityKind = (schema: Schema.Schema.All): EntityKind | undefined => getTypeAnnotation(schema)?.kind;
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* @internal
|
|
126
|
+
* @returns Schema typename (without dxn: prefix or version number).
|
|
127
|
+
*/
|
|
128
|
+
export const getSchemaTypename = (schema: Schema.Schema.All): string | undefined => getTypeAnnotation(schema)?.typename;
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* @internal
|
|
132
|
+
* @returns Schema version in semver format.
|
|
133
|
+
*/
|
|
134
|
+
export const getSchemaVersion = (schema: Schema.Schema.All): string | undefined => getTypeAnnotation(schema)?.version;
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* Gets the typename of the object without the version.
|
|
138
|
+
* Returns only the name portion, not the DXN.
|
|
139
|
+
* @example "example.org/type/Contact"
|
|
140
|
+
*
|
|
141
|
+
* @internal (use Obj.getTypename)
|
|
142
|
+
*/
|
|
143
|
+
export const getTypename = (obj: AnyProperties): string | undefined => {
|
|
144
|
+
const schema = getSchema(obj);
|
|
145
|
+
if (schema != null) {
|
|
146
|
+
// Try to extract typename from DXN.
|
|
147
|
+
return getSchemaTypename(schema);
|
|
148
|
+
} else {
|
|
149
|
+
const type = getTypeDXN(obj);
|
|
150
|
+
return type?.asTypeDXN()?.type;
|
|
151
|
+
}
|
|
152
|
+
};
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* @internal (use Type.setTypename)
|
|
156
|
+
*/
|
|
157
|
+
// TODO(dmaretskyi): Rename setTypeDXN.
|
|
158
|
+
export const setTypename = (obj: any, typename: DXN): void => {
|
|
159
|
+
invariant(typename instanceof DXN, 'Invalid type.');
|
|
160
|
+
Object.defineProperty(obj, TypeId, {
|
|
161
|
+
value: typename,
|
|
162
|
+
writable: false,
|
|
163
|
+
enumerable: false,
|
|
164
|
+
configurable: false,
|
|
165
|
+
});
|
|
166
|
+
};
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* @returns Object type as {@link DXN}.
|
|
170
|
+
* @returns undefined if the object doesn't have a type.
|
|
171
|
+
* @example `dxn:example.com/type/Person:1.0.0`
|
|
172
|
+
*
|
|
173
|
+
* @internal (use Obj.getTypeDXN)
|
|
174
|
+
*/
|
|
175
|
+
// TODO(burdon): Narrow type.
|
|
176
|
+
export const getTypeDXN = (obj: AnyProperties): DXN | undefined => {
|
|
177
|
+
if (!obj) {
|
|
178
|
+
return undefined;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
const type = (obj as any)[TypeId];
|
|
182
|
+
if (!type) {
|
|
183
|
+
return undefined;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
invariant(type instanceof DXN, 'Invalid object.');
|
|
187
|
+
return type;
|
|
188
|
+
};
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* Checks if the object is an instance of the schema.
|
|
192
|
+
* Only typename is compared, the schema version is ignored.
|
|
193
|
+
*
|
|
194
|
+
* The following cases are considered to mean that the object is an instance of the schema:
|
|
195
|
+
* - Object was created with this exact schema.
|
|
196
|
+
* - Object was created with a different version of this schema.
|
|
197
|
+
* - Object was created with a different schema (maybe dynamic) that has the same typename.
|
|
198
|
+
*/
|
|
199
|
+
// TODO(burdon): Can we use `Schema.is`?
|
|
200
|
+
export const isInstanceOf = <Schema extends Schema.Schema.AnyNoContext>(
|
|
201
|
+
schema: Schema,
|
|
202
|
+
object: any,
|
|
203
|
+
): object is Schema.Schema.Type<Schema> => {
|
|
204
|
+
if (object == null) {
|
|
205
|
+
return false;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
const schemaDXN = getSchemaDXN(schema);
|
|
209
|
+
if (!schemaDXN) {
|
|
210
|
+
throw new Error('Schema must have an object annotation.');
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
const type = getTypeDXN(object);
|
|
214
|
+
if (type && DXN.equals(type, schemaDXN)) {
|
|
215
|
+
return true;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
const typename = getTypename(object);
|
|
219
|
+
if (!typename) {
|
|
220
|
+
return false;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
const typeDXN = schemaDXN.asTypeDXN();
|
|
224
|
+
if (!typeDXN) {
|
|
225
|
+
return false;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
return typeDXN.type === typename;
|
|
229
|
+
};
|
|
230
|
+
|
|
231
|
+
/**
|
|
232
|
+
* PropertyMeta (metadata for dynamic schema properties).
|
|
233
|
+
* For user-defined annotations.
|
|
234
|
+
*/
|
|
235
|
+
export const PropertyMetaAnnotationId = Symbol.for('@dxos/schema/annotation/PropertyMeta');
|
|
236
|
+
|
|
237
|
+
export type PropertyMetaValue = Primitive | Record<string, Primitive> | Primitive[];
|
|
238
|
+
|
|
239
|
+
export type PropertyMetaAnnotation = {
|
|
240
|
+
[name: string]: PropertyMetaValue;
|
|
241
|
+
};
|
|
242
|
+
|
|
243
|
+
// TODO(wittjosiah): Align with other annotations.
|
|
244
|
+
// TODO(wittjosiah): Why is this separate from FormatAnnotation?
|
|
245
|
+
export const PropertyMeta = (name: string, value: PropertyMetaValue) => {
|
|
246
|
+
return <A, I, R>(self: Schema.Schema<A, I, R>): Schema.Schema<A, I, R> => {
|
|
247
|
+
const existingMeta = self.ast.annotations[PropertyMetaAnnotationId] as PropertyMetaAnnotation;
|
|
248
|
+
return self.annotations({
|
|
249
|
+
[PropertyMetaAnnotationId]: {
|
|
250
|
+
...existingMeta,
|
|
251
|
+
[name]: value,
|
|
252
|
+
},
|
|
253
|
+
});
|
|
254
|
+
};
|
|
255
|
+
};
|
|
256
|
+
|
|
257
|
+
export const getPropertyMetaAnnotation = <T>(prop: SchemaAST.PropertySignature, name: string) =>
|
|
258
|
+
Function.pipe(
|
|
259
|
+
SchemaAST.getAnnotation<PropertyMetaAnnotation>(PropertyMetaAnnotationId)(prop.type),
|
|
260
|
+
Option.map((meta) => meta[name] as T),
|
|
261
|
+
Option.getOrElse(() => undefined),
|
|
262
|
+
);
|
|
263
|
+
|
|
264
|
+
/**
|
|
265
|
+
* Schema reference.
|
|
266
|
+
*/
|
|
267
|
+
export const ReferenceAnnotationId = Symbol.for('@dxos/schema/annotation/Reference');
|
|
268
|
+
|
|
269
|
+
export type ReferenceAnnotationValue = TypeAnnotation;
|
|
270
|
+
|
|
271
|
+
export const getReferenceAnnotation = (schema: Schema.Schema.AnyNoContext) =>
|
|
272
|
+
Function.pipe(
|
|
273
|
+
SchemaAST.getAnnotation<ReferenceAnnotationValue>(ReferenceAnnotationId)(schema.ast),
|
|
274
|
+
Option.getOrElse(() => undefined),
|
|
275
|
+
);
|
|
276
|
+
|
|
277
|
+
/**
|
|
278
|
+
* SchemaMeta.
|
|
279
|
+
*/
|
|
280
|
+
export const SchemaMetaSymbol = Symbol.for('@dxos/schema/SchemaMeta');
|
|
281
|
+
export type SchemaMeta = TypeMeta & { id: string };
|
|
282
|
+
|
|
283
|
+
/**
|
|
284
|
+
* Identifies a schema as a schema for a hidden system type.
|
|
285
|
+
*/
|
|
286
|
+
export const SystemTypeAnnotationId = Symbol.for('@dxos/schema/annotation/SystemType');
|
|
287
|
+
export const SystemTypeAnnotation = createAnnotationHelper<boolean>(SystemTypeAnnotationId);
|
|
288
|
+
|
|
289
|
+
/**
|
|
290
|
+
* Identifies label property or JSON path expression.
|
|
291
|
+
* Either a string or an array of strings representing field accessors each matched in priority order.
|
|
292
|
+
*/
|
|
293
|
+
export const LabelAnnotationId = Symbol.for('@dxos/schema/annotation/Label');
|
|
294
|
+
export const LabelAnnotation = createAnnotationHelper<string[]>(LabelAnnotationId);
|
|
295
|
+
|
|
296
|
+
/**
|
|
297
|
+
* Returns the label for a given object based on {@link LabelAnnotationId}.
|
|
298
|
+
*/
|
|
299
|
+
// TODO(burdon): Convert to JsonPath?
|
|
300
|
+
export const getLabel = <S extends Schema.Schema.Any>(schema: S, object: Schema.Schema.Type<S>): string | undefined => {
|
|
301
|
+
const annotation = LabelAnnotation.get(schema).pipe(Option.getOrElse(() => ['name']));
|
|
302
|
+
for (const accessor of annotation) {
|
|
303
|
+
assertArgument(
|
|
304
|
+
typeof accessor === 'string',
|
|
305
|
+
'accessor',
|
|
306
|
+
'Label annotation must be a string or an array of strings',
|
|
307
|
+
);
|
|
308
|
+
const value = getField(object, accessor as JsonPath);
|
|
309
|
+
switch (typeof value) {
|
|
310
|
+
case 'string':
|
|
311
|
+
case 'number':
|
|
312
|
+
case 'boolean':
|
|
313
|
+
case 'bigint':
|
|
314
|
+
case 'symbol':
|
|
315
|
+
return value.toString();
|
|
316
|
+
case 'undefined':
|
|
317
|
+
case 'object':
|
|
318
|
+
case 'function':
|
|
319
|
+
continue;
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
return undefined;
|
|
324
|
+
};
|
|
325
|
+
|
|
326
|
+
/**
|
|
327
|
+
* Sets the label for a given object based on {@link LabelAnnotationId}.
|
|
328
|
+
*/
|
|
329
|
+
export const setLabel = <S extends Schema.Schema.Any>(schema: S, object: Schema.Schema.Type<S>, label: string) => {
|
|
330
|
+
const annotation = LabelAnnotation.get(schema).pipe(
|
|
331
|
+
Option.map((field) => field[0]),
|
|
332
|
+
Option.getOrElse(() => 'name'),
|
|
333
|
+
);
|
|
334
|
+
object[annotation] = label;
|
|
335
|
+
};
|
|
336
|
+
|
|
337
|
+
/**
|
|
338
|
+
* Identifies description property or JSON path expression.
|
|
339
|
+
* A string representing field accessor.
|
|
340
|
+
*/
|
|
341
|
+
export const DescriptionAnnotationId = Symbol.for('@dxos/schema/annotation/Description');
|
|
342
|
+
export const DescriptionAnnotation = createAnnotationHelper<string>(DescriptionAnnotationId);
|
|
343
|
+
|
|
344
|
+
/**
|
|
345
|
+
* Returns the label for a given object based on {@link LabelAnnotationId}.
|
|
346
|
+
*/
|
|
347
|
+
// TODO(burdon): Convert to JsonPath?
|
|
348
|
+
export const getDescription = <S extends Schema.Schema.Any>(
|
|
349
|
+
schema: S,
|
|
350
|
+
object: Schema.Schema.Type<S>,
|
|
351
|
+
): string | undefined => {
|
|
352
|
+
const accessor = DescriptionAnnotation.get(schema).pipe(Option.getOrElse(() => 'description'));
|
|
353
|
+
assertArgument(typeof accessor === 'string', 'accessor', 'Description annotation must be a string');
|
|
354
|
+
const value = getField(object, accessor as JsonPath);
|
|
355
|
+
switch (typeof value) {
|
|
356
|
+
case 'string':
|
|
357
|
+
case 'number':
|
|
358
|
+
case 'boolean':
|
|
359
|
+
case 'bigint':
|
|
360
|
+
case 'symbol':
|
|
361
|
+
return value.toString();
|
|
362
|
+
case 'undefined':
|
|
363
|
+
case 'object':
|
|
364
|
+
case 'function':
|
|
365
|
+
default:
|
|
366
|
+
return undefined;
|
|
367
|
+
}
|
|
368
|
+
};
|
|
369
|
+
|
|
370
|
+
/**
|
|
371
|
+
* Sets the description for a given object based on {@link DescriptionAnnotationId}.
|
|
372
|
+
*/
|
|
373
|
+
export const setDescription = <S extends Schema.Schema.Any>(
|
|
374
|
+
schema: S,
|
|
375
|
+
object: Schema.Schema.Type<S>,
|
|
376
|
+
description: string,
|
|
377
|
+
) => {
|
|
378
|
+
const accessor = DescriptionAnnotation.get(schema).pipe(Option.getOrElse(() => 'description'));
|
|
379
|
+
object[accessor] = description;
|
|
380
|
+
};
|
|
381
|
+
|
|
382
|
+
/**
|
|
383
|
+
* Identifies if a property should be included in a form or not.
|
|
384
|
+
* By default, all properties are included in forms, so this is opt-out.
|
|
385
|
+
*/
|
|
386
|
+
// TODO(burdon): UI concern.
|
|
387
|
+
export const FormInputAnnotationId = Symbol.for('@dxos/schema/annotation/FormInput');
|
|
388
|
+
export const FormInputAnnotation = createAnnotationHelper<boolean>(FormInputAnnotationId);
|
|
389
|
+
|
|
390
|
+
/**
|
|
391
|
+
* Default field to be used on referenced schema to lookup the value.
|
|
392
|
+
*/
|
|
393
|
+
export const FieldLookupAnnotationId = Symbol.for('@dxos/schema/annotation/FieldLookup');
|
|
394
|
+
|
|
395
|
+
/**
|
|
396
|
+
* Generate test data.
|
|
397
|
+
*/
|
|
398
|
+
export const GeneratorAnnotationId = Symbol.for('@dxos/schema/annotation/Generator');
|
|
399
|
+
|
|
400
|
+
export type GeneratorAnnotationValue =
|
|
401
|
+
| string
|
|
402
|
+
| {
|
|
403
|
+
generator: string;
|
|
404
|
+
args?: any[];
|
|
405
|
+
probability?: number;
|
|
406
|
+
};
|
|
407
|
+
|
|
408
|
+
export const GeneratorAnnotation = createAnnotationHelper<GeneratorAnnotationValue>(GeneratorAnnotationId);
|
|
409
|
+
|
|
410
|
+
/**
|
|
411
|
+
* @returns DXN of the schema.
|
|
412
|
+
*
|
|
413
|
+
* For non-stored schema returns `dxn:type:`.
|
|
414
|
+
* For stored schema returns `dxn:echo:`.
|
|
415
|
+
*/
|
|
416
|
+
export const getSchemaDXN = (schema: Schema.Schema.All): DXN | undefined => {
|
|
417
|
+
assertArgument(Schema.isSchema(schema), 'schema', 'invalid schema');
|
|
418
|
+
|
|
419
|
+
const id = getTypeIdentifierAnnotation(schema);
|
|
420
|
+
if (id) {
|
|
421
|
+
return DXN.parse(id);
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
// TODO(dmaretskyi): Add support for dynamic schema.
|
|
425
|
+
const objectAnnotation = getTypeAnnotation(schema);
|
|
426
|
+
if (!objectAnnotation) {
|
|
427
|
+
return undefined;
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
return DXN.fromTypenameAndVersion(objectAnnotation.typename, objectAnnotation.version);
|
|
431
|
+
};
|
|
432
|
+
|
|
433
|
+
/**
|
|
434
|
+
* Returns a reference that will be used to point to a schema.
|
|
435
|
+
* @deprecated Use {@link getSchemaDXN} instead.
|
|
436
|
+
*/
|
|
437
|
+
export const getTypeReference = (schema: Schema.Schema.All | undefined): Reference | undefined => {
|
|
438
|
+
if (!schema) {
|
|
439
|
+
return undefined;
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
const schemaDXN = getSchemaDXN(schema);
|
|
443
|
+
if (!schemaDXN) {
|
|
444
|
+
return undefined;
|
|
445
|
+
}
|
|
446
|
+
return Reference.fromDXN(schemaDXN);
|
|
447
|
+
};
|
|
448
|
+
|
|
449
|
+
/**
|
|
450
|
+
* Returns a reference that will be used to point to a schema.
|
|
451
|
+
* @throws If it is not possible to reference this schema.
|
|
452
|
+
*
|
|
453
|
+
* @deprecated Use {@link getSchemaDXN} instead.
|
|
454
|
+
*/
|
|
455
|
+
export const requireTypeReference = (schema: Schema.Schema.AnyNoContext): Reference => {
|
|
456
|
+
const typeReference = getTypeReference(schema);
|
|
457
|
+
if (typeReference == null) {
|
|
458
|
+
// TODO(burdon): Catalog user-facing errors (this is too verbose).
|
|
459
|
+
throw new Error('Schema must be defined via TypedObject.');
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
return typeReference;
|
|
463
|
+
};
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import type * as Option from 'effect/Option';
|
|
6
|
+
import type * as Schema from 'effect/Schema';
|
|
7
|
+
import * as SchemaAST from 'effect/SchemaAST';
|
|
8
|
+
|
|
9
|
+
import { assertArgument } from '@dxos/invariant';
|
|
10
|
+
import { DXN } from '@dxos/keys';
|
|
11
|
+
|
|
12
|
+
import { EntityKind } from '../types';
|
|
13
|
+
|
|
14
|
+
export interface AnnotationHelper<T> {
|
|
15
|
+
get: (schema: Schema.Schema.Any) => Option.Option<T>;
|
|
16
|
+
set: (value: T) => <S extends Schema.Schema.Any>(schema: S) => S;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export const createAnnotationHelper = <T>(id: symbol): AnnotationHelper<T> => {
|
|
20
|
+
return {
|
|
21
|
+
get: (schema) => SchemaAST.getAnnotation(schema.ast, id),
|
|
22
|
+
set:
|
|
23
|
+
(value) =>
|
|
24
|
+
<S extends Schema.Schema.Any>(schema: S) =>
|
|
25
|
+
schema.annotations({ [id]: value }) as S,
|
|
26
|
+
};
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* If property is optional returns the nested property, otherwise returns the property.
|
|
31
|
+
*/
|
|
32
|
+
// TODO(wittjosiah): Is there a way to do this as a generic?
|
|
33
|
+
export const unwrapOptional = (property: SchemaAST.PropertySignature) => {
|
|
34
|
+
if (!property.isOptional || !SchemaAST.isUnion(property.type)) {
|
|
35
|
+
return property;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
return property.type.types[0];
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* @see JSONSchemaAnnotationId
|
|
43
|
+
* @returns JSON-schema annotation so that the schema can be serialized with correct parameters.
|
|
44
|
+
*/
|
|
45
|
+
// TODO(burdon): Required type.
|
|
46
|
+
export const makeTypeJsonSchemaAnnotation = (options: {
|
|
47
|
+
identifier?: string;
|
|
48
|
+
kind: EntityKind;
|
|
49
|
+
typename: string;
|
|
50
|
+
version: string;
|
|
51
|
+
relationSource?: string;
|
|
52
|
+
relationTarget?: string;
|
|
53
|
+
}) => {
|
|
54
|
+
assertArgument(!!options.relationSource === (options.kind === EntityKind.Relation), 'relationSource');
|
|
55
|
+
assertArgument(!!options.relationTarget === (options.kind === EntityKind.Relation), 'relationTarget');
|
|
56
|
+
|
|
57
|
+
const obj = {
|
|
58
|
+
// TODO(dmaretskyi): Should this include the version?
|
|
59
|
+
$id: options.identifier ?? DXN.fromTypename(options.typename).toString(),
|
|
60
|
+
entityKind: options.kind,
|
|
61
|
+
version: options.version,
|
|
62
|
+
typename: options.typename,
|
|
63
|
+
} as any;
|
|
64
|
+
if (options.kind === EntityKind.Relation) {
|
|
65
|
+
obj.relationSource = { $ref: options.relationSource };
|
|
66
|
+
obj.relationTarget = { $ref: options.relationTarget };
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
return obj;
|
|
70
|
+
};
|