@fncts/schema 0.0.5 → 0.0.7

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.
Files changed (157) hide show
  1. package/AST.d.ts +528 -0
  2. package/ASTAnnotation.d.ts +82 -0
  3. package/ASTAnnotationMap.d.ts +14 -0
  4. package/Gen.d.ts +15 -0
  5. package/InvalidInterpretationError.d.ts +5 -0
  6. package/ParseError.d.ts +107 -0
  7. package/ParseResult.d.ts +24 -0
  8. package/Parser/api.d.ts +42 -0
  9. package/Parser/definition.d.ts +22 -0
  10. package/Parser/interpreter.d.ts +6 -0
  11. package/Parser.d.ts +3 -0
  12. package/Schema/api/conc.d.ts +18 -0
  13. package/Schema/api/either.d.ts +19 -0
  14. package/Schema/api/hashMap.d.ts +21 -0
  15. package/Schema/api/immutableArray.d.ts +23 -0
  16. package/Schema/api/list.d.ts +23 -0
  17. package/Schema/api/maybe.d.ts +21 -0
  18. package/Schema/api.d.ts +243 -0
  19. package/Schema/definition.d.ts +29 -0
  20. package/Schema/derivations.d.ts +70 -0
  21. package/Schema.d.ts +9 -144
  22. package/_cjs/AST.cjs +1172 -0
  23. package/_cjs/AST.cjs.map +1 -0
  24. package/_cjs/ASTAnnotation.cjs +111 -0
  25. package/_cjs/ASTAnnotation.cjs.map +1 -0
  26. package/_cjs/ASTAnnotationMap.cjs +35 -0
  27. package/_cjs/ASTAnnotationMap.cjs.map +1 -0
  28. package/_cjs/Gen.cjs +182 -0
  29. package/_cjs/Gen.cjs.map +1 -0
  30. package/_cjs/InvalidInterpretationError.cjs +18 -0
  31. package/_cjs/InvalidInterpretationError.cjs.map +1 -0
  32. package/_cjs/ParseError.cjs +223 -0
  33. package/_cjs/ParseError.cjs.map +1 -0
  34. package/_cjs/{Decoder.cjs → ParseResult.cjs} +25 -22
  35. package/_cjs/ParseResult.cjs.map +1 -0
  36. package/_cjs/Parser/api.cjs +80 -0
  37. package/_cjs/Parser/api.cjs.map +1 -0
  38. package/_cjs/{Guard.cjs → Parser/definition.cjs} +17 -22
  39. package/_cjs/Parser/definition.cjs.map +1 -0
  40. package/_cjs/Parser/interpreter.cjs +410 -0
  41. package/_cjs/Parser/interpreter.cjs.map +1 -0
  42. package/_cjs/Parser.cjs +39 -0
  43. package/_cjs/Parser.cjs.map +1 -0
  44. package/_cjs/Schema/api/conc.cjs +84 -0
  45. package/_cjs/Schema/api/conc.cjs.map +1 -0
  46. package/_cjs/Schema/api/either.cjs +96 -0
  47. package/_cjs/Schema/api/either.cjs.map +1 -0
  48. package/_cjs/Schema/api/hashMap.cjs +161 -0
  49. package/_cjs/Schema/api/hashMap.cjs.map +1 -0
  50. package/_cjs/Schema/api/immutableArray.cjs +90 -0
  51. package/_cjs/Schema/api/immutableArray.cjs.map +1 -0
  52. package/_cjs/Schema/api/list.cjs +98 -0
  53. package/_cjs/Schema/api/list.cjs.map +1 -0
  54. package/_cjs/Schema/api/maybe.cjs +76 -0
  55. package/_cjs/Schema/api/maybe.cjs.map +1 -0
  56. package/_cjs/Schema/api.cjs +425 -0
  57. package/_cjs/Schema/api.cjs.map +1 -0
  58. package/_cjs/Schema/definition.cjs +26 -0
  59. package/_cjs/Schema/definition.cjs.map +1 -0
  60. package/_cjs/Schema/derivations.cjs +108 -0
  61. package/_cjs/Schema/derivations.cjs.map +1 -0
  62. package/_cjs/Schema.cjs +97 -237
  63. package/_cjs/Schema.cjs.map +1 -1
  64. package/_cjs/utils.cjs +52 -0
  65. package/_cjs/utils.cjs.map +1 -0
  66. package/_mjs/AST.mjs +1061 -0
  67. package/_mjs/AST.mjs.map +1 -0
  68. package/_mjs/ASTAnnotation.mjs +80 -0
  69. package/_mjs/ASTAnnotation.mjs.map +1 -0
  70. package/_mjs/ASTAnnotationMap.mjs +27 -0
  71. package/_mjs/ASTAnnotationMap.mjs.map +1 -0
  72. package/_mjs/Gen.mjs +173 -0
  73. package/_mjs/Gen.mjs.map +1 -0
  74. package/_mjs/InvalidInterpretationError.mjs +10 -0
  75. package/_mjs/InvalidInterpretationError.mjs.map +1 -0
  76. package/_mjs/ParseError.mjs +201 -0
  77. package/_mjs/ParseError.mjs.map +1 -0
  78. package/_mjs/ParseResult.mjs +22 -0
  79. package/_mjs/ParseResult.mjs.map +1 -0
  80. package/_mjs/Parser/api.mjs +67 -0
  81. package/_mjs/Parser/api.mjs.map +1 -0
  82. package/_mjs/Parser/definition.mjs +15 -0
  83. package/_mjs/Parser/definition.mjs.map +1 -0
  84. package/_mjs/Parser/interpreter.mjs +402 -0
  85. package/_mjs/Parser/interpreter.mjs.map +1 -0
  86. package/_mjs/Parser.mjs +5 -0
  87. package/_mjs/Parser.mjs.map +1 -0
  88. package/_mjs/Schema/api/conc.mjs +72 -0
  89. package/_mjs/Schema/api/conc.mjs.map +1 -0
  90. package/_mjs/Schema/api/either.mjs +85 -0
  91. package/_mjs/Schema/api/either.mjs.map +1 -0
  92. package/_mjs/Schema/api/hashMap.mjs +150 -0
  93. package/_mjs/Schema/api/hashMap.mjs.map +1 -0
  94. package/_mjs/Schema/api/immutableArray.mjs +79 -0
  95. package/_mjs/Schema/api/immutableArray.mjs.map +1 -0
  96. package/_mjs/Schema/api/list.mjs +87 -0
  97. package/_mjs/Schema/api/list.mjs.map +1 -0
  98. package/_mjs/Schema/api/maybe.mjs +65 -0
  99. package/_mjs/Schema/api/maybe.mjs.map +1 -0
  100. package/_mjs/Schema/api.mjs +368 -0
  101. package/_mjs/Schema/api.mjs.map +1 -0
  102. package/_mjs/Schema/definition.mjs +16 -0
  103. package/_mjs/Schema/definition.mjs.map +1 -0
  104. package/_mjs/Schema/derivations.mjs +94 -0
  105. package/_mjs/Schema/derivations.mjs.map +1 -0
  106. package/_mjs/Schema.mjs +12 -212
  107. package/_mjs/Schema.mjs.map +1 -1
  108. package/_mjs/utils.mjs +41 -0
  109. package/_mjs/utils.mjs.map +1 -0
  110. package/_src/AST.ts +1353 -0
  111. package/_src/ASTAnnotation.ts +98 -0
  112. package/_src/ASTAnnotationMap.ts +38 -0
  113. package/_src/Gen.ts +168 -0
  114. package/_src/InvalidInterpretationError.ts +6 -0
  115. package/_src/ParseError.ts +237 -0
  116. package/_src/ParseResult.ts +26 -0
  117. package/_src/Parser/api.ts +71 -0
  118. package/_src/Parser/definition.ts +24 -0
  119. package/_src/Parser/interpreter.ts +442 -0
  120. package/_src/Parser.ts +5 -0
  121. package/_src/Schema/api/conc.ts +78 -0
  122. package/_src/Schema/api/either.ts +96 -0
  123. package/_src/Schema/api/hashMap.ts +184 -0
  124. package/_src/Schema/api/immutableArray.ts +88 -0
  125. package/_src/Schema/api/list.ts +96 -0
  126. package/_src/Schema/api/maybe.ts +68 -0
  127. package/_src/Schema/api.ts +530 -0
  128. package/_src/Schema/definition.ts +32 -0
  129. package/_src/Schema/derivations.ts +185 -0
  130. package/_src/Schema.ts +14 -254
  131. package/_src/global.ts +53 -0
  132. package/_src/utils.ts +48 -0
  133. package/global.d.ts +52 -0
  134. package/package.json +2 -2
  135. package/utils.d.ts +8 -0
  136. package/Decoder.d.ts +0 -3
  137. package/Encoder.d.ts +0 -4
  138. package/Guard.d.ts +0 -3
  139. package/Schemable.d.ts +0 -39
  140. package/_cjs/Decoder.cjs.map +0 -1
  141. package/_cjs/Encoder.cjs +0 -45
  142. package/_cjs/Encoder.cjs.map +0 -1
  143. package/_cjs/Guard.cjs.map +0 -1
  144. package/_cjs/Schemable.cjs +0 -6
  145. package/_cjs/Schemable.cjs.map +0 -1
  146. package/_mjs/Decoder.mjs +0 -20
  147. package/_mjs/Decoder.mjs.map +0 -1
  148. package/_mjs/Encoder.mjs +0 -36
  149. package/_mjs/Encoder.mjs.map +0 -1
  150. package/_mjs/Guard.mjs +0 -20
  151. package/_mjs/Guard.mjs.map +0 -1
  152. package/_mjs/Schemable.mjs +0 -2
  153. package/_mjs/Schemable.mjs.map +0 -1
  154. package/_src/Decoder.ts +0 -20
  155. package/_src/Encoder.ts +0 -38
  156. package/_src/Guard.ts +0 -20
  157. package/_src/Schemable.ts +0 -46
