@fougere/schema 0.3.0-alpha.0 → 0.5.0-alpha.0
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/README.md +1 -1
- package/dist/EntityAdapters.d.ts +28 -0
- package/dist/EntityAdapters.d.ts.map +1 -0
- package/dist/EntityAdapters.js +75 -0
- package/dist/EntityAdapters.js.map +1 -0
- package/dist/EntityDeclarations.d.ts +7 -14
- package/dist/EntityDeclarations.d.ts.map +1 -1
- package/dist/EntityDeclarations.js +1 -18
- package/dist/EntityDeclarations.js.map +1 -1
- package/dist/axis/Axis.d.ts +2 -0
- package/dist/axis/Axis.d.ts.map +1 -1
- package/dist/axis/Axis.js +5 -3
- package/dist/axis/Axis.js.map +1 -1
- package/dist/axis/boundary/Boundary.d.ts +1 -1
- package/dist/axis/boundary/Boundary.d.ts.map +1 -1
- package/dist/axis/boundary/{axis.d.ts → BoundaryAxis.d.ts} +1 -1
- package/dist/axis/boundary/BoundaryAxis.d.ts.map +1 -0
- package/dist/axis/boundary/{axis.js → BoundaryAxis.js} +3 -2
- package/dist/axis/boundary/BoundaryAxis.js.map +1 -0
- package/dist/axis/lifecycle/Clock.d.ts +18 -0
- package/dist/axis/lifecycle/Clock.d.ts.map +1 -0
- package/dist/axis/lifecycle/Clock.js +25 -0
- package/dist/axis/lifecycle/Clock.js.map +1 -0
- package/dist/axis/lifecycle/Generators.d.ts +16 -2
- package/dist/axis/lifecycle/Generators.d.ts.map +1 -1
- package/dist/axis/lifecycle/Generators.js +35 -5
- package/dist/axis/lifecycle/Generators.js.map +1 -1
- package/dist/axis/lifecycle/{axis.d.ts → LifecycleAxis.d.ts} +1 -1
- package/dist/axis/lifecycle/LifecycleAxis.d.ts.map +1 -0
- package/dist/axis/lifecycle/{axis.js → LifecycleAxis.js} +3 -2
- package/dist/axis/lifecycle/LifecycleAxis.js.map +1 -0
- package/dist/axis/lifecycle/apply.d.ts +1 -3
- package/dist/axis/lifecycle/apply.d.ts.map +1 -1
- package/dist/axis/lifecycle/apply.js +5 -43
- package/dist/axis/lifecycle/apply.js.map +1 -1
- package/dist/axis/role/Relation.d.ts +11 -1
- package/dist/axis/role/Relation.d.ts.map +1 -1
- package/dist/axis/role/Relation.js +18 -3
- package/dist/axis/role/Relation.js.map +1 -1
- package/dist/axis/role/Role.d.ts +5 -17
- package/dist/axis/role/Role.d.ts.map +1 -1
- package/dist/axis/role/Role.js +9 -19
- package/dist/axis/role/Role.js.map +1 -1
- package/dist/axis/role/{axis.d.ts → RoleAxis.d.ts} +1 -1
- package/dist/axis/role/RoleAxis.d.ts.map +1 -0
- package/dist/axis/role/{axis.js → RoleAxis.js} +5 -4
- package/dist/axis/role/RoleAxis.js.map +1 -0
- package/dist/card/Bundle.d.ts +15 -0
- package/dist/card/Bundle.d.ts.map +1 -0
- package/dist/card/Bundle.js +67 -0
- package/dist/card/Bundle.js.map +1 -0
- package/dist/card/Card.d.ts +27 -0
- package/dist/card/Card.d.ts.map +1 -0
- package/dist/card/Card.js +230 -0
- package/dist/card/Card.js.map +1 -0
- package/dist/card/EntityTypeSource.d.ts +12 -0
- package/dist/card/EntityTypeSource.d.ts.map +1 -0
- package/dist/card/{typescript.js → EntityTypeSource.js} +20 -35
- package/dist/card/EntityTypeSource.js.map +1 -0
- package/dist/card/FacadeTypeSource.d.ts +19 -0
- package/dist/card/FacadeTypeSource.d.ts.map +1 -0
- package/dist/card/FacadeTypeSource.js +40 -0
- package/dist/card/FacadeTypeSource.js.map +1 -0
- package/dist/{judge/form.d.ts → card/admission.d.ts} +2 -4
- package/dist/card/admission.d.ts.map +1 -0
- package/dist/{judge/form.js → card/admission.js} +1 -3
- package/dist/card/admission.js.map +1 -0
- package/dist/card/diff.d.ts +11 -55
- package/dist/card/diff.d.ts.map +1 -1
- package/dist/card/diff.js +1 -122
- package/dist/card/diff.js.map +1 -1
- package/dist/constraint/FieldGroup.d.ts +2 -2
- package/dist/constraint/FieldGroup.d.ts.map +1 -1
- package/dist/constraint/FieldGroup.js.map +1 -1
- package/dist/entity.d.ts +3 -3
- package/dist/entity.d.ts.map +1 -1
- package/dist/entity.js +3 -13
- package/dist/entity.js.map +1 -1
- package/dist/fields/Field.d.ts +24 -0
- package/dist/fields/Field.d.ts.map +1 -0
- package/dist/{Field.js → fields/Field.js} +17 -15
- package/dist/fields/Field.js.map +1 -0
- package/dist/fields/FieldSet.d.ts +28 -0
- package/dist/fields/FieldSet.d.ts.map +1 -0
- package/dist/fields/FieldSet.js +55 -0
- package/dist/fields/FieldSet.js.map +1 -0
- package/dist/index.d.ts +24 -18
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +21 -17
- package/dist/index.js.map +1 -1
- package/dist/judge/{cases.d.ts → Cases.d.ts} +22 -22
- package/dist/judge/Cases.d.ts.map +1 -0
- package/dist/judge/{cases.js → Cases.js} +35 -27
- package/dist/judge/Cases.js.map +1 -0
- package/dist/judge/FieldJudge.d.ts +9 -0
- package/dist/judge/FieldJudge.d.ts.map +1 -0
- package/dist/judge/FieldJudge.js +54 -0
- package/dist/judge/FieldJudge.js.map +1 -0
- package/dist/judge/RowJudge.d.ts +12 -0
- package/dist/judge/RowJudge.d.ts.map +1 -0
- package/dist/judge/RowJudge.js +81 -0
- package/dist/judge/RowJudge.js.map +1 -0
- package/dist/judge/RowRefusal.d.ts +17 -0
- package/dist/judge/RowRefusal.d.ts.map +1 -0
- package/dist/judge/RowRefusal.js +16 -0
- package/dist/judge/RowRefusal.js.map +1 -0
- package/dist/judge/ValueForm.d.ts +3 -0
- package/dist/judge/ValueForm.d.ts.map +1 -0
- package/dist/judge/ValueForm.js +3 -0
- package/dist/judge/ValueForm.js.map +1 -0
- package/dist/judge/ValueJudge.d.ts +12 -0
- package/dist/judge/ValueJudge.d.ts.map +1 -0
- package/dist/judge/ValueJudge.js +60 -0
- package/dist/judge/ValueJudge.js.map +1 -0
- package/dist/projection/SchemaOrCard.d.ts +7 -0
- package/dist/projection/SchemaOrCard.d.ts.map +1 -0
- package/dist/projection/{source.js → SchemaOrCard.js} +3 -3
- package/dist/projection/SchemaOrCard.js.map +1 -0
- package/dist/projection/Visibility.d.ts +10 -0
- package/dist/projection/Visibility.d.ts.map +1 -0
- package/dist/projection/Visibility.js +55 -0
- package/dist/projection/Visibility.js.map +1 -0
- package/dist/projection/standard.d.ts +11 -32
- package/dist/projection/standard.d.ts.map +1 -1
- package/dist/schema/Schema.d.ts +80 -0
- package/dist/schema/Schema.d.ts.map +1 -0
- package/dist/{Schema.js → schema/Schema.js} +46 -48
- package/dist/schema/Schema.js.map +1 -0
- package/dist/schema/SchemaDefinition.d.ts +60 -0
- package/dist/schema/SchemaDefinition.d.ts.map +1 -0
- package/dist/schema/SchemaDefinition.js +85 -0
- package/dist/schema/SchemaDefinition.js.map +1 -0
- package/dist/schema/SchemaDerivation.d.ts +34 -0
- package/dist/schema/SchemaDerivation.d.ts.map +1 -0
- package/dist/schema/SchemaDerivation.js +46 -0
- package/dist/schema/SchemaDerivation.js.map +1 -0
- package/dist/schema/SchemaView.d.ts +25 -0
- package/dist/schema/SchemaView.d.ts.map +1 -0
- package/dist/{SchemaView.js.map → schema/SchemaView.js.map} +1 -1
- package/dist/utils.d.ts +4 -0
- package/dist/utils.d.ts.map +1 -0
- package/dist/utils.js +13 -0
- package/dist/utils.js.map +1 -0
- package/dist/vocabulary/bool.d.ts +1 -1
- package/dist/vocabulary/bool.d.ts.map +1 -1
- package/dist/vocabulary/bool.js +1 -1
- package/dist/vocabulary/bool.js.map +1 -1
- package/dist/vocabulary/created.d.ts +1 -1
- package/dist/vocabulary/created.d.ts.map +1 -1
- package/dist/vocabulary/date.d.ts +1 -1
- package/dist/vocabulary/date.d.ts.map +1 -1
- package/dist/vocabulary/date.js +1 -1
- package/dist/vocabulary/date.js.map +1 -1
- package/dist/vocabulary/email.d.ts +1 -1
- package/dist/vocabulary/email.d.ts.map +1 -1
- package/dist/vocabulary/immutable.d.ts +1 -1
- package/dist/vocabulary/immutable.d.ts.map +1 -1
- package/dist/vocabulary/indexed.d.ts +1 -1
- package/dist/vocabulary/indexed.d.ts.map +1 -1
- package/dist/vocabulary/json.d.ts +2 -2
- package/dist/vocabulary/json.d.ts.map +1 -1
- package/dist/vocabulary/json.js +6 -4
- package/dist/vocabulary/json.js.map +1 -1
- package/dist/vocabulary/list.d.ts +1 -1
- package/dist/vocabulary/list.d.ts.map +1 -1
- package/dist/vocabulary/list.js +1 -1
- package/dist/vocabulary/list.js.map +1 -1
- package/dist/vocabulary/many.d.ts +1 -1
- package/dist/vocabulary/many.d.ts.map +1 -1
- package/dist/vocabulary/many.js +3 -3
- package/dist/vocabulary/many.js.map +1 -1
- package/dist/vocabulary/nullable.d.ts +1 -1
- package/dist/vocabulary/nullable.d.ts.map +1 -1
- package/dist/vocabulary/number.d.ts +1 -1
- package/dist/vocabulary/number.d.ts.map +1 -1
- package/dist/vocabulary/number.js +1 -1
- package/dist/vocabulary/number.js.map +1 -1
- package/dist/vocabulary/oneOf.d.ts +1 -1
- package/dist/vocabulary/oneOf.d.ts.map +1 -1
- package/dist/vocabulary/oneOf.js +1 -1
- package/dist/vocabulary/oneOf.js.map +1 -1
- package/dist/vocabulary/optional.d.ts +1 -1
- package/dist/vocabulary/optional.d.ts.map +1 -1
- package/dist/vocabulary/primary.d.ts +1 -1
- package/dist/vocabulary/primary.d.ts.map +1 -1
- package/dist/vocabulary/primary.js +4 -5
- package/dist/vocabulary/primary.js.map +1 -1
- package/dist/vocabulary/readOnly.d.ts +1 -1
- package/dist/vocabulary/readOnly.d.ts.map +1 -1
- package/dist/vocabulary/ref.d.ts +1 -1
- package/dist/vocabulary/ref.d.ts.map +1 -1
- package/dist/vocabulary/ref.js +3 -9
- package/dist/vocabulary/ref.js.map +1 -1
- package/dist/vocabulary/text.d.ts +1 -1
- package/dist/vocabulary/text.d.ts.map +1 -1
- package/dist/vocabulary/text.js +1 -1
- package/dist/vocabulary/text.js.map +1 -1
- package/dist/vocabulary/unique.d.ts +1 -1
- package/dist/vocabulary/unique.d.ts.map +1 -1
- package/dist/vocabulary/updated.d.ts +1 -1
- package/dist/vocabulary/updated.d.ts.map +1 -1
- package/dist/vocabulary/url.d.ts +1 -1
- package/dist/vocabulary/url.d.ts.map +1 -1
- package/dist/vocabulary/vocabulary.d.ts +2 -3
- package/dist/vocabulary/vocabulary.d.ts.map +1 -1
- package/dist/vocabulary/vocabulary.js +4 -4
- package/dist/vocabulary/vocabulary.js.map +1 -1
- package/dist/vocabulary/writeOnly.d.ts +1 -1
- package/dist/vocabulary/writeOnly.d.ts.map +1 -1
- package/package.json +9 -4
- package/src/EntityAdapters.ts +100 -0
- package/src/EntityDeclarations.ts +19 -0
- package/src/axis/Axis.ts +25 -0
- package/src/axis/Meta.ts +3 -0
- package/src/axis/boundary/Boundaries.ts +44 -0
- package/src/axis/boundary/Boundary.ts +69 -0
- package/src/axis/boundary/BoundaryAxis.ts +32 -0
- package/src/axis/lifecycle/Clock.ts +26 -0
- package/src/axis/lifecycle/Generators.ts +46 -0
- package/src/axis/lifecycle/Lifecycle.ts +53 -0
- package/src/axis/lifecycle/LifecycleAxis.ts +49 -0
- package/src/axis/lifecycle/apply.ts +38 -0
- package/src/axis/role/Relation.ts +31 -0
- package/src/axis/role/Role.ts +56 -0
- package/src/axis/role/RoleAxis.ts +115 -0
- package/src/axis/shape/Formats.ts +22 -0
- package/src/axis/shape/Shape.ts +91 -0
- package/src/card/Bundle.ts +80 -0
- package/src/card/Card.ts +256 -0
- package/src/card/Descriptor.ts +68 -0
- package/src/card/EntityTypeSource.ts +90 -0
- package/src/card/FacadeTypeSource.ts +55 -0
- package/src/card/admission.ts +25 -0
- package/src/card/diff.ts +56 -0
- package/src/constraint/FieldGroup.ts +91 -0
- package/src/constraint/Unique.ts +3 -0
- package/src/entity.ts +10 -0
- package/src/fields/Field.ts +80 -0
- package/src/fields/FieldSet.ts +62 -0
- package/src/index.ts +69 -0
- package/src/judge/Cases.ts +139 -0
- package/src/judge/FieldJudge.ts +61 -0
- package/src/judge/RowJudge.ts +84 -0
- package/src/judge/RowRefusal.ts +17 -0
- package/src/judge/ValueForm.ts +7 -0
- package/src/judge/ValueJudge.ts +72 -0
- package/src/judge/options.ts +3 -0
- package/src/judge/result.ts +10 -0
- package/src/projection/SchemaOrCard.ts +18 -0
- package/src/projection/Visibility.ts +47 -0
- package/src/projection/standard.ts +11 -0
- package/src/schema/Schema.ts +197 -0
- package/src/schema/SchemaDefinition.ts +120 -0
- package/src/schema/SchemaDerivation.ts +57 -0
- package/src/schema/SchemaView.ts +27 -0
- package/src/utils.ts +13 -0
- package/src/vocabulary/bool.ts +9 -0
- package/src/vocabulary/created.ts +8 -0
- package/src/vocabulary/date.ts +5 -0
- package/src/vocabulary/email.ts +6 -0
- package/src/vocabulary/immutable.ts +6 -0
- package/src/vocabulary/indexed.ts +4 -0
- package/src/vocabulary/json.ts +22 -0
- package/src/vocabulary/list.ts +13 -0
- package/src/vocabulary/many.ts +9 -0
- package/src/vocabulary/nullable.ts +7 -0
- package/src/vocabulary/number.ts +17 -0
- package/src/vocabulary/oneOf.ts +24 -0
- package/src/vocabulary/optional.ts +8 -0
- package/src/vocabulary/primary.ts +33 -0
- package/src/vocabulary/readOnly.ts +7 -0
- package/src/vocabulary/ref.ts +13 -0
- package/src/vocabulary/text.ts +19 -0
- package/src/vocabulary/unique.ts +10 -0
- package/src/vocabulary/updated.ts +7 -0
- package/src/vocabulary/url.ts +6 -0
- package/src/vocabulary/vocabulary.ts +40 -0
- package/src/vocabulary/writeOnly.ts +7 -0
- package/dist/Field.d.ts +0 -21
- package/dist/Field.d.ts.map +0 -1
- package/dist/Field.js.map +0 -1
- package/dist/Schema.d.ts +0 -67
- package/dist/Schema.d.ts.map +0 -1
- package/dist/Schema.js.map +0 -1
- package/dist/SchemaView.d.ts +0 -18
- package/dist/SchemaView.d.ts.map +0 -1
- package/dist/axis/boundary/axis.d.ts.map +0 -1
- package/dist/axis/boundary/axis.js.map +0 -1
- package/dist/axis/lifecycle/axis.d.ts.map +0 -1
- package/dist/axis/lifecycle/axis.js.map +0 -1
- package/dist/axis/role/axis.d.ts.map +0 -1
- package/dist/axis/role/axis.js.map +0 -1
- package/dist/card/describe.d.ts +0 -12
- package/dist/card/describe.d.ts.map +0 -1
- package/dist/card/describe.js +0 -83
- package/dist/card/describe.js.map +0 -1
- package/dist/card/reconstruct.d.ts +0 -11
- package/dist/card/reconstruct.d.ts.map +0 -1
- package/dist/card/reconstruct.js +0 -83
- package/dist/card/reconstruct.js.map +0 -1
- package/dist/card/typescript.d.ts +0 -17
- package/dist/card/typescript.d.ts.map +0 -1
- package/dist/card/typescript.js.map +0 -1
- package/dist/clean.d.ts +0 -2
- package/dist/clean.d.ts.map +0 -1
- package/dist/clean.js +0 -7
- package/dist/clean.js.map +0 -1
- package/dist/judge/Judge.d.ts +0 -14
- package/dist/judge/Judge.d.ts.map +0 -1
- package/dist/judge/Judge.js +0 -161
- package/dist/judge/Judge.js.map +0 -1
- package/dist/judge/cases.d.ts.map +0 -1
- package/dist/judge/cases.js.map +0 -1
- package/dist/judge/form.d.ts.map +0 -1
- package/dist/judge/form.js.map +0 -1
- package/dist/name.d.ts +0 -3
- package/dist/name.d.ts.map +0 -1
- package/dist/name.js +0 -7
- package/dist/name.js.map +0 -1
- package/dist/projection/encode.d.ts +0 -3
- package/dist/projection/encode.d.ts.map +0 -1
- package/dist/projection/encode.js +0 -19
- package/dist/projection/encode.js.map +0 -1
- package/dist/projection/io.d.ts +0 -4
- package/dist/projection/io.d.ts.map +0 -1
- package/dist/projection/io.js +0 -29
- package/dist/projection/io.js.map +0 -1
- package/dist/projection/source.d.ts +0 -7
- package/dist/projection/source.d.ts.map +0 -1
- package/dist/projection/source.js.map +0 -1
- package/dist/same.d.ts +0 -6
- package/dist/same.d.ts.map +0 -1
- package/dist/same.js +0 -35
- package/dist/same.js.map +0 -1
- /package/dist/{SchemaView.js → schema/SchemaView.js} +0 -0
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { Boundary } from '../axis/boundary/Boundary.js';
|
|
2
|
+
import { Lifecycle } from '../axis/lifecycle/Lifecycle.js';
|
|
3
|
+
import { Role } from '../axis/role/Role.js';
|
|
4
|
+
import type { Fields } from '../fields/Field.js';
|
|
5
|
+
|
|
6
|
+
export class Visibility {
|
|
7
|
+
private constructor(private readonly fields: Fields) {}
|
|
8
|
+
|
|
9
|
+
static of(fields: Fields): Visibility {
|
|
10
|
+
return new Visibility(fields);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
get input(): Fields {
|
|
14
|
+
const result: Fields = {};
|
|
15
|
+
for (const [name, field] of Object.entries(this.fields)) {
|
|
16
|
+
const role = Role.of(field);
|
|
17
|
+
if (role.isPrimary) continue;
|
|
18
|
+
if (Lifecycle.of(field).create === 'now') continue;
|
|
19
|
+
if (role.isCollection) continue;
|
|
20
|
+
if (Boundary.of(field).readOnly) continue;
|
|
21
|
+
result[name] = field;
|
|
22
|
+
}
|
|
23
|
+
return result;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
get output(): Fields {
|
|
27
|
+
const result: Fields = {};
|
|
28
|
+
for (const [name, field] of Object.entries(this.fields)) {
|
|
29
|
+
if (Boundary.of(field).writeOnly) continue;
|
|
30
|
+
result[name] = field;
|
|
31
|
+
}
|
|
32
|
+
return result;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
encode(record: Record<string, unknown>): Record<string, unknown> {
|
|
36
|
+
const out: Record<string, unknown> = { ...record };
|
|
37
|
+
for (const [key, field] of Object.entries(this.fields)) {
|
|
38
|
+
if (!(key in record)) continue;
|
|
39
|
+
const boundary = Boundary.of(field);
|
|
40
|
+
if (boundary.writeOnly) { delete out[key]; continue; }
|
|
41
|
+
const value = record[key];
|
|
42
|
+
if (value === null || value === undefined) continue;
|
|
43
|
+
out[key] = boundary.encode(value);
|
|
44
|
+
}
|
|
45
|
+
return out;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The Standard Schema interface, from the package that owns it.
|
|
3
|
+
*
|
|
4
|
+
* This file used to CARRY the spec — 47 lines of hand-copied interface — and the copy had
|
|
5
|
+
* silently fallen a version behind: v1.1 gave `Props` a base it extends (`StandardTypedV1`)
|
|
6
|
+
* and gave `validate` a second `options` parameter. Nothing broke, because a realization may
|
|
7
|
+
* ignore an optional argument, which is exactly why nobody noticed. A standard is owned by
|
|
8
|
+
* whoever publishes it; the package is types only (`index.js` is zero bytes), so importing
|
|
9
|
+
* it costs a browser nothing.
|
|
10
|
+
*/
|
|
11
|
+
export type { StandardSchemaV1 } from '@standard-schema/spec';
|
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
import { Boundary } from '../axis/boundary/Boundary.js';
|
|
2
|
+
import { type Fields } from '../fields/Field.js';
|
|
3
|
+
import {
|
|
4
|
+
type CompositeUnique,
|
|
5
|
+
type EntityDeclarations,
|
|
6
|
+
type PreviousNames,
|
|
7
|
+
} from '../EntityDeclarations.js';
|
|
8
|
+
import { EntityAdapterSet, type EntityAdapters } from '../EntityAdapters.js';
|
|
9
|
+
import { FieldGroup } from '../constraint/FieldGroup.js';
|
|
10
|
+
import { Unique } from '../constraint/Unique.js';
|
|
11
|
+
import { RowJudge } from '../judge/RowJudge.js';
|
|
12
|
+
import { SchemaDerivation } from './SchemaDerivation.js';
|
|
13
|
+
import { SchemaDefinition } from './SchemaDefinition.js';
|
|
14
|
+
import { type ValidateOptions } from '../judge/options.js';
|
|
15
|
+
import type { StandardSchemaV1 } from '../projection/standard.js';
|
|
16
|
+
import type { PartialRow, Row, SchemaView } from './SchemaView.js';
|
|
17
|
+
|
|
18
|
+
export const ANONYMOUS_SCHEMA_NAME = "Schema";
|
|
19
|
+
|
|
20
|
+
export interface SchemaConstructor<TFields extends Fields> extends SchemaView<TFields> {
|
|
21
|
+
new (data: PartialRow<TFields>): Row<TFields>;
|
|
22
|
+
readonly "~standard": StandardSchemaV1.Props<Record<string, unknown>, Row<TFields>>;
|
|
23
|
+
readonly derivation?: SchemaDerivation;
|
|
24
|
+
readonly previous?: PreviousNames<TFields>;
|
|
25
|
+
readonly anchored?: boolean;
|
|
26
|
+
from(data: Record<string, unknown>): Row<TFields>;
|
|
27
|
+
pick<K extends string & keyof TFields>(...keys: K[]): SchemaConstructor<Pick<TFields, K>>;
|
|
28
|
+
omit<K extends string & keyof TFields>(...keys: K[]): SchemaConstructor<Omit<TFields, K>>;
|
|
29
|
+
partial(): SchemaConstructor<TFields>;
|
|
30
|
+
extend<E extends Fields>(extra: E): SchemaConstructor<TFields & E>;
|
|
31
|
+
declares(declarations: EntityDeclarations<TFields>): SchemaConstructor<TFields>;
|
|
32
|
+
anchor(): SchemaConstructor<TFields>;
|
|
33
|
+
named(name: string): SchemaConstructor<TFields>;
|
|
34
|
+
rename(mapping: Partial<Record<string & keyof TFields, string>>): SchemaConstructor<Fields>;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export class Schema {
|
|
38
|
+
/** The one place a schema holds what it is. The readings below are its projections. */
|
|
39
|
+
static definition: SchemaDefinition = SchemaDefinition.stated({ fields: {} });
|
|
40
|
+
|
|
41
|
+
static get fields(): Fields { return this.definition.fields; }
|
|
42
|
+
static get adapters(): EntityAdapters<Fields> | undefined { return this.definition.adapters; }
|
|
43
|
+
static get opts(): ValidateOptions { return this.definition.opts; }
|
|
44
|
+
static get derivation(): SchemaDerivation | undefined { return this.definition.derivation; }
|
|
45
|
+
static get previous(): PreviousNames<Fields> | undefined { return this.definition.previous; }
|
|
46
|
+
static get anchored(): boolean { return this.definition.anchored; }
|
|
47
|
+
|
|
48
|
+
constructor(data?: Record<string, unknown>) {
|
|
49
|
+
if (!data) return;
|
|
50
|
+
for (const [key, value] of Object.entries(data)) {
|
|
51
|
+
Object.defineProperty(this, key, { value, writable: true, enumerable: true, configurable: true });
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
static getFields() { return this.fields; }
|
|
56
|
+
static getAdapters() { return this.adapters; }
|
|
57
|
+
static getUnique(): CompositeUnique<Fields> | undefined {
|
|
58
|
+
const groups = FieldGroup.groupsOf(this.fields, Unique);
|
|
59
|
+
return groups.length ? groups.map((group) => [...group.members]) : undefined;
|
|
60
|
+
}
|
|
61
|
+
static getOpts() { return this.opts; }
|
|
62
|
+
|
|
63
|
+
static validate(input: unknown) {
|
|
64
|
+
return RowJudge.of(this.fields, this.opts).check(input);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
static from(data: Record<string, unknown>) {
|
|
68
|
+
const out: Record<string, unknown> = {};
|
|
69
|
+
for (const [key, field] of Object.entries(this.fields)) {
|
|
70
|
+
if (!(key in data)) continue;
|
|
71
|
+
const value = data[key];
|
|
72
|
+
if (value === null || value === undefined) { out[key] = value; continue; }
|
|
73
|
+
const decoded = Boundary.of(field).decode(value);
|
|
74
|
+
out[key] = "error" in decoded ? value : decoded.value;
|
|
75
|
+
}
|
|
76
|
+
return out;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
static get ["~standard"](): StandardSchemaV1.Props<Record<string, unknown>> {
|
|
80
|
+
const { fields, opts } = this;
|
|
81
|
+
return {
|
|
82
|
+
version: 1,
|
|
83
|
+
vendor: "fougere",
|
|
84
|
+
validate(value: unknown) {
|
|
85
|
+
const result = RowJudge.of(fields, opts).check(value);
|
|
86
|
+
if (result.success) return { value: result.data };
|
|
87
|
+
return {
|
|
88
|
+
issues: result.errors.map((e) => ({
|
|
89
|
+
message: e.message,
|
|
90
|
+
path: e.path && e.path !== "." ? [{ key: e.path }] : undefined,
|
|
91
|
+
})),
|
|
92
|
+
};
|
|
93
|
+
},
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
static pick(...keys: string[]) {
|
|
98
|
+
assertKnownKeys("pick", keys, this.fields);
|
|
99
|
+
const picked: Fields = {};
|
|
100
|
+
for (const key of keys) if (this.fields[key]) picked[key] = this.fields[key];
|
|
101
|
+
return this.derive(picked, (k) => (keys.includes(k) ? k : undefined));
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
static omit(...keys: string[]) {
|
|
105
|
+
assertKnownKeys("omit", keys, this.fields);
|
|
106
|
+
const kept: Fields = {};
|
|
107
|
+
for (const [key, field] of Object.entries(this.fields)) if (!keys.includes(key)) kept[key] = field;
|
|
108
|
+
return this.derive(kept, (k) => (keys.includes(k) ? undefined : k));
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
static rename(mapping: Record<string, string>) {
|
|
112
|
+
assertKnownKeys("rename", Object.keys(mapping), this.fields);
|
|
113
|
+
const renamed: Fields = {};
|
|
114
|
+
for (const [key, field] of Object.entries(this.fields)) renamed[mapping[key] ?? key] = field;
|
|
115
|
+
return this.derive(renamed, (k) => mapping[k] ?? k);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Everything a schema states about ITSELF, available wherever a schema is. `entity(fields, …)`
|
|
120
|
+
* is this same gesture at the declaration site; the fields do not move, so neither does the type.
|
|
121
|
+
*/
|
|
122
|
+
static declares(declarations: EntityDeclarations<Fields>) {
|
|
123
|
+
const addressed = EntityAdapterSet.of(declarations.adapters)?.fieldNames ?? [];
|
|
124
|
+
assertKnownKeys("declares", [...addressed, ...Object.keys(declarations.previous ?? {})], this.fields);
|
|
125
|
+
|
|
126
|
+
return Schema.subclass(this.definition.declaring(declarations));
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* This schema holds rows of its own — every entity is one, and a derivation says so.
|
|
131
|
+
*
|
|
132
|
+
* An anchor is where a walk STOPS: `derivation.anchor` climbs to the nearest one, and what
|
|
133
|
+
* is cut from an anchor roots on it. Two anchors are two entities that happen to share a
|
|
134
|
+
* shape, never a hierarchy — nothing here subtypes, so a relation between them is `ref()`.
|
|
135
|
+
*/
|
|
136
|
+
static anchor() {
|
|
137
|
+
return Schema.subclass(this.definition.anchoring());
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
static partial() {
|
|
141
|
+
return Schema.subclass(this.definition.patched(this));
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
static extend(extra: Fields) {
|
|
145
|
+
return Schema.subclass(this.definition.extended(extra, this));
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
static named(name: string) {
|
|
149
|
+
if (!/^[A-Za-z_$][A-Za-z0-9_$]*$/.test(name)) {
|
|
150
|
+
throw new Error(`named(): \`${name}\` is not a valid class name.`);
|
|
151
|
+
}
|
|
152
|
+
if (this.name !== ANONYMOUS_SCHEMA_NAME) {
|
|
153
|
+
throw new Error(`named(): \`${this.name}\` is already named by its class declaration.`);
|
|
154
|
+
}
|
|
155
|
+
Object.defineProperty(this, "name", { value: name, configurable: true });
|
|
156
|
+
return this;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
static compose<T extends SchemaView[]>(...sources: T): SchemaConstructor<Merged<T>> {
|
|
160
|
+
return Schema.subclass(SchemaDefinition.merged(sources)) as unknown as SchemaConstructor<Merged<T>>;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
static of<TFields extends Fields>(stated: {
|
|
164
|
+
fields: TFields;
|
|
165
|
+
derivation?: SchemaDerivation;
|
|
166
|
+
adapters?: EntityAdapters<TFields>;
|
|
167
|
+
opts?: ValidateOptions;
|
|
168
|
+
previous?: PreviousNames<TFields>;
|
|
169
|
+
anchored?: boolean;
|
|
170
|
+
}): SchemaConstructor<TFields> {
|
|
171
|
+
return Schema.subclass(SchemaDefinition.stated(stated)) as unknown as SchemaConstructor<TFields>;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/** The class a definition becomes. The one place a Schema subclass is born. */
|
|
175
|
+
private static subclass(definition: SchemaDefinition): SchemaConstructor<Fields> {
|
|
176
|
+
class Derived extends Schema {}
|
|
177
|
+
Derived.definition = definition;
|
|
178
|
+
Object.defineProperty(Derived, "name", { value: ANONYMOUS_SCHEMA_NAME, configurable: true });
|
|
179
|
+
return Derived as unknown as SchemaConstructor<Fields>;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
private static derive(fields: Fields, survives: (key: string) => string | undefined) {
|
|
183
|
+
return Schema.subclass(this.definition.cut(fields, survives, this));
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
function assertKnownKeys(operation: string, keys: string[], fields: Fields): void {
|
|
188
|
+
const strangers = keys.filter((key) => !(key in fields));
|
|
189
|
+
if (strangers.length === 0) return;
|
|
190
|
+
throw new Error(
|
|
191
|
+
`${operation}(): unknown field ${strangers.map((s) => `\`${s}\``).join(", ")}. ` +
|
|
192
|
+
`This schema carries ${Object.keys(fields).join(", ")}.`,
|
|
193
|
+
);
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
type UnionToIntersection<U> = (U extends unknown ? (k: U) => void : never) extends (k: infer I) => void ? I : never;
|
|
197
|
+
type Merged<T extends SchemaView[]> = UnionToIntersection<T[number] extends { getFields(): infer F } ? F : Fields> & Fields;
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
import { type Fields } from '../fields/Field.js';
|
|
2
|
+
import { FieldSet } from '../fields/FieldSet.js';
|
|
3
|
+
import { type EntityDeclarations, type PreviousNames } from '../EntityDeclarations.js';
|
|
4
|
+
import { EntityAdapterSet, type EntityAdapters } from '../EntityAdapters.js';
|
|
5
|
+
import { SchemaDerivation } from './SchemaDerivation.js';
|
|
6
|
+
import { type ValidateOptions } from '../judge/options.js';
|
|
7
|
+
import type { SchemaView } from './SchemaView.js';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Everything a schema IS, apart from being a class whose instances are rows.
|
|
11
|
+
*
|
|
12
|
+
* The members used to be that many statics assigned side by side, and every derivation
|
|
13
|
+
* re-threaded them by hand — which is how one of them came to be dropped from a list
|
|
14
|
+
* without anyone noticing. A transformation here returns a COMPLETE definition or it does
|
|
15
|
+
* not compile, so no gesture can mix the fields of one with the options of another.
|
|
16
|
+
*
|
|
17
|
+
* Internal: it is not exported from the package and never appears on `SchemaConstructor`.
|
|
18
|
+
* A user derives with `Order.pick(…)`, never `Order.definition.pick(…)`.
|
|
19
|
+
*/
|
|
20
|
+
export class SchemaDefinition {
|
|
21
|
+
private constructor(
|
|
22
|
+
readonly fields: Fields,
|
|
23
|
+
readonly adapters: EntityAdapters<Fields> | undefined,
|
|
24
|
+
readonly opts: ValidateOptions,
|
|
25
|
+
readonly previous: PreviousNames<Fields> | undefined,
|
|
26
|
+
readonly derivation: SchemaDerivation | undefined,
|
|
27
|
+
readonly anchored: boolean,
|
|
28
|
+
) {}
|
|
29
|
+
|
|
30
|
+
static stated(said: {
|
|
31
|
+
fields: Fields;
|
|
32
|
+
adapters?: EntityAdapters<Fields>;
|
|
33
|
+
opts?: ValidateOptions;
|
|
34
|
+
previous?: PreviousNames<Fields>;
|
|
35
|
+
derivation?: SchemaDerivation;
|
|
36
|
+
anchored?: boolean;
|
|
37
|
+
}): SchemaDefinition {
|
|
38
|
+
return new SchemaDefinition(
|
|
39
|
+
said.fields, EntityAdapterSet.of(said.adapters)?.stated, said.opts ?? {}, said.previous, said.derivation, said.anchored ?? false,
|
|
40
|
+
);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* What a schema states about ITSELF, folded into what it already states — one adapter
|
|
45
|
+
* entry per field wins the later spelling, and a unique group is stamped on its members.
|
|
46
|
+
*/
|
|
47
|
+
declaring(said: EntityDeclarations<Fields>): SchemaDefinition {
|
|
48
|
+
return new SchemaDefinition(
|
|
49
|
+
FieldSet.declared(this.fields, said.unique),
|
|
50
|
+
EntityAdapterSet.merged([this.adapters, said.adapters])?.stated,
|
|
51
|
+
this.opts,
|
|
52
|
+
said.previous ?? this.previous,
|
|
53
|
+
this.derivation,
|
|
54
|
+
this.anchored,
|
|
55
|
+
);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* The same schema, holding rows of its own. A derivation is an answer until it says this.
|
|
60
|
+
*/
|
|
61
|
+
anchoring(): SchemaDefinition {
|
|
62
|
+
return new SchemaDefinition(
|
|
63
|
+
this.fields, this.adapters, this.opts, this.previous, this.derivation, true,
|
|
64
|
+
);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* `pick`, `omit` and `rename` are one gesture: the fields that remain, and what became
|
|
69
|
+
* of each name. What each adapter was handed follows the same correspondence, and so does the origin.
|
|
70
|
+
*/
|
|
71
|
+
cut(fields: Fields, survives: (key: string) => string | undefined, root: SchemaView): SchemaDefinition {
|
|
72
|
+
const renamed: Fields = {};
|
|
73
|
+
for (const [key, field] of Object.entries(fields)) renamed[key] = field.rename(survives);
|
|
74
|
+
return new SchemaDefinition(
|
|
75
|
+
renamed,
|
|
76
|
+
EntityAdapterSet.of(this.adapters)?.cut(survives)?.stated,
|
|
77
|
+
this.opts,
|
|
78
|
+
undefined,
|
|
79
|
+
this.origin(root).compose(survives),
|
|
80
|
+
false,
|
|
81
|
+
);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/** The same fields, judged as an update. */
|
|
85
|
+
patched(root: SchemaView): SchemaDefinition {
|
|
86
|
+
return new SchemaDefinition(
|
|
87
|
+
{ ...this.fields }, this.adapters, { ...this.opts, patch: true }, undefined, this.origin(root), false,
|
|
88
|
+
);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/** The added fields have no origin, so the derivation is unchanged: it speaks of the root. */
|
|
92
|
+
extended(extra: Fields, root: SchemaView): SchemaDefinition {
|
|
93
|
+
return new SchemaDefinition(
|
|
94
|
+
{ ...this.fields, ...extra }, this.adapters, this.opts, undefined, this.origin(root), false,
|
|
95
|
+
);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/** Several views folded into one definition, which therefore has no single origin. */
|
|
99
|
+
static merged(views: readonly SchemaView[]): SchemaDefinition {
|
|
100
|
+
const fields: Fields = {};
|
|
101
|
+
let opts: ValidateOptions = {};
|
|
102
|
+
for (const view of views) {
|
|
103
|
+
Object.assign(fields, view.getFields());
|
|
104
|
+
opts = { ...opts, ...view.getOpts() };
|
|
105
|
+
}
|
|
106
|
+
const adapters = EntityAdapterSet.merged(views.map((view) => view.getAdapters()))?.stated;
|
|
107
|
+
|
|
108
|
+
return new SchemaDefinition(fields, adapters, opts, undefined, undefined, false);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* The origin this definition already carries, or the one `root` becomes. A schema that
|
|
113
|
+
* is an ANCHOR holds rows of its own, so what is cut from it roots on IT and not on its own origin.
|
|
114
|
+
*/
|
|
115
|
+
private origin(root: SchemaView): SchemaDerivation {
|
|
116
|
+
if (!this.anchored && this.derivation) return this.derivation;
|
|
117
|
+
|
|
118
|
+
return SchemaDerivation.first(root, this.fields);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { type Fields } from '../fields/Field.js';
|
|
2
|
+
import type { SchemaView } from './SchemaView.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* What a derived schema says about its ORIGIN: the class it was cut from, and what the cut
|
|
6
|
+
* left of each of that class's fields.
|
|
7
|
+
*
|
|
8
|
+
* The origin is the ROOT — `Post.pick(a, b).omit(b)` answers `Post` and never the
|
|
9
|
+
* intermediate — so a chain is flattened rather than journalled, and the two halves cannot
|
|
10
|
+
* disagree: a derivation always carries both, which `source` and `survived` as independent
|
|
11
|
+
* properties could not promise.
|
|
12
|
+
*/
|
|
13
|
+
export class SchemaDerivation {
|
|
14
|
+
private constructor(
|
|
15
|
+
readonly source: SchemaView,
|
|
16
|
+
readonly survived: Readonly<Record<string, string | undefined>>,
|
|
17
|
+
) {}
|
|
18
|
+
|
|
19
|
+
/** The first cut: every field of the origin still answers to its own name. */
|
|
20
|
+
static first(source: SchemaView, fields: Fields): SchemaDerivation {
|
|
21
|
+
return new SchemaDerivation(source, Object.fromEntries(Object.keys(fields).map((key) => [key, key])));
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** The same origin, seen through one more cut. */
|
|
25
|
+
compose(survives: (key: string) => string | undefined): SchemaDerivation {
|
|
26
|
+
return new SchemaDerivation(
|
|
27
|
+
this.source,
|
|
28
|
+
Object.fromEntries(
|
|
29
|
+
Object.entries(this.survived).map(([origin, here]) => [origin, here === undefined ? undefined : survives(here)]),
|
|
30
|
+
),
|
|
31
|
+
);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
get sourceName(): string {
|
|
35
|
+
return this.source.name;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* The anchor whose rows this describes — the nearest one going up, and a schema with no
|
|
40
|
+
* origin of its own is one. An answer borrows its rows; this says from whom. Whether
|
|
41
|
+
* those rows carry a key is a separate question: an anchor may have none.
|
|
42
|
+
*/
|
|
43
|
+
get anchor(): SchemaView {
|
|
44
|
+
const source = this.source;
|
|
45
|
+
|
|
46
|
+
return source.anchored !== undefined || !source.derivation ? source : source.derivation.anchor;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* What the ROOT calls `key`, translated to the name it carries here — `undefined` when a
|
|
51
|
+
* cut dropped it. A reader that needs something the root declared about a field asks the
|
|
52
|
+
* root and comes back through this; it does not keep a copy of the answer.
|
|
53
|
+
*/
|
|
54
|
+
hereFor(key: string): string | undefined {
|
|
55
|
+
return this.survived[key];
|
|
56
|
+
}
|
|
57
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { Field, Fields } from '../fields/Field.js';
|
|
2
|
+
import type { CompositeUnique, PreviousNames } from '../EntityDeclarations.js';
|
|
3
|
+
import type { EntityAdapters } from '../EntityAdapters.js';
|
|
4
|
+
import type { ValidationResult } from '../judge/result.js';
|
|
5
|
+
import type { ValidateOptions } from '../judge/options.js';
|
|
6
|
+
import type { SchemaDerivation } from './SchemaDerivation.js';
|
|
7
|
+
|
|
8
|
+
export type Row<TFields extends Fields> = {
|
|
9
|
+
[K in keyof TFields]: TFields[K] extends Field<infer T> ? T : never;
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
export type PartialRow<TFields extends Fields> = Partial<Row<TFields>>;
|
|
13
|
+
|
|
14
|
+
export interface SchemaView<TFields extends Fields = Fields> {
|
|
15
|
+
readonly name: string;
|
|
16
|
+
/** What this schema was cut from, when it was cut from anything. */
|
|
17
|
+
readonly derivation?: SchemaDerivation;
|
|
18
|
+
/** What its fields were called before, keyed by the names they carry now. */
|
|
19
|
+
readonly previous?: PreviousNames<TFields>;
|
|
20
|
+
/** Does it hold rows of its own? False on a derivation means an answer. */
|
|
21
|
+
readonly anchored?: boolean;
|
|
22
|
+
getFields(): TFields;
|
|
23
|
+
getAdapters(): EntityAdapters<TFields> | undefined;
|
|
24
|
+
getUnique(): CompositeUnique<TFields> | undefined;
|
|
25
|
+
getOpts(): ValidateOptions;
|
|
26
|
+
validate(input: unknown): ValidationResult<Row<TFields>>;
|
|
27
|
+
}
|
package/src/utils.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export function lowerFirst(text: string): string {
|
|
2
|
+
return text ? text[0].toLowerCase() + text.slice(1) : text;
|
|
3
|
+
}
|
|
4
|
+
|
|
5
|
+
export function upperFirst(text: string): string {
|
|
6
|
+
return text ? text[0].toUpperCase() + text.slice(1) : text;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export function clean<T extends Record<string, unknown>>(obj: T): T {
|
|
10
|
+
for (const key of Object.keys(obj)) if (obj[key] === undefined) delete obj[key];
|
|
11
|
+
|
|
12
|
+
return obj;
|
|
13
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { Field } from '../fields/Field.js';
|
|
2
|
+
|
|
3
|
+
export function bool(opts?: { default?: boolean; description?: string }): Field<boolean> {
|
|
4
|
+
return new Field<boolean>({
|
|
5
|
+
shape: { type: 'boolean' },
|
|
6
|
+
lifecycle: opts?.default !== undefined ? { create: { value: opts.default } } : undefined,
|
|
7
|
+
meta: opts?.description !== undefined ? { description: opts.description } : undefined,
|
|
8
|
+
});
|
|
9
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { Field } from '../fields/Field.js';
|
|
2
|
+
import type { SchemaView } from '../schema/SchemaView.js';
|
|
3
|
+
import { RowJudge } from '../judge/RowJudge.js';
|
|
4
|
+
|
|
5
|
+
export function json<T = unknown>(): Field<T>;
|
|
6
|
+
export function json<E extends SchemaView & (new (...args: any[]) => any)>(of: E): Field<InstanceType<E>>;
|
|
7
|
+
export function json(of?: SchemaView): Field<unknown> {
|
|
8
|
+
if (!of) return new Field({ shape: { type: 'object' } });
|
|
9
|
+
const fields = of.getFields();
|
|
10
|
+
const judge = RowJudge.of(fields);
|
|
11
|
+
const properties: Record<string, unknown> = {};
|
|
12
|
+
const required: string[] = [];
|
|
13
|
+
for (const [key, field] of Object.entries(fields)) {
|
|
14
|
+
properties[key] = field.meta?.description
|
|
15
|
+
? { ...field.shape, description: field.meta.description }
|
|
16
|
+
: field.shape;
|
|
17
|
+
if (judge.onAbsent(field) === null) required.push(key);
|
|
18
|
+
}
|
|
19
|
+
return new Field({
|
|
20
|
+
shape: { type: 'object', properties, ...(required.length ? { required } : {}) },
|
|
21
|
+
});
|
|
22
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { Field } from '../fields/Field.js';
|
|
2
|
+
|
|
3
|
+
export interface ListOptions {
|
|
4
|
+
min?: number;
|
|
5
|
+
max?: number;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export function list<T>(item: Field<T>, opts?: ListOptions): Field<T[]> {
|
|
9
|
+
if (item.role?.relation) throw new Error('list() takes a value field (text(), number()…) — a relation has no value shape');
|
|
10
|
+
return new Field<T[]>({
|
|
11
|
+
shape: { type: 'array', items: item.shape, minItems: opts?.min, maxItems: opts?.max },
|
|
12
|
+
});
|
|
13
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { Relation, type EntityConstructor } from '../axis/role/Relation.js';
|
|
2
|
+
import { Field } from '../fields/Field.js';
|
|
3
|
+
|
|
4
|
+
export function many<E extends EntityConstructor>(target: E | (() => E)): Field<InstanceType<E>[]> {
|
|
5
|
+
return new Field<InstanceType<E>[]>({
|
|
6
|
+
shape: { type: 'array' },
|
|
7
|
+
role: { relation: Relation.many(target) },
|
|
8
|
+
});
|
|
9
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { vocabulary } from './vocabulary.js';
|
|
2
|
+
import { Anatomy } from '../axis/shape/Shape.js';
|
|
3
|
+
import { type Field } from '../fields/Field.js';
|
|
4
|
+
|
|
5
|
+
export const nullable: <T>(field: Field<T>) => Field<T | null> = vocabulary('nullable', (field) => ({
|
|
6
|
+
shape: Anatomy.nullable(field.shape),
|
|
7
|
+
}));
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { Field } from '../fields/Field.js';
|
|
2
|
+
|
|
3
|
+
export interface NumberOptions {
|
|
4
|
+
min?: number;
|
|
5
|
+
max?: number;
|
|
6
|
+
integer?: boolean;
|
|
7
|
+
default?: number;
|
|
8
|
+
description?: string;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export function number(opts?: NumberOptions): Field<number> {
|
|
12
|
+
return new Field<number>({
|
|
13
|
+
shape: { type: opts?.integer ? 'integer' : 'number', minimum: opts?.min, maximum: opts?.max },
|
|
14
|
+
lifecycle: opts?.default !== undefined ? { create: { value: opts.default } } : undefined,
|
|
15
|
+
meta: opts?.description !== undefined ? { description: opts.description } : undefined,
|
|
16
|
+
});
|
|
17
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { Field } from '../fields/Field.js';
|
|
2
|
+
|
|
3
|
+
interface OneOfOptions<V extends string = string> {
|
|
4
|
+
description?: string;
|
|
5
|
+
default?: V;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export function oneOf<const T extends readonly string[]>(...values: [...T]): Field<T[number]>;
|
|
9
|
+
export function oneOf<const T extends readonly string[]>(
|
|
10
|
+
...args: [...T, OneOfOptions<T[number]>]
|
|
11
|
+
): Field<T[number]>;
|
|
12
|
+
export function oneOf<const T extends readonly string[]>(
|
|
13
|
+
...args: [...T] | [...T, OneOfOptions<T[number]>]
|
|
14
|
+
): Field<T[number]> {
|
|
15
|
+
const last = args[args.length - 1];
|
|
16
|
+
const hasOpts = typeof last === 'object' && last !== null && !Array.isArray(last);
|
|
17
|
+
const values = (hasOpts ? args.slice(0, -1) : args) as unknown as readonly string[];
|
|
18
|
+
const opts = hasOpts ? (last as OneOfOptions) : {};
|
|
19
|
+
return new Field<T[number]>({
|
|
20
|
+
shape: { type: 'string', enum: values },
|
|
21
|
+
lifecycle: opts.default !== undefined ? { create: { value: opts.default } } : undefined,
|
|
22
|
+
meta: opts.description !== undefined ? { description: opts.description } : undefined,
|
|
23
|
+
});
|
|
24
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { vocabulary } from './vocabulary.js';
|
|
2
|
+
import { Anatomy } from '../axis/shape/Shape.js';
|
|
3
|
+
import { type Field } from '../fields/Field.js';
|
|
4
|
+
|
|
5
|
+
export const optional: <T>(field: Field<T>) => Field<T | null> = vocabulary('optional', (field) => ({
|
|
6
|
+
shape: Anatomy.nullable(field.shape),
|
|
7
|
+
...(field.lifecycle?.create === undefined ? { lifecycle: { create: 'optional' as const } } : {}),
|
|
8
|
+
}));
|