@fncts/schema 0.0.1

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.
@@ -0,0 +1,36 @@
1
+ import * as tsplus_module_1 from "@fncts/base/data/Encoder/api";
2
+ import * as tsplus_module_2 from "@fncts/base/data/function/api";
3
+ import { interpret } from "@fncts/schema/Schema";
4
+ import * as G from "./Guard.mjs";
5
+ export const Schemable = {
6
+ unknown: tsplus_module_1.unknown,
7
+ string: tsplus_module_1.string,
8
+ number: tsplus_module_1.number,
9
+ boolean: tsplus_module_1.boolean,
10
+ bigint: tsplus_module_1.bigint,
11
+ literal: tsplus_module_1.literal,
12
+ nullable: tsplus_module_1.nullable,
13
+ struct: tsplus_module_1.struct,
14
+ partial: tsplus_module_1.partial,
15
+ array: tsplus_module_1.readonlyArray,
16
+ record: tsplus_module_1.record,
17
+ tuple: tsplus_module_1.tuple,
18
+ lazy: tsplus_module_1.deriveLazy,
19
+ validation: tsplus_module_1.validation,
20
+ union: (members, schema) => {
21
+ const guards = schema.members.map(interpret(G.Schemable));
22
+ return tsplus_module_1.makeEncoder(inp => {
23
+ let encoder;
24
+ for (let i = 0; i < guards.length; i++) {
25
+ if (guards[i].is(inp)) {
26
+ encoder = members[i];
27
+ }
28
+ }
29
+ if (!encoder) {
30
+ throw new Error("BUG: Encoder not found for union");
31
+ }
32
+ return encoder.encode(inp);
33
+ });
34
+ }
35
+ };
36
+ //# sourceMappingURL=Encoder.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Encoder.mjs","mappings":";;AAGA,SAASA,SAAS,QAAQ,sBAAsB;AAEhD,OAAO,KAAKC,CAAC,MAAM,aAAY;AAE/B,OAAO,MAAMC,SAAS,GAAgB;EACpCC,OAAO,yBAAiB;EACxBC,MAAM,wBAAU;EAChBC,MAAM,wBAAU;EAChBC,OAAO,yBAAU;EACjBC,MAAM,wBAAU;EAChBC,OAAO,yBAAiB;EACxBC,QAAQ,0BAAkB;EAC1BC,MAAM,wBAAgB;EACtBC,OAAO,yBAAiB;EACxBC,KAAK,+BAAuB;EAC5BC,MAAM,wBAAgB;EACtBC,KAAK,uBAA6B;EAClCC,IAAI,4BAAc;EAClBC,UAAU,4BAAkC;EAC5CC,KAAK,EAAE,CAACC,OAAO,EAAEC,MAAM,KAAI;IACzB,MAAMC,MAAM,GAAGD,MAAM,CAACD,OAAO,CAACG,GAAG,CAACrB,SAAS,CAACC,CAAC,CAACC,SAAS,CAAC,CAAC;IACzD,OAAOoB,4BAASC,GAAG,IAAI;MACrB,IAAIC,OAAiC;MACrC,KAAK,IAAIC,CAAC,GAAG,CAAC,EAAEA,CAAC,GAAGL,MAAM,CAACM,MAAM,EAAED,CAAC,EAAE,EAAE;QACtC,IAAIL,MAAM,CAACK,CAAC,CAAE,CAACE,EAAE,CAACJ,GAAG,CAAC,EAAE;UACtBC,OAAO,GAAGN,OAAO,CAACO,CAAC,CAAE;;;MAGzB,IAAI,CAACD,OAAO,EAAE;QACZ,MAAM,IAAII,KAAK,CAAC,kCAAkC,CAAC;;MAErD,OAAOJ,OAAO,CAACK,MAAM,CAACN,GAAG,CAAC;IAC5B,CAAC,CAAC;EACJ;CACD","names":["interpret","G","Schemable","unknown","string","number","boolean","bigint","literal","nullable","struct","partial","array","record","tuple","lazy","validation","union","members","schema","guards","map","tsplus_module_1","inp","encoder","i","length","is","Error","encode"],"sourceRoot":"","sources":["../_src/Encoder.ts"],"sourcesContent":[null]}
package/_mjs/Guard.mjs ADDED
@@ -0,0 +1,20 @@
1
+ import * as tsplus_module_1 from "@fncts/base/data/Guard/api";
2
+ import * as tsplus_module_2 from "@fncts/base/data/function/api";
3
+ export const Schemable = {
4
+ unknown: tsplus_module_1.unknown,
5
+ string: tsplus_module_1.string,
6
+ number: tsplus_module_1.number,
7
+ boolean: tsplus_module_1.boolean,
8
+ bigint: tsplus_module_1.bigint,
9
+ literal: tsplus_module_1.literal,
10
+ nullable: tsplus_module_1.nullable,
11
+ struct: tsplus_module_1.struct,
12
+ partial: tsplus_module_1.partial,
13
+ array: tsplus_module_1.readonlyArray,
14
+ record: tsplus_module_1.record,
15
+ tuple: tsplus_module_1.tuple,
16
+ lazy: tsplus_module_1.deriveLazy,
17
+ validation: tsplus_module_1.validation,
18
+ union: members => tsplus_module_1.deriveUnion(...members)
19
+ };
20
+ //# sourceMappingURL=Guard.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Guard.mjs","mappings":";;AAGA,OAAO,MAAMA,SAAS,GAAc;EAClCC,OAAO,yBAAe;EACtBC,MAAM,wBAAU;EAChBC,MAAM,wBAAU;EAChBC,OAAO,yBAAU;EACjBC,MAAM,wBAAU;EAChBC,OAAO,yBAAe;EACtBC,QAAQ,0BAAgB;EACxBC,MAAM,wBAAc;EACpBC,OAAO,yBAAe;EACtBC,KAAK,+BAAqB;EAC1BC,MAAM,wBAAc;EACpBC,KAAK,uBAA2B;EAChCC,IAAI,4BAAY;EAChBC,UAAU,4BAAgC;EAC1CC,KAAK,EAAGC,OAAO,IAAKC,4BAAY,GAAGD,OAAO;CAC3C","names":["Schemable","unknown","string","number","boolean","bigint","literal","nullable","struct","partial","array","record","tuple","lazy","validation","union","members","tsplus_module_1"],"sourceRoot":"","sources":["../_src/Guard.ts"],"sourcesContent":[null]}
@@ -0,0 +1,210 @@
1
+ import * as tsplus_module_1 from "@fncts/base/collection/immutable/Dictionary/definition";
2
+ import * as tsplus_module_2 from "@fncts/base/collection/immutable/Dictionary/api";
3
+ export const SchemaTypeId = /*#__PURE__*/Symbol.for("fncts.schema.Schema");
4
+ export class Schema {
5
+ constructor() {
6
+ this._typeId = SchemaTypeId;
7
+ }
8
+ }
9
+ export class UnknownSchema extends Schema {
10
+ constructor() {
11
+ super(...arguments);
12
+ this._tag = 0 /* SchemaTag.Unknown */;
13
+ }
14
+ }
15
+
16
+ export class StringSchema extends Schema {
17
+ constructor() {
18
+ super(...arguments);
19
+ this._tag = 1 /* SchemaTag.String */;
20
+ }
21
+ }
22
+
23
+ export class NumberSchema extends Schema {
24
+ constructor() {
25
+ super(...arguments);
26
+ this._tag = 2 /* SchemaTag.Number */;
27
+ }
28
+ }
29
+
30
+ export class BooleanSchema extends Schema {
31
+ constructor() {
32
+ super(...arguments);
33
+ this._tag = 3 /* SchemaTag.Boolean */;
34
+ }
35
+ }
36
+
37
+ export class BigIntSchema extends Schema {
38
+ constructor() {
39
+ super(...arguments);
40
+ this._tag = 4 /* SchemaTag.BigInt */;
41
+ }
42
+ }
43
+
44
+ export class LiteralSchema extends Schema {
45
+ constructor(value) {
46
+ super();
47
+ this.value = value;
48
+ this._tag = 5 /* SchemaTag.Literal */;
49
+ }
50
+ }
51
+
52
+ export class NullableSchema extends Schema {
53
+ constructor(base) {
54
+ super();
55
+ this.base = base;
56
+ this._tag = 6 /* SchemaTag.Nullable */;
57
+ }
58
+ }
59
+
60
+ export class StructSchema extends Schema {
61
+ constructor(fields) {
62
+ super();
63
+ this.fields = fields;
64
+ this._tag = 7 /* SchemaTag.Struct */;
65
+ }
66
+ }
67
+
68
+ export class PartialSchema extends Schema {
69
+ constructor(fields) {
70
+ super();
71
+ this.fields = fields;
72
+ this._tag = 8 /* SchemaTag.Partial */;
73
+ }
74
+ }
75
+
76
+ export class ArraySchema extends Schema {
77
+ constructor(base) {
78
+ super();
79
+ this.base = base;
80
+ this._tag = 9 /* SchemaTag.Array */;
81
+ }
82
+ }
83
+
84
+ export class RecordSchema extends Schema {
85
+ constructor(base) {
86
+ super();
87
+ this.base = base;
88
+ this._tag = 10 /* SchemaTag.Record */;
89
+ }
90
+ }
91
+
92
+ export class TupleSchema extends Schema {
93
+ constructor(components) {
94
+ super();
95
+ this.components = components;
96
+ this._tag = 11 /* SchemaTag.Tuple */;
97
+ }
98
+ }
99
+
100
+ export class LazySchema extends Schema {
101
+ constructor(make) {
102
+ super();
103
+ this.make = make;
104
+ this._tag = 12 /* SchemaTag.Lazy */;
105
+ }
106
+ }
107
+
108
+ export class ValidationSchema extends Schema {
109
+ constructor(base, validations) {
110
+ super();
111
+ this.base = base;
112
+ this.validations = validations;
113
+ this._tag = 13 /* SchemaTag.Validation */;
114
+ }
115
+ }
116
+
117
+ export class UnionSchema extends Schema {
118
+ constructor(members) {
119
+ super();
120
+ this.members = members;
121
+ this._tag = 14 /* SchemaTag.Union */;
122
+ }
123
+ }
124
+
125
+ export const unknown = /*#__PURE__*/new UnknownSchema();
126
+ export const string = /*#__PURE__*/new StringSchema();
127
+ export const number = /*#__PURE__*/new NumberSchema();
128
+ export const boolean = /*#__PURE__*/new BooleanSchema();
129
+ export const bigint = /*#__PURE__*/new BigIntSchema();
130
+ export function literal(value) {
131
+ return new LiteralSchema(value);
132
+ }
133
+ export function nullable(base) {
134
+ return new NullableSchema(base);
135
+ }
136
+ export function struct(fields) {
137
+ return new StructSchema(fields);
138
+ }
139
+ export function partial(fields) {
140
+ return new PartialSchema(fields);
141
+ }
142
+ export function array(base) {
143
+ return new ArraySchema(base);
144
+ }
145
+ export function record(base) {
146
+ return new RecordSchema(base);
147
+ }
148
+ export function tuple(...components) {
149
+ return new TupleSchema(components).unsafeCoerce();
150
+ }
151
+ export function lazy(make) {
152
+ return new LazySchema(make);
153
+ }
154
+ export function validation(...validations) {
155
+ return base => new ValidationSchema(base, validations).unsafeCoerce();
156
+ }
157
+ export function union(...members) {
158
+ return new UnionSchema(members);
159
+ }
160
+ export function concrete(_) {
161
+ //
162
+ }
163
+ function cacheThunk(f) {
164
+ let cached;
165
+ return () => {
166
+ if (!cached) {
167
+ cached = f();
168
+ }
169
+ return cached;
170
+ };
171
+ }
172
+ export function interpret(S) {
173
+ return schema => {
174
+ concrete(schema);
175
+ switch (schema._tag) {
176
+ case 0 /* SchemaTag.Unknown */:
177
+ return S.unknown;
178
+ case 1 /* SchemaTag.String */:
179
+ return S.string;
180
+ case 2 /* SchemaTag.Number */:
181
+ return S.number;
182
+ case 3 /* SchemaTag.Boolean */:
183
+ return S.boolean;
184
+ case 4 /* SchemaTag.BigInt */:
185
+ return S.bigint;
186
+ case 5 /* SchemaTag.Literal */:
187
+ return S.literal(schema.value);
188
+ case 12 /* SchemaTag.Lazy */:
189
+ const cached = cacheThunk(schema.make);
190
+ return S.lazy(() => interpret(S)(cached()));
191
+ case 6 /* SchemaTag.Nullable */:
192
+ return S.nullable(interpret(S)(schema.base));
193
+ case 7 /* SchemaTag.Struct */:
194
+ return S.struct(tsplus_module_1.Dictionary.reverseGet(tsplus_module_2.map(interpret(S))(tsplus_module_1.Dictionary.get(schema.fields))));
195
+ case 8 /* SchemaTag.Partial */:
196
+ return S.partial(tsplus_module_1.Dictionary.reverseGet(tsplus_module_2.map(interpret(S))(tsplus_module_1.Dictionary.get(schema.fields))));
197
+ case 9 /* SchemaTag.Array */:
198
+ return S.array(interpret(S)(schema.base));
199
+ case 10 /* SchemaTag.Record */:
200
+ return S.record(interpret(S)(schema.base));
201
+ case 11 /* SchemaTag.Tuple */:
202
+ return S.tuple(...schema.components.map(interpret(S)));
203
+ case 13 /* SchemaTag.Validation */:
204
+ return S.validation(...schema.validations)(interpret(S)(schema.base));
205
+ case 14 /* SchemaTag.Union */:
206
+ return S.union(schema.members.map(interpret(S)), schema);
207
+ }
208
+ };
209
+ }
210
+ //# sourceMappingURL=Schema.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Schema.mjs","mappings":";;AAIA,OAAO,MAAMA,YAAY,gBAAGC,MAAM,CAACC,GAAG,CAAC,qBAAqB,CAAC;AAyB7D,OAAM,MAAgBC,MAAM;EAA5BC;IACW,YAAO,GAAiBJ,YAAY;EAE/C;;AAEA,OAAM,MAAOK,aAAc,SAAQF,MAAe;EAAlDC;;IACW,SAAI;EACf;;;AAEA,OAAM,MAAOE,YAAa,SAAQH,MAAc;EAAhDC;;IACW,SAAI;EACf;;;AAEA,OAAM,MAAOG,YAAa,SAAQJ,MAAc;EAAhDC;;IACW,SAAI;EACf;;;AAEA,OAAM,MAAOI,aAAc,SAAQL,MAAe;EAAlDC;;IACW,SAAI;EACf;;;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AAM,CAAC,CAAkB,CAAC,CAAC;MACzF;QACE,OAAOkC,CAAC,CAACd,OAAO,CAACiB,0BAAU,CAACC,UAAU,CAACC,oBAAkCN,SAAS,CAACC,CAAC,CAAC,EAA9CG,0BAAU,CAACG,GAAG,CAACL,MAAM,CAACnC,MAAM,CAAC,CAAkB,CAAC,CAAC;MAC1F;QACE,OAAOkC,CAAC,CAACb,KAAK,CAACY,SAAS,CAACC,CAAC,CAAC,CAACC,MAAM,CAACrC,IAAI,CAAC,CAAC;MAC3C;QACE,OAAOoC,CAAC,CAACZ,MAAM,CAACW,SAAS,CAACC,CAAC,CAAC,CAACC,MAAM,CAACrC,IAAI,CAAC,CAAC;MAC5C;QACE,OAAOoC,CAAC,CAACX,KAAK,CAAC,GAAGY,MAAM,CAAC9B,UAAU,CAACoC,GAAG,CAACR,SAAS,CAACC,CAAC,CAAC,CAAC,CAAC;MACxD;QACE,OAAOA,CAAC,CAACR,UAAU,CAAC,GAAGS,MAAM,CAAC1B,WAAW,CAAC,CAACwB,SAAS,CAACC,CAAC,CAAC,CAACC,MAAM,CAACrC,IAAI,CAAC,CAAC;MACvE;QACE,OAAOoC,CAAC,CAACP,KAAK,CAACQ,MAAM,CAACxB,OAAO,CAAC8B,GAAG,CAACR,SAAS,CAACC,CAAC,CAAC,CAAC,EAAEC,MAAM,CAAC;IAAC;EAE/D,CAAC;AACH","names":["SchemaTypeId","Symbol","for","Schema","constructor","UnknownSchema","StringSchema","NumberSchema","BooleanSchema","BigIntSchema","LiteralSchema","value","NullableSchema","base","StructSchema","fields","PartialSchema","ArraySchema","RecordSchema","TupleSchema","components","LazySchema","make","ValidationSchema","validations","UnionSchema","members","unknown","string","number","boolean","bigint","literal","nullable","struct","partial","array","record","tuple","unsafeCoerce","lazy","validation","union","concrete","_","cacheThunk","f","cached","interpret","S","schema","_tag","tsplus_module_1","reverseGet","tsplus_module_2","get","map"],"sourceRoot":"","sources":["../_src/Schema.ts"],"sourcesContent":[null]}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=Schemable.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Schemable.mjs","mappings":"","names":[],"sourceRoot":"","sources":[],"sourcesContent":[]}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=global.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"global.mjs","mappings":"","names":[],"sourceRoot":"","sources":[],"sourcesContent":[]}
package/_mjs/index.mjs ADDED
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=index.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.mjs","mappings":"","names":[],"sourceRoot":"","sources":[],"sourcesContent":[]}
@@ -0,0 +1,20 @@
1
+ import type { DecoderF } from "@fncts/base/data/Decoder";
2
+ import type { Schemable as S } from "@fncts/schema/Schemable";
3
+
4
+ export const Schemable: S<DecoderF> = {
5
+ unknown: Decoder.unknown,
6
+ string: Derive(),
7
+ number: Derive(),
8
+ boolean: Derive(),
9
+ bigint: Derive(),
10
+ literal: Decoder.literal,
11
+ nullable: Decoder.nullable,
12
+ struct: Decoder.struct,
13
+ partial: Decoder.partial,
14
+ array: Decoder.readonlyArray,
15
+ record: Decoder.record,
16
+ tuple: unsafeCoerce(Decoder.tuple),
17
+ lazy: Decoder.lazy,
18
+ validation: unsafeCoerce(Decoder.validation),
19
+ union: (members) => Decoder.union(...members),
20
+ };
@@ -0,0 +1,38 @@
1
+ import type { EncoderF } from "@fncts/base/data/Encoder";
2
+ import type { Schemable as S } from "@fncts/schema/Schemable";
3
+
4
+ import { interpret } from "@fncts/schema/Schema";
5
+
6
+ import * as G from "./Guard.js";
7
+
8
+ export const Schemable: S<EncoderF> = {
9
+ unknown: Encoder.unknown,
10
+ string: Derive(),
11
+ number: Derive(),
12
+ boolean: Derive(),
13
+ bigint: Derive(),
14
+ literal: Encoder.literal,
15
+ nullable: Encoder.nullable,
16
+ struct: Encoder.struct,
17
+ partial: Encoder.partial,
18
+ array: Encoder.readonlyArray,
19
+ record: Encoder.record,
20
+ tuple: unsafeCoerce(Encoder.tuple),
21
+ lazy: Encoder.lazy,
22
+ validation: unsafeCoerce(Encoder.validation),
23
+ union: (members, schema) => {
24
+ const guards = schema.members.map(interpret(G.Schemable));
25
+ return Encoder((inp) => {
26
+ let encoder: Encoder<any> | undefined;
27
+ for (let i = 0; i < guards.length; i++) {
28
+ if (guards[i]!.is(inp)) {
29
+ encoder = members[i]!;
30
+ }
31
+ }
32
+ if (!encoder) {
33
+ throw new Error("BUG: Encoder not found for union");
34
+ }
35
+ return encoder.encode(inp);
36
+ });
37
+ },
38
+ };
package/_src/Guard.ts ADDED
@@ -0,0 +1,20 @@
1
+ import type { GuardF } from "@fncts/base/data/Guard";
2
+ import type { Schemable as S } from "@fncts/schema/Schemable";
3
+
4
+ export const Schemable: S<GuardF> = {
5
+ unknown: Guard.unknown,
6
+ string: Derive(),
7
+ number: Derive(),
8
+ boolean: Derive(),
9
+ bigint: Derive(),
10
+ literal: Guard.literal,
11
+ nullable: Guard.nullable,
12
+ struct: Guard.struct,
13
+ partial: Guard.partial,
14
+ array: Guard.readonlyArray,
15
+ record: Guard.record,
16
+ tuple: unsafeCoerce(Guard.tuple),
17
+ lazy: Guard.lazy,
18
+ validation: unsafeCoerce(Guard.validation),
19
+ union: (members) => Guard.union(...members),
20
+ };
package/_src/Schema.ts ADDED
@@ -0,0 +1,249 @@
1
+ import type { Brand, Validation } from "@fncts/base/data/Branded";
2
+ import type { Schemable as S, SchemableKind } from "@fncts/schema/Schemable";
3
+ import type { Literal } from "@fncts/typelevel/Any";
4
+
5
+ export const SchemaTypeId = Symbol.for("fncts.schema.Schema");
6
+ export type SchemaTypeId = typeof SchemaTypeId;
7
+
8
+ export const enum SchemaTag {
9
+ Unknown,
10
+ String,
11
+ Number,
12
+ Boolean,
13
+ BigInt,
14
+ Literal,
15
+ Nullable,
16
+ Struct,
17
+ Partial,
18
+ Array,
19
+ Record,
20
+ Tuple,
21
+ Lazy,
22
+ Validation,
23
+ Union,
24
+ }
25
+
26
+ export interface SchemaF extends HKT {
27
+ readonly type: Schema<this["A"]>;
28
+ }
29
+
30
+ export abstract class Schema<in out A> {
31
+ readonly _typeId: SchemaTypeId = SchemaTypeId;
32
+ declare _A: (_: A) => A;
33
+ }
34
+
35
+ export class UnknownSchema extends Schema<unknown> {
36
+ readonly _tag = SchemaTag.Unknown;
37
+ }
38
+
39
+ export class StringSchema extends Schema<string> {
40
+ readonly _tag = SchemaTag.String;
41
+ }
42
+
43
+ export class NumberSchema extends Schema<number> {
44
+ readonly _tag = SchemaTag.Number;
45
+ }
46
+
47
+ export class BooleanSchema extends Schema<boolean> {
48
+ readonly _tag = SchemaTag.Boolean;
49
+ }
50
+
51
+ export class BigIntSchema extends Schema<bigint> {
52
+ readonly _tag = SchemaTag.BigInt;
53
+ }
54
+
55
+ export class LiteralSchema<A extends Literal> extends Schema<A> {
56
+ readonly _tag = SchemaTag.Literal;
57
+ constructor(readonly value: A) {
58
+ super();
59
+ }
60
+ }
61
+
62
+ export class NullableSchema<A> extends Schema<A | null | undefined> {
63
+ readonly _tag = SchemaTag.Nullable;
64
+ constructor(readonly base: Schema<A>) {
65
+ super();
66
+ }
67
+ }
68
+
69
+ export class StructSchema<A extends Record<string, any>> extends Schema<A> {
70
+ readonly _tag = SchemaTag.Struct;
71
+ constructor(readonly fields: { [K in keyof A]: Schema<A[K]> }) {
72
+ super();
73
+ }
74
+ }
75
+
76
+ export class PartialSchema<A extends Record<string, any>> extends Schema<Partial<A>> {
77
+ readonly _tag = SchemaTag.Partial;
78
+ constructor(readonly fields: { [K in keyof A]: Schema<A[K]> }) {
79
+ super();
80
+ }
81
+ }
82
+
83
+ export class ArraySchema<A> extends Schema<ReadonlyArray<A>> {
84
+ readonly _tag = SchemaTag.Array;
85
+ constructor(readonly base: Schema<A>) {
86
+ super();
87
+ }
88
+ }
89
+
90
+ export class RecordSchema<A> extends Schema<Record<string, A>> {
91
+ readonly _tag = SchemaTag.Record;
92
+ constructor(readonly base: Schema<A>) {
93
+ super();
94
+ }
95
+ }
96
+
97
+ export class TupleSchema<A extends ReadonlyArray<any>> extends Schema<A> {
98
+ readonly _tag = SchemaTag.Tuple;
99
+ constructor(readonly components: { [K in keyof A]: Schema<A[K]> }) {
100
+ super();
101
+ }
102
+ }
103
+
104
+ export class LazySchema<A> extends Schema<A> {
105
+ readonly _tag = SchemaTag.Lazy;
106
+ constructor(readonly make: () => Schema<A>) {
107
+ super();
108
+ }
109
+ }
110
+
111
+ export class ValidationSchema<A, B extends ReadonlyArray<Validation<A, any>>> extends Schema<
112
+ A & { [K in keyof B]: B[K] extends Validation<any, infer S> ? Brand.Valid<A, S> : never }[number]
113
+ > {
114
+ readonly _tag = SchemaTag.Validation;
115
+ constructor(readonly base: Schema<A>, readonly validations: readonly [...B]) {
116
+ super();
117
+ }
118
+ }
119
+
120
+ export class UnionSchema<A extends ReadonlyArray<any>> extends Schema<A[number]> {
121
+ readonly _tag = SchemaTag.Union;
122
+ constructor(readonly members: { [K in keyof A]: Schema<A[K]> }) {
123
+ super();
124
+ }
125
+ }
126
+
127
+ export const unknown: Schema<unknown> = new UnknownSchema();
128
+
129
+ export const string: Schema<string> = new StringSchema();
130
+
131
+ export const number: Schema<number> = new NumberSchema();
132
+
133
+ export const boolean: Schema<boolean> = new BooleanSchema();
134
+
135
+ export const bigint: Schema<bigint> = new BigIntSchema();
136
+
137
+ export function literal<A extends Literal>(value: A): Schema<A> {
138
+ return new LiteralSchema(value);
139
+ }
140
+
141
+ export function nullable<A>(base: Schema<A>): Schema<A | null | undefined> {
142
+ return new NullableSchema(base);
143
+ }
144
+
145
+ export function struct<A extends Record<string, any>>(fields: { [K in keyof A]: Schema<A[K]> }): Schema<A> {
146
+ return new StructSchema(fields);
147
+ }
148
+
149
+ export function partial<A extends Record<string, any>>(fields: { [K in keyof A]: Schema<A[K]> }): Schema<Partial<A>> {
150
+ return new PartialSchema(fields);
151
+ }
152
+
153
+ export function array<A>(base: Schema<A>): Schema<ReadonlyArray<A>> {
154
+ return new ArraySchema(base);
155
+ }
156
+
157
+ export function record<A>(base: Schema<A>): Schema<Record<string, A>> {
158
+ return new RecordSchema(base);
159
+ }
160
+
161
+ export function tuple<A extends ReadonlyArray<any>>(...components: { [K in keyof A]: Schema<A[K]> }): Schema<A> {
162
+ return new TupleSchema(components).unsafeCoerce();
163
+ }
164
+
165
+ export function lazy<A>(make: () => Schema<A>): Schema<A> {
166
+ return new LazySchema(make);
167
+ }
168
+
169
+ export function validation<A, B extends ReadonlyArray<Validation<A, any>>>(...validations: B) {
170
+ return (
171
+ base: Schema<A>,
172
+ ): Schema<A & { [K in keyof B]: B[K] extends Validation<any, infer S> ? Brand.Valid<A, S> : never }[number]> =>
173
+ new ValidationSchema(base, validations).unsafeCoerce();
174
+ }
175
+
176
+ export function union<A extends ReadonlyArray<any>>(...members: { [K in keyof A]: Schema<A[K]> }): Schema<A[number]> {
177
+ return new UnionSchema(members);
178
+ }
179
+
180
+ export type Concrete =
181
+ | UnknownSchema
182
+ | StringSchema
183
+ | NumberSchema
184
+ | BooleanSchema
185
+ | BigIntSchema
186
+ | LiteralSchema<any>
187
+ | NullableSchema<any>
188
+ | StructSchema<Record<string, any>>
189
+ | PartialSchema<Record<string, any>>
190
+ | ArraySchema<any>
191
+ | RecordSchema<any>
192
+ | TupleSchema<ReadonlyArray<any>>
193
+ | LazySchema<any>
194
+ | ValidationSchema<any, ReadonlyArray<Validation<any, any>>>
195
+ | UnionSchema<ReadonlyArray<any>>;
196
+
197
+ export function concrete(_: Schema<any>): asserts _ is Concrete {
198
+ //
199
+ }
200
+
201
+ function cacheThunk<A>(f: () => A): () => A {
202
+ let cached: A;
203
+ return () => {
204
+ if (!cached) {
205
+ cached = f();
206
+ }
207
+ return cached;
208
+ };
209
+ }
210
+
211
+ export function interpret<F extends HKT>(S: S<F>): <A>(schema: Schema<A>) => SchemableKind<F, A>;
212
+ export function interpret<F extends HKT>(S: S<F>) {
213
+ return <A>(schema: Schema<A>): SchemableKind<F, any> => {
214
+ concrete(schema);
215
+ switch (schema._tag) {
216
+ case SchemaTag.Unknown:
217
+ return S.unknown;
218
+ case SchemaTag.String:
219
+ return S.string;
220
+ case SchemaTag.Number:
221
+ return S.number;
222
+ case SchemaTag.Boolean:
223
+ return S.boolean;
224
+ case SchemaTag.BigInt:
225
+ return S.bigint;
226
+ case SchemaTag.Literal:
227
+ return S.literal(schema.value);
228
+ case SchemaTag.Lazy:
229
+ const cached = cacheThunk(schema.make);
230
+ return S.lazy(() => interpret(S)(cached()));
231
+ case SchemaTag.Nullable:
232
+ return S.nullable(interpret(S)(schema.base));
233
+ case SchemaTag.Struct:
234
+ return S.struct(Dictionary.reverseGet(Dictionary.get(schema.fields).map(interpret(S))));
235
+ case SchemaTag.Partial:
236
+ return S.partial(Dictionary.reverseGet(Dictionary.get(schema.fields).map(interpret(S))));
237
+ case SchemaTag.Array:
238
+ return S.array(interpret(S)(schema.base));
239
+ case SchemaTag.Record:
240
+ return S.record(interpret(S)(schema.base));
241
+ case SchemaTag.Tuple:
242
+ return S.tuple(...schema.components.map(interpret(S)));
243
+ case SchemaTag.Validation:
244
+ return S.validation(...schema.validations)(interpret(S)(schema.base));
245
+ case SchemaTag.Union:
246
+ return S.union(schema.members.map(interpret(S)), schema);
247
+ }
248
+ };
249
+ }
@@ -0,0 +1,46 @@
1
+ import type { Brand, Validation } from "@fncts/base/data/Branded";
2
+ import type { UnionSchema } from "@fncts/schema/Schema";
3
+ import type { Literal } from "@fncts/typelevel/Any";
4
+
5
+ export type SchemableKind<F extends HKT, A> = F extends { readonly type: unknown }
6
+ ? (F & { readonly A: A })["type"]
7
+ : {
8
+ readonly _F: F;
9
+ readonly _A: A;
10
+ };
11
+
12
+ export interface Schemable<F extends HKT> {
13
+ readonly unknown: SchemableKind<F, unknown>;
14
+ readonly string: SchemableKind<F, string>;
15
+ readonly number: SchemableKind<F, number>;
16
+ readonly boolean: SchemableKind<F, boolean>;
17
+ readonly bigint: SchemableKind<F, bigint>;
18
+ readonly literal: <A extends Literal>(literal: A) => SchemableKind<F, A>;
19
+ readonly nullable: <A>(or: SchemableKind<F, A>) => SchemableKind<F, A | null | undefined>;
20
+ readonly struct: <P extends Record<string, any>>(properties: {
21
+ [K in keyof P]: SchemableKind<F, P[K]>;
22
+ }) => SchemableKind<F, P>;
23
+ readonly partial: <P extends Record<string, any>>(properties: {
24
+ [K in keyof P]: SchemableKind<F, P[K]>;
25
+ }) => SchemableKind<F, Partial<P>>;
26
+ readonly array: <A>(item: SchemableKind<F, A>) => SchemableKind<F, ReadonlyArray<A>>;
27
+ readonly record: <A>(codomain: SchemableKind<F, A>) => SchemableKind<F, Record<string, A>>;
28
+ readonly tuple: <C extends ReadonlyArray<any>>(
29
+ ...components: {
30
+ [K in keyof C]: SchemableKind<F, C[K]>;
31
+ }
32
+ ) => SchemableKind<F, C>;
33
+ readonly lazy: <A>(f: () => SchemableKind<F, A>) => SchemableKind<F, A>;
34
+ readonly validation: <A, B extends ReadonlyArray<Validation<A, any>>>(
35
+ ...validations: B
36
+ ) => (
37
+ base: SchemableKind<F, A>,
38
+ ) => SchemableKind<
39
+ F,
40
+ A & { [K in keyof B]: B[K] extends Validation<any, infer S> ? Brand.Valid<A, S> : never }[number]
41
+ >;
42
+ readonly union: <A extends ReadonlyArray<any>>(
43
+ members: { [K in keyof A]: SchemableKind<F, A[K]> },
44
+ schema: UnionSchema<A>,
45
+ ) => SchemableKind<F, A[number]>;
46
+ }