@@ -0,0 +1,185 @@
1
+ import type { TypeLiteral } from "@fncts/schema/AST";
2
+ import type { OptionalKeys, RequiredKeys } from "@fncts/typelevel/Object";
3
+
4
+ import { ownKeys } from "@fncts/schema/utils";
5
+
6
+ /**
7
+ * @tsplus derive fncts.schema.Schema<_> 10
8
+ */
9
+ export function deriveValidation<A extends Brand.Valid<any, any>>(
10
+ ...[base, brands]: Check<Brand.IsValidated<A>> extends Check.True
11
+ ? [
12
+ base: Schema<Brand.Unbranded<A>>,
13
+ brands: {
14
+ [K in keyof A[Brand.valid] & string]: Validation<A[Brand.valid][K], K>;
15
+ },
16
+ ]
17
+ : never
18
+ ): Schema<A> {
19
+ return Schema.fromAST(
20
+ AST.createValidation(
21
+ base.ast,
22
+ Vector.from(Object.values(brands)),
23
+ base.ast.annotations.annotate(ASTAnnotation.Brand, Vector.from(Object.values(brands))),
24
+ ),
25
+ );
26
+ }
27
+
28
+ /**
29
+ * @tsplus derive fncts.schema.Schema<_> 20
30
+ */
31
+ export function deriveLiteral<A extends LiteralValue>(
32
+ ...[value]: Check<Check.IsLiteral<A> & Check.Not<Check.IsUnion<A>>> extends Check.True ? [value: A] : never
33
+ ): Schema<A> {
34
+ return Schema.literal(value);
35
+ }
36
+
37
+ type MaybeKeys<A> = { [K in keyof A]: A[K] extends Maybe<any> ? K : never }[keyof A];
38
+
39
+ /**
40
+ * @tsplus derive fncts.schema.Schema<_> 20
41
+ */
42
+ export function deriveStruct<A extends Record<string, any>>(
43
+ ...[requiredFields, optionalFields, maybeFields]: Check<Check.IsStruct<A>> extends Check.True
44
+ ? [
45
+ ...[
46
+ requiredFields: {
47
+ [k in Exclude<RequiredKeys<A>, MaybeKeys<A>>]: Schema<A[k]>;
48
+ },
49
+ ],
50
+ ...([OptionalKeys<A>] extends [never]
51
+ ? [optionalFields: {}]
52
+ : [
53
+ optionalFields: {
54
+ [k in OptionalKeys<A>]: Schema<NonNullable<A[k]>>;
55
+ },
56
+ ]),
57
+ ...([MaybeKeys<A>] extends [never]
58
+ ? []
59
+ : [
60
+ maybeFields: {
61
+ [k in MaybeKeys<A>]: [A[k]] extends [Maybe<infer _A>] ? Schema<_A> : never;
62
+ },
63
+ ]),
64
+ ]
65
+ : never
66
+ ): Schema<A> {
67
+ const maybeFieldsKeys: Vector<PropertyKey> = ownKeys(maybeFields);
68
+ let propertySignatures = ownKeys(requiredFields).map((key) =>
69
+ AST.createPropertySignature(key, requiredFields[key]!.ast, false, true),
70
+ );
71
+
72
+ if (optionalFields) {
73
+ propertySignatures = propertySignatures.concat(
74
+ ownKeys(optionalFields).map((key) => AST.createPropertySignature(key, optionalFields[key]!.ast, true, true)),
75
+ );
76
+ }
77
+
78
+ const struct = Schema.fromAST(AST.createTypeLiteral(propertySignatures, Vector.empty()));
79
+
80
+ if (maybeFieldsKeys.isEmpty()) {
81
+ return struct as Schema<any>;
82
+ }
83
+
84
+ propertySignatures = (struct.ast as TypeLiteral).propertySignatures;
85
+
86
+ const from = Schema.fromAST<any>(
87
+ AST.createTypeLiteral(
88
+ propertySignatures.concat(
89
+ maybeFieldsKeys.map((key) =>
90
+ AST.createPropertySignature(
91
+ key,
92
+ AST.createUnion(Vector(AST.undefinedKeyword, AST.createLiteral(null), maybeFields![key]!.ast)),
93
+ true,
94
+ true,
95
+ ),
96
+ ),
97
+ ),
98
+ Vector.empty(),
99
+ ),
100
+ );
101
+
102
+ const to = Schema.fromAST<any>(
103
+ AST.createTypeLiteral(
104
+ propertySignatures.concat(
105
+ maybeFieldsKeys.map((key) =>
106
+ AST.createPropertySignature(key, Schema.maybe(maybeFields![key]!).ast, true, true),
107
+ ),
108
+ ),
109
+ Vector.empty(),
110
+ ),
111
+ );
112
+
113
+ return from.transform(
114
+ to,
115
+ (input) => {
116
+ const output = { ...input };
117
+ for (const key of maybeFieldsKeys) {
118
+ output[key] = Maybe.fromNullable(input[key]);
119
+ }
120
+ return output;
121
+ },
122
+ (input) => {
123
+ const output = { ...input };
124
+ for (const key of maybeFieldsKeys) {
125
+ const value: Maybe<any> = input[key];
126
+ if (value.isNothing()) {
127
+ delete output[key];
128
+ continue;
129
+ }
130
+ output[key] = value.value;
131
+ }
132
+ return output;
133
+ },
134
+ );
135
+ }
136
+
137
+ /**
138
+ * @tsplus derive fncts.schema.Schema<_> 10
139
+ */
140
+ export function deriveTuple<A extends ReadonlyArray<unknown>>(
141
+ ...[components]: Check<Check.IsTuple<A>> extends Check.True ? [components: { [K in keyof A]: Schema<A[K]> }] : never
142
+ ): Schema<A> {
143
+ return unsafeCoerce(Schema.tuple(...components));
144
+ }
145
+
146
+ /**
147
+ * @tsplus derive fncts.schema.Schema lazy
148
+ */
149
+ export function deriveLazy<A>(f: (_: Schema<A>) => Schema<A>): Schema<A> {
150
+ let cached: AST | undefined;
151
+ const ast = AST.createLazy(() => {
152
+ if (!cached) {
153
+ cached = f(Schema.fromAST(ast)).ast;
154
+ }
155
+ return cached;
156
+ });
157
+ return Schema.fromAST(ast);
158
+ }
159
+
160
+ /**
161
+ * @tsplus derive fncts.schema.Schema[fncts.Array]<_> 10
162
+ * @tsplus derive fncts.schema.Schema[fncts.ReadonlyArray]<_> 10
163
+ */
164
+ export function deriveArray<A extends ReadonlyArray<any>>(
165
+ ...[element]: [A] extends [ReadonlyArray<infer _A>]
166
+ ? Check<Check.IsEqual<A, ReadonlyArray<_A>>> extends Check.True
167
+ ? [element: Schema<_A>]
168
+ : never
169
+ : never
170
+ ): Schema<A> {
171
+ return Schema.array(element) as unknown as Schema<A>;
172
+ }
173
+
174
+ /**
175
+ * @tsplus derive fncts.schema.Schema<_> 15
176
+ */
177
+ export function deriveRecord<A extends Record<string | symbol, any>>(
178
+ ...[keySchema, valueSchema]: [A] extends [Record<infer X, infer Y>]
179
+ ? Check<Check.Not<Check.IsUnion<A>> & Check.IsEqual<A, Record<X, Y>>> extends Check.True
180
+ ? [keySchema: Schema<X>, valueSchema: Schema<Y>]
181
+ : never
182
+ : never
183
+ ): Schema<A> {
184
+ return Schema.record(keySchema as Schema<string | symbol>, valueSchema) as Schema<A>;
185
+ }
package/_src/Schema.ts CHANGED
@@ -1,254 +1,14 @@
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 SchemaVariance = Symbol.for("fncts.schema.Schema.Variance");
6
- export type SchemaVariance = typeof SchemaVariance;
7
-
8
- export const SchemaTypeId = Symbol.for("fncts.schema.Schema");
9
- export type SchemaTypeId = typeof SchemaTypeId;
10
-
11
- export const enum SchemaTag {
12
- Unknown,
13
- String,
14
- Number,
15
- Boolean,
16
- BigInt,
17
- Literal,
18
- Nullable,
19
- Struct,
20
- Partial,
21
- Array,
22
- Record,
23
- Tuple,
24
- Lazy,
25
- Validation,
26
- Union,
27
- }
28
-
29
- export interface SchemaF extends HKT {
30
- readonly type: Schema<this["A"]>;
31
- }
32
-
33
- export abstract class Schema<in out A> {
34
- readonly [SchemaTypeId]: SchemaTypeId = SchemaTypeId;
35
- declare [SchemaVariance]: {
36
- _A: (_: A) => A;
37
- };
38
- }
39
-
40
- export class UnknownSchema extends Schema<unknown> {
41
- readonly _tag = SchemaTag.Unknown;
42
- }
43
-
44
- export class StringSchema extends Schema<string> {
45
- readonly _tag = SchemaTag.String;
46
- }
47
-
48
- export class NumberSchema extends Schema<number> {
49
- readonly _tag = SchemaTag.Number;
50
- }
51
-
52
- export class BooleanSchema extends Schema<boolean> {
53
- readonly _tag = SchemaTag.Boolean;
54
- }
55
-
56
- export class BigIntSchema extends Schema<bigint> {
57
- readonly _tag = SchemaTag.BigInt;
58
- }
59
-
60
- export class LiteralSchema<A extends Literal> extends Schema<A> {
61
- readonly _tag = SchemaTag.Literal;
62
- constructor(readonly value: A) {
63
- super();
64
- }
65
- }
66
-
67
- export class NullableSchema<A> extends Schema<A | null | undefined> {
68
- readonly _tag = SchemaTag.Nullable;
69
- constructor(readonly base: Schema<A>) {
70
- super();
71
- }
72
- }
73
-
74
- export class StructSchema<A extends Record<string, any>> extends Schema<A> {
75
- readonly _tag = SchemaTag.Struct;
76
- constructor(readonly fields: { [K in keyof A]: Schema<A[K]> }) {
77
- super();
78
- }
79
- }
80
-
81
- export class PartialSchema<A extends Record<string, any>> extends Schema<Partial<A>> {
82
- readonly _tag = SchemaTag.Partial;
83
- constructor(readonly fields: { [K in keyof A]: Schema<A[K]> }) {
84
- super();
85
- }
86
- }
87
-
88
- export class ArraySchema<A> extends Schema<ReadonlyArray<A>> {
89
- readonly _tag = SchemaTag.Array;
90
- constructor(readonly base: Schema<A>) {
91
- super();
92
- }
93
- }
94
-
95
- export class RecordSchema<A> extends Schema<Record<string, A>> {
96
- readonly _tag = SchemaTag.Record;
97
- constructor(readonly base: Schema<A>) {
98
- super();
99
- }
100
- }
101
-
102
- export class TupleSchema<A extends ReadonlyArray<any>> extends Schema<A> {
103
- readonly _tag = SchemaTag.Tuple;
104
- constructor(readonly components: { [K in keyof A]: Schema<A[K]> }) {
105
- super();
106
- }
107
- }
108
-
109
- export class LazySchema<A> extends Schema<A> {
110
- readonly _tag = SchemaTag.Lazy;
111
- constructor(readonly make: () => Schema<A>) {
112
- super();
113
- }
114
- }
115
-
116
- export class ValidationSchema<A, B extends ReadonlyArray<Validation<A, any>>> extends Schema<
117
- A & { [K in keyof B]: B[K] extends Validation<any, infer S> ? Brand.Valid<A, S> : never }[number]
118
- > {
119
- readonly _tag = SchemaTag.Validation;
120
- constructor(readonly base: Schema<A>, readonly validations: readonly [...B]) {
121
- super();
122
- }
123
- }
124
-
125
- export class UnionSchema<A extends ReadonlyArray<any>> extends Schema<A[number]> {
126
- readonly _tag = SchemaTag.Union;
127
- constructor(readonly members: { [K in keyof A]: Schema<A[K]> }) {
128
- super();
129
- }
130
- }
131
-
132
- export const unknown: Schema<unknown> = new UnknownSchema();
133
-
134
- export const string: Schema<string> = new StringSchema();
135
-
136
- export const number: Schema<number> = new NumberSchema();
137
-
138
- export const boolean: Schema<boolean> = new BooleanSchema();
139
-
140
- export const bigint: Schema<bigint> = new BigIntSchema();
141
-
142
- export function literal<A extends Literal>(value: A): Schema<A> {
143
- return new LiteralSchema(value);
144
- }
145
-
146
- export function nullable<A>(base: Schema<A>): Schema<A | null | undefined> {
147
- return new NullableSchema(base);
148
- }
149
-
150
- export function struct<A extends Record<string, any>>(fields: { [K in keyof A]: Schema<A[K]> }): Schema<A> {
151
- return new StructSchema(fields);
152
- }
153
-
154
- export function partial<A extends Record<string, any>>(fields: { [K in keyof A]: Schema<A[K]> }): Schema<Partial<A>> {
155
- return new PartialSchema(fields);
156
- }
157
-
158
- export function array<A>(base: Schema<A>): Schema<ReadonlyArray<A>> {
159
- return new ArraySchema(base);
160
- }
161
-
162
- export function record<A>(base: Schema<A>): Schema<Record<string, A>> {
163
- return new RecordSchema(base);
164
- }
165
-
166
- export function tuple<A extends ReadonlyArray<any>>(...components: { [K in keyof A]: Schema<A[K]> }): Schema<A> {
167
- return new TupleSchema(components).unsafeCoerce();
168
- }
169
-
170
- export function lazy<A>(make: () => Schema<A>): Schema<A> {
171
- return new LazySchema(make);
172
- }
173
-
174
- export function validation<A, B extends ReadonlyArray<Validation<A, any>>>(...validations: B) {
175
- return (
176
- base: Schema<A>,
177
- ): Schema<A & { [K in keyof B]: B[K] extends Validation<any, infer S> ? Brand.Valid<A, S> : never }[number]> =>
178
- new ValidationSchema(base, validations).unsafeCoerce();
179
- }
180
-
181
- export function union<A extends ReadonlyArray<any>>(...members: { [K in keyof A]: Schema<A[K]> }): Schema<A[number]> {
182
- return new UnionSchema(members);
183
- }
184
-
185
- export type Concrete =
186
- | UnknownSchema
187
- | StringSchema
188
- | NumberSchema
189
- | BooleanSchema
190
- | BigIntSchema
191
- | LiteralSchema<any>
192
- | NullableSchema<any>
193
- | StructSchema<Record<string, any>>
194
- | PartialSchema<Record<string, any>>
195
- | ArraySchema<any>
196
- | RecordSchema<any>
197
- | TupleSchema<ReadonlyArray<any>>
198
- | LazySchema<any>
199
- | ValidationSchema<any, ReadonlyArray<Validation<any, any>>>
200
- | UnionSchema<ReadonlyArray<any>>;
201
-
202
- export function concrete(_: Schema<any>): asserts _ is Concrete {
203
- //
204
- }
205
-
206
- function cacheThunk<A>(f: () => A): () => A {
207
- let cached: A;
208
- return () => {
209
- if (!cached) {
210
- cached = f();
211
- }
212
- return cached;
213
- };
214
- }
215
-
216
- export function interpret<F extends HKT>(S: S<F>): <A>(schema: Schema<A>) => SchemableKind<F, A>;
217
- export function interpret<F extends HKT>(S: S<F>) {
218
- return <A>(schema: Schema<A>): SchemableKind<F, any> => {
219
- concrete(schema);
220
- switch (schema._tag) {
221
- case SchemaTag.Unknown:
222
- return S.unknown;
223
- case SchemaTag.String:
224
- return S.string;
225
- case SchemaTag.Number:
226
- return S.number;
227
- case SchemaTag.Boolean:
228
- return S.boolean;
229
- case SchemaTag.BigInt:
230
- return S.bigint;
231
- case SchemaTag.Literal:
232
- return S.literal(schema.value);
233
- case SchemaTag.Lazy:
234
- const cached = cacheThunk(schema.make);
235
- return S.lazy(() => interpret(S)(cached()));
236
- case SchemaTag.Nullable:
237
- return S.nullable(interpret(S)(schema.base));
238
- case SchemaTag.Struct:
239
- return S.struct(Dictionary.reverseGet(Dictionary.get(schema.fields).map(interpret(S))));
240
- case SchemaTag.Partial:
241
- return S.partial(Dictionary.reverseGet(Dictionary.get(schema.fields).map(interpret(S))));
242
- case SchemaTag.Array:
243
- return S.array(interpret(S)(schema.base));
244
- case SchemaTag.Record:
245
- return S.record(interpret(S)(schema.base));
246
- case SchemaTag.Tuple:
247
- return S.tuple(...schema.components.map(interpret(S)));
248
- case SchemaTag.Validation:
249
- return S.validation(...schema.validations)(interpret(S)(schema.base));
250
- case SchemaTag.Union:
251
- return S.union(schema.members.map(interpret(S)), schema);
252
- }
253
- };
254
- }
1
+ // codegen:start { preset: barrel, include: ./Schema/*.ts }
2
+ export * from "./Schema/api.js";
3
+ export * from "./Schema/definition.js";
4
+ export * from "./Schema/derivations.js";
5
+ // codegen:end
6
+
7
+ // codegen:start { preset: barrel, include: ./Schema/api/*.ts }
8
+ export * from "./Schema/api/conc.js";
9
+ export * from "./Schema/api/either.js";
10
+ export * from "./Schema/api/hashMap.js";
11
+ export * from "./Schema/api/immutableArray.js";
12
+ export * from "./Schema/api/list.js";
13
+ export * from "./Schema/api/maybe.js";
14
+ // codegen:end
package/_src/global.ts CHANGED
@@ -6,3 +6,56 @@ import type {} from "@fncts/base/global";
6
6
  * @tsplus global
7
7
  */
8
8
  import type { HKT } from "@fncts/typelevel/HKT";
9
+
10
+ /**
11
+ * @tsplus global
12
+ */
13
+ import { Brand, Validation } from "@fncts/base/data/Branded";
14
+ /**
15
+ * @tsplus global
16
+ */
17
+ import { FIO, IO, UIO } from "@fncts/io/IO";
18
+ /**
19
+ * @tsplus global
20
+ */
21
+ import { Layer } from "@fncts/io/Layer";
22
+ /**
23
+ * @tsplus global
24
+ */
25
+ import { Stream } from "@fncts/io/Stream";
26
+ /**
27
+ * @tsplus global
28
+ */
29
+ import { AST, LiteralValue } from "@fncts/schema/AST";
30
+ /**
31
+ * @tsplus global
32
+ */
33
+ import { ASTAnnotation } from "@fncts/schema/ASTAnnotation";
34
+ /**
35
+ * @tsplus global
36
+ */
37
+ import { ASTAnnotationMap } from "@fncts/schema/ASTAnnotationMap";
38
+ /**
39
+ * @tsplus global
40
+ */
41
+ import { ParseError } from "@fncts/schema/ParseError";
42
+ /**
43
+ * @tsplus global
44
+ */
45
+ import { Parser } from "@fncts/schema/Parser/definition";
46
+ /**
47
+ * @tsplus global
48
+ */
49
+ import { ParseResult } from "@fncts/schema/ParseResult";
50
+ /**
51
+ * @tsplus global
52
+ */
53
+ import { OptionalSchema, Schema } from "@fncts/schema/Schema/definition";
54
+ /**
55
+ * @tsplus global
56
+ */
57
+ import { Gen } from "@fncts/test/control/Gen";
58
+ /**
59
+ * @tsplus global
60
+ */
61
+ import { Check } from "@fncts/typelevel";
package/_src/utils.ts ADDED
@@ -0,0 +1,48 @@
1
+ import type { IndexSignature, TemplateLiteral } from "./AST";
2
+
3
+ import { ASTTag } from "./AST.js";
4
+
5
+ export function memoize<A, B>(f: (a: A) => B): (a: A) => B {
6
+ const cache = new Map();
7
+ return (a) => {
8
+ if (!cache.has(a)) {
9
+ const b = f(a);
10
+ cache.set(a, b);
11
+ return b;
12
+ }
13
+ return cache.get(a);
14
+ };
15
+ }
16
+
17
+ export function ownKeys(o: object | undefined): Vector<PropertyKey> {
18
+ return o ? Vector.from(Reflect.ownKeys(o)) : Vector.empty();
19
+ }
20
+
21
+ export function getTemplateLiteralRegex(ast: TemplateLiteral): RegExp {
22
+ let pattern = `^${ast.head}`;
23
+ for (const span of ast.spans) {
24
+ if (span.type.isStringKeyword()) {
25
+ pattern += ".*";
26
+ } else if (span.type.isNumberKeyword()) {
27
+ pattern += "-?\\d+(\\.\\d+)?";
28
+ }
29
+ pattern += span.literal;
30
+ }
31
+ pattern += "$";
32
+ return new RegExp(pattern);
33
+ }
34
+
35
+ export function getKeysForIndexSignature(
36
+ input: { readonly [x: PropertyKey]: unknown },
37
+ parameter: IndexSignature["parameter"],
38
+ ): Vector<string> | Vector<symbol> {
39
+ switch (parameter._tag) {
40
+ case ASTTag.StringKeyword:
41
+ case ASTTag.TemplateLiteral:
42
+ return Vector.from(Object.keys(input));
43
+ case ASTTag.SymbolKeyword:
44
+ return Vector.from(Object.getOwnPropertySymbols(input));
45
+ case ASTTag.Refinement:
46
+ return getKeysForIndexSignature(input, parameter.from as any);
47
+ }
48
+ }
package/global.d.ts CHANGED
@@ -6,4 +6,56 @@ import type {} from "@fncts/base/global";
6
6
  * @tsplus global
7
7
  */
8
8
  import type { HKT } from "@fncts/typelevel/HKT";
9
+ /**
10
+ * @tsplus global
11
+ */
12
+ import { Brand, Validation } from "@fncts/base/data/Branded";
13
+ /**
14
+ * @tsplus global
15
+ */
16
+ import { FIO, IO, UIO } from "@fncts/io/IO";
17
+ /**
18
+ * @tsplus global
19
+ */
20
+ import { Layer } from "@fncts/io/Layer";
21
+ /**
22
+ * @tsplus global
23
+ */
24
+ import { Stream } from "@fncts/io/Stream";
25
+ /**
26
+ * @tsplus global
27
+ */
28
+ import { AST, LiteralValue } from "@fncts/schema/AST";
29
+ /**
30
+ * @tsplus global
31
+ */
32
+ import { ASTAnnotation } from "@fncts/schema/ASTAnnotation";
33
+ /**
34
+ * @tsplus global
35
+ */
36
+ import { ASTAnnotationMap } from "@fncts/schema/ASTAnnotationMap";
37
+ /**
38
+ * @tsplus global
39
+ */
40
+ import { ParseError } from "@fncts/schema/ParseError";
41
+ /**
42
+ * @tsplus global
43
+ */
44
+ import { Parser } from "@fncts/schema/Parser/definition";
45
+ /**
46
+ * @tsplus global
47
+ */
48
+ import { ParseResult } from "@fncts/schema/ParseResult";
49
+ /**
50
+ * @tsplus global
51
+ */
52
+ import { OptionalSchema, Schema } from "@fncts/schema/Schema/definition";
53
+ /**
54
+ * @tsplus global
55
+ */
56
+ import { Gen } from "@fncts/test/control/Gen";
57
+ /**
58
+ * @tsplus global
59
+ */
60
+ import { Check } from "@fncts/typelevel";
9
61
  export {};
package/package.json CHANGED
@@ -1,8 +1,8 @@
1
1
  {
2
2
  "name": "@fncts/schema",
3
- "version": "0.0.5",
3
+ "version": "0.0.7",
4
4
  "dependencies": {
5
- "@fncts/base": "0.0.26",
5
+ "@fncts/base": "0.0.28",
6
6
  "@fncts/typelevel": "0.0.15"
7
7
  },
8
8
  "exports": {
package/utils.d.ts ADDED
@@ -0,0 +1,8 @@
1
+ import { Vector } from "@fncts/base/collection/immutable/Vector";
2
+ import type { IndexSignature, TemplateLiteral } from "./AST";
3
+ export declare function memoize<A, B>(f: (a: A) => B): (a: A) => B;
4
+ export declare function ownKeys(o: object | undefined): Vector<PropertyKey>;
5
+ export declare function getTemplateLiteralRegex(ast: TemplateLiteral): RegExp;
6
+ export declare function getKeysForIndexSignature(input: {
7
+ readonly [x: PropertyKey]: unknown;
8
+ }, parameter: IndexSignature["parameter"]): Vector<string> | Vector<symbol>;
package/Decoder.d.ts DELETED
@@ -1,3 +0,0 @@
1
- import type { DecoderF } from "@fncts/base/data/Decoder";
2
- import type { Schemable as S } from "@fncts/schema/Schemable";
3
- export declare const Schemable: S<DecoderF>;
package/Encoder.d.ts DELETED
@@ -1,4 +0,0 @@
1
- import { Encoder } from "@fncts/base/data/Encoder/definition";
2
- import type { EncoderF } from "@fncts/base/data/Encoder";
3
- import type { Schemable as S } from "@fncts/schema/Schemable";
4
- export declare const Schemable: S<EncoderF>;
package/Guard.d.ts DELETED
@@ -1,3 +0,0 @@
1
- import type { GuardF } from "@fncts/base/data/Guard";
2
- import type { Schemable as S } from "@fncts/schema/Schemable";
3
- export declare const Schemable: S<GuardF>;
package/Schemable.d.ts DELETED
@@ -1,39 +0,0 @@
1
- import { HKT } from "@fncts/typelevel/HKT";
2
- import type { Brand, Validation } from "@fncts/base/data/Branded";
3
- import type { UnionSchema } from "@fncts/schema/Schema";
4
- import type { Literal } from "@fncts/typelevel/Any";
5
- export type SchemableKind<F extends HKT, A> = F extends {
6
- readonly type: unknown;
7
- } ? (F & {
8
- readonly A: A;
9
- })["type"] : {
10
- readonly _F: F;
11
- readonly _A: A;
12
- };
13
- export interface Schemable<F extends HKT> {
14
- readonly unknown: SchemableKind<F, unknown>;
15
- readonly string: SchemableKind<F, string>;
16
- readonly number: SchemableKind<F, number>;
17
- readonly boolean: SchemableKind<F, boolean>;
18
- readonly bigint: SchemableKind<F, bigint>;
19
- readonly literal: <A extends Literal>(literal: A) => SchemableKind<F, A>;
20
- readonly nullable: <A>(or: SchemableKind<F, A>) => SchemableKind<F, A | null | undefined>;
21
- readonly struct: <P extends Record<string, any>>(properties: {
22
- [K in keyof P]: SchemableKind<F, P[K]>;
23
- }) => SchemableKind<F, P>;
24
- readonly partial: <P extends Record<string, any>>(properties: {
25
- [K in keyof P]: SchemableKind<F, P[K]>;
26
- }) => SchemableKind<F, Partial<P>>;
27
- readonly array: <A>(item: SchemableKind<F, A>) => SchemableKind<F, ReadonlyArray<A>>;
28
- readonly record: <A>(codomain: SchemableKind<F, A>) => SchemableKind<F, Record<string, A>>;
29
- readonly tuple: <C extends ReadonlyArray<any>>(...components: {
30
- [K in keyof C]: SchemableKind<F, C[K]>;
31
- }) => SchemableKind<F, C>;
32
- readonly lazy: <A>(f: () => SchemableKind<F, A>) => SchemableKind<F, A>;
33
- readonly validation: <A, B extends ReadonlyArray<Validation<A, any>>>(...validations: B) => (base: SchemableKind<F, A>) => SchemableKind<F, A & {
34
- [K in keyof B]: B[K] extends Validation<any, infer S> ? Brand.Valid<A, S> : never;
35
- }[number]>;
36
- readonly union: <A extends ReadonlyArray<any>>(members: {
37
- [K in keyof A]: SchemableKind<F, A[K]>;
38
- }, schema: UnionSchema<A>) => SchemableKind<F, A[number]>;
39
- }