Skip to content

Commit aeafa1d

Browse files
committed
Deep Standard Schema Embedding
1 parent d45d331 commit aeafa1d

1 file changed

Lines changed: 85 additions & 76 deletions

File tree

src/schema/types/static.ts

Lines changed: 85 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ import type { XRequired } from './required.ts'
4545
// ------------------------------------------------------------------
4646
// XStandardSchemaV1
4747
// ------------------------------------------------------------------
48-
export type StaticXStandardSchemaV1<Validator extends XStandardValidatorV1,
48+
export type XStaticStandardSchemaV1<Validator extends XStandardValidatorV1,
4949
ValidateResult extends unknown = ReturnType<Validator['validate']>,
5050
RemoveAsync extends unknown = Exclude<ValidateResult, Promise<any>>,
5151
RemoveIssue extends unknown = Exclude<RemoveAsync, { issues: any }>,
@@ -54,83 +54,83 @@ export type StaticXStandardSchemaV1<Validator extends XStandardValidatorV1,
5454
// ------------------------------------------------------------------
5555
// XAdditionalProperties
5656
// ------------------------------------------------------------------
57-
type StaticXAdditionalProperties<Schema extends XSchemaLike,
57+
type XStaticAdditionalProperties<Schema extends XSchemaLike,
5858
Result extends Record<PropertyKey, unknown> = (
5959
Schema extends true ? { [key: string]: unknown } :
6060
Schema extends false ? {} :
61-
{ [key: string]: XStatic<Schema> }
61+
{ [key: string]: XStaticType<Schema> }
6262
)
6363
> = Result
6464
// ------------------------------------------------------------------
6565
// XAllOf
6666
// ------------------------------------------------------------------
67-
type StaticXAllOf<Schemas extends XSchemaLike[], Result extends unknown = unknown> =
67+
type XStaticAllOf<Schemas extends XSchemaLike[], Result extends unknown = unknown> =
6868
Schemas extends [infer Left extends XSchemaLike, ...infer Right extends XSchemaLike[]]
69-
? StaticXAllOf<Right, XStatic<Left> & Result>
69+
? XStaticAllOf<Right, XStaticType<Left> & Result>
7070
: Result
7171
// ------------------------------------------------------------------
7272
// XAnyOf
7373
// ------------------------------------------------------------------
74-
type StaticXAnyOf<Schemas extends XSchemaLike[], Result extends unknown = never> =
74+
type XStaticAnyOf<Schemas extends XSchemaLike[], Result extends unknown = never> =
7575
Schemas extends [infer Left extends XSchemaLike, ...infer Right extends XSchemaLike[]]
76-
? StaticXAnyOf<Right, XStatic<Left> | Result>
76+
? XStaticAnyOf<Right, XStaticType<Left> | Result>
7777
: Result
7878
// ------------------------------------------------------------------
7979
// XConst
8080
// ------------------------------------------------------------------
81-
type StaticXConst<Value extends unknown> = Value
81+
type XStaticConst<Value extends unknown> = Value
8282
// ------------------------------------------------------------------
8383
// XEnum
8484
// ------------------------------------------------------------------
85-
type StaticXEnum<Values extends unknown[], Result extends unknown = never> =
85+
type XStaticEnum<Values extends unknown[], Result extends unknown = never> =
8686
Values extends [infer Left extends unknown, ...infer Right extends unknown[]]
87-
? StaticXEnum<Right, Left | Result>
87+
? XStaticEnum<Right, Left | Result>
8888
: Result
8989
// ------------------------------------------------------------------
9090
// XItems
9191
// ------------------------------------------------------------------
92-
type StaticXItemsUnsized<Schema extends XSchemaLike> = XStatic<Schema>[]
93-
type StaticXItemsSized<Schema extends XSchemaLike[], Result extends unknown[] = []> =
92+
type XStaticItemsUnsized<Schema extends XSchemaLike> = XStaticType<Schema>[]
93+
type XStaticItemsSized<Schema extends XSchemaLike[], Result extends unknown[] = []> =
9494
Schema extends [infer Left extends XSchemaLike, ...infer Right extends XSchemaLike[]]
95-
? StaticXItemsSized<Right, [...Result, XStatic<Left>]>
95+
? XStaticItemsSized<Right, [...Result, XStaticType<Left>]>
9696
: Result
97-
type StaticXItems<Schemas extends XSchemaLike[] | XSchemaLike,
97+
type XStaticItems<Schemas extends XSchemaLike[] | XSchemaLike,
9898
Result extends unknown = (
99-
Schemas extends XSchemaLike[] ? StaticXItemsSized<[...Schemas]> :
100-
Schemas extends XSchemaLike ? StaticXItemsUnsized<Schemas> :
99+
Schemas extends XSchemaLike[] ? XStaticItemsSized<[...Schemas]> :
100+
Schemas extends XSchemaLike ? XStaticItemsUnsized<Schemas> :
101101
never
102102
)
103103
> = Result
104104
// ------------------------------------------------------------------
105105
// XOneOf
106106
// ------------------------------------------------------------------
107-
type StaticXOneOf<Schemas extends XSchemaLike[], Result extends unknown = never> =
107+
type XStaticOneOf<Schemas extends XSchemaLike[], Result extends unknown = never> =
108108
Schemas extends [infer Left extends XSchemaLike, ...infer Right extends XSchemaLike[]]
109-
? StaticXOneOf<Right, XStatic<Left> | Result>
109+
? XStaticOneOf<Right, XStaticType<Left> | Result>
110110
: Result
111111
// ------------------------------------------------------------------
112112
// XPatternProperties
113113
// ------------------------------------------------------------------
114-
type StaticXPatternProperties<Properties extends Record<PropertyKey, XSchemaLike> = Record<PropertyKey, XSchemaLike>,
115-
InferredProperties extends Record<PropertyKey, unknown> = { [Key in keyof Properties]: XStatic<Properties[Key]> },
114+
type XStaticPatternProperties<Properties extends Record<PropertyKey, XSchemaLike> = Record<PropertyKey, XSchemaLike>,
115+
InferredProperties extends Record<PropertyKey, unknown> = { [Key in keyof Properties]: XStaticType<Properties[Key]> },
116116
EvaluatedProperties extends unknown = { [key: string]: InferredProperties[keyof InferredProperties] }
117117
> = EvaluatedProperties
118118
// ------------------------------------------------------------------
119119
// XPrefixItems
120120
// ------------------------------------------------------------------
121-
type StaticXPrefixItems<Schemas extends XSchemaLike[], Result extends unknown[] = []> =
121+
type XStaticPrefixItems<Schemas extends XSchemaLike[], Result extends unknown[] = []> =
122122
Schemas extends [infer Left extends XSchemaLike, ...infer Right extends XSchemaLike[]]
123-
? StaticXPrefixItems<Right, [...Result, XStatic<Left>]>
123+
? XStaticPrefixItems<Right, [...Result, XStaticType<Left>]>
124124
: Result
125125
// ------------------------------------------------------------------
126126
// XProperties
127127
// ------------------------------------------------------------------
128-
type StaticXProperties<Properties extends Record<PropertyKey, XSchemaLike>,
128+
type XStaticProperties<Properties extends Record<PropertyKey, XSchemaLike>,
129129
Readonly extends Record<PropertyKey, unknown> = {
130-
readonly [Key in keyof Properties as Properties[Key] extends { readOnly: true } ? Key : never]?: XStatic<Properties[Key]>
130+
readonly [Key in keyof Properties as Properties[Key] extends { readOnly: true } ? Key : never]?: XStaticType<Properties[Key]>
131131
},
132132
Standard extends Record<PropertyKey, unknown> = {
133-
[Key in keyof Properties as Properties[Key] extends { readOnly: true } ? never : Key]?: XStatic<Properties[Key]>
133+
[Key in keyof Properties as Properties[Key] extends { readOnly: true } ? never : Key]?: XStaticType<Properties[Key]>
134134
},
135135
Result extends Record<PropertyKey, unknown> = Readonly & Standard
136136
> = Result
@@ -140,8 +140,8 @@ type StaticXProperties<Properties extends Record<PropertyKey, XSchemaLike>,
140140
type RequiredSelectProperty<Properties extends Record<PropertyKey, XSchemaLike>, Key extends string, Result extends Record<PropertyKey, unknown> = (
141141
Key extends keyof Properties
142142
? Properties[Key] extends { readOnly: true }
143-
? { readonly [_ in Key]: XStatic<Properties[Key]> }
144-
: { [_ in Key]: XStatic<Properties[Key]> }
143+
? { readonly [_ in Key]: XStaticType<Properties[Key]> }
144+
: { [_ in Key]: XStaticType<Properties[Key]> }
145145
: { [_ in Key]: unknown }
146146
)> = Result
147147
type RequiredSelectProperties<Properties extends Record<PropertyKey, XSchemaLike>, Keys extends string[], Result extends Record<PropertyKey, unknown> = {}> =
@@ -153,27 +153,25 @@ type RequiredGetProperties<Schema extends XSchemaLike, Result extends Record<Pro
153153
? Properties
154154
: {}
155155
)> = Result
156-
type StaticXRequired<Schema extends XSchemaLike, Keys extends string[],
156+
type XStaticRequired<Schema extends XSchemaLike, Keys extends string[],
157157
Properties extends Record<PropertyKey, XSchemaLike> = RequiredGetProperties<Schema>,
158158
Result extends Record<PropertyKey, unknown> = RequiredSelectProperties<Properties, Keys>
159159
> = Result
160160
// ------------------------------------------------------------------
161161
// XStaticKeywords
162-
//
163-
// Returns a Tuple where each element is an inferred Keyword.
164-
// ------------------------------------------------------------------
165-
type StaticXKeywords<Schema, Result extends unknown[] = [
166-
Schema extends XAdditionalProperties<infer Type extends XSchemaLike> ? StaticXAdditionalProperties<Type> : unknown,
167-
Schema extends XAllOf<infer Types extends XSchemaLike[]> ? StaticXAllOf<Types> : unknown,
168-
Schema extends XAnyOf<infer Types extends XSchemaLike[]> ? StaticXAnyOf<Types> : unknown,
169-
Schema extends XConst<infer Value extends unknown> ? StaticXConst<Value> : unknown,
170-
Schema extends XEnum<infer Values extends unknown[]> ? StaticXEnum<Values> : unknown,
171-
Schema extends XItems<infer Types extends XSchemaLike[] | XSchemaLike> ? StaticXItems<Types> : unknown,
172-
Schema extends XOneOf<infer Types extends XSchemaLike[]> ? StaticXOneOf<Types> : unknown,
173-
Schema extends XPatternProperties<infer Properties extends Record<PropertyKey, XSchemaLike>> ? StaticXPatternProperties<Properties> : unknown,
174-
Schema extends XPrefixItems<infer Types extends XSchemaLike[]> ? StaticXPrefixItems<Types> : unknown,
175-
Schema extends XProperties<infer Properties extends Record<PropertyKey, XSchemaLike>> ? StaticXProperties<Properties> : unknown,
176-
Schema extends XRequired<infer Keys extends string[]> ? StaticXRequired<Schema, Keys> : unknown,
162+
// ------------------------------------------------------------------
163+
type XStaticKeywords<Schema, Result extends unknown[] = [
164+
Schema extends XAdditionalProperties<infer Type extends XSchemaLike> ? XStaticAdditionalProperties<Type> : unknown,
165+
Schema extends XAllOf<infer Types extends XSchemaLike[]> ? XStaticAllOf<Types> : unknown,
166+
Schema extends XAnyOf<infer Types extends XSchemaLike[]> ? XStaticAnyOf<Types> : unknown,
167+
Schema extends XConst<infer Value extends unknown> ? XStaticConst<Value> : unknown,
168+
Schema extends XEnum<infer Values extends unknown[]> ? XStaticEnum<Values> : unknown,
169+
Schema extends XItems<infer Types extends XSchemaLike[] | XSchemaLike> ? XStaticItems<Types> : unknown,
170+
Schema extends XOneOf<infer Types extends XSchemaLike[]> ? XStaticOneOf<Types> : unknown,
171+
Schema extends XPatternProperties<infer Properties extends Record<PropertyKey, XSchemaLike>> ? XStaticPatternProperties<Properties> : unknown,
172+
Schema extends XPrefixItems<infer Types extends XSchemaLike[]> ? XStaticPrefixItems<Types> : unknown,
173+
Schema extends XProperties<infer Properties extends Record<PropertyKey, XSchemaLike>> ? XStaticProperties<Properties> : unknown,
174+
Schema extends XRequired<infer Keys extends string[]> ? XStaticRequired<Schema, Keys> : unknown,
177175
Schema extends { type: 'array' } ? {} : unknown,
178176
Schema extends { type: 'bigint' } ? bigint : unknown,
179177
Schema extends { type: 'boolean' } ? boolean : unknown,
@@ -186,53 +184,64 @@ type StaticXKeywords<Schema, Result extends unknown[] = [
186184
Schema extends { type: 'undefined' } ? undefined : unknown,
187185
]> = Result
188186
// ------------------------------------------------------------------
189-
// StaticXReduce
187+
// XStaticKeywordIntersect
190188
// ------------------------------------------------------------------
191-
type StaticXReduce<Schemas extends unknown[], Result extends unknown = unknown> = (
189+
type XStaticKeywordIntersect<Schemas extends unknown[], Result extends unknown = unknown> = (
192190
Schemas extends [infer Left extends unknown, ...infer Right extends unknown[]]
193-
? StaticXReduce<Right, Result & Left>
191+
? XStaticKeywordIntersect<Right, Result & Left>
194192
: Result
195193
)
196194
// ------------------------------------------------------------------
197-
// XStaticNonReadonly
195+
// XStaticEvaluate
196+
// ------------------------------------------------------------------
197+
type XStaticEvaluate<Schema extends unknown,
198+
Result extends unknown = Schema extends object
199+
? { [Key in keyof Schema]: Schema[Key] }
200+
: Schema
201+
> = Result
202+
// ------------------------------------------------------------------
203+
// XStaticJsonSchema
198204
// ------------------------------------------------------------------
199-
type StaticXNonReadonlyTuple<Schemas extends readonly unknown[]> = (
205+
type XStaticJsonSchema<Schema extends unknown,
206+
Keywords extends unknown[] = XStaticKeywords<Schema>,
207+
Intersect extends unknown = XStaticKeywordIntersect<Keywords>,
208+
Result extends unknown = XStaticEvaluate<Intersect>
209+
> = Result
210+
// ------------------------------------------------------------------
211+
// XStatic
212+
// ------------------------------------------------------------------
213+
/** Statically infers a JSON Schema or Standard Schema */
214+
export type XStaticType<Schema extends XSchemaLike, Result extends unknown = (
215+
Schema extends XStandardSchemaV1<infer Validator extends XStandardValidatorV1>
216+
? XStaticStandardSchemaV1<Validator>
217+
: XStaticJsonSchema<Schema>
218+
)> = Result
219+
// ------------------------------------------------------------------
220+
// XStaticMutable: Preflight
221+
// ------------------------------------------------------------------
222+
type XStaticMutableTuple<Schemas extends readonly unknown[]> = (
200223
Schemas extends [infer Left, ...infer Right extends unknown[]]
201-
? [StaticXNonReadonly<Left>, ...StaticXNonReadonlyTuple<Right>]
224+
? [XStaticMutable<Left>, ...XStaticMutableTuple<Right>]
202225
: []
203226
)
204-
type StaticXNonReadonlyArray<Schema extends unknown,
205-
Result extends unknown[] = StaticXNonReadonly<Schema>[]
227+
type XStaticMutableArray<Schema extends unknown,
228+
Result extends unknown[] = XStaticMutable<Schema>[]
206229
> = Result
207-
type StaticXNonReadonlyObject<Schema extends object, Result extends Record<PropertyKey, unknown> = {
208-
-readonly [K in keyof Schema]: StaticXNonReadonly<Schema[K]>
230+
type XStaticMutableObject<Schema extends object, Result extends Record<PropertyKey, unknown> = {
231+
-readonly [K in keyof Schema]: XStaticMutable<Schema[K]>
209232
}> = Result
210-
type StaticXNonReadonly<Schema> = (
211-
Schema extends readonly [...infer Rest extends unknown[]] ? StaticXNonReadonlyTuple<Rest> :
212-
Schema extends readonly (infer U)[] ? StaticXNonReadonlyArray<U> :
213-
Schema extends object ? StaticXNonReadonlyObject<Schema> :
233+
type XStaticMutable<Schema> = (
234+
Schema extends XStandardSchemaV1 ? Schema : // terminate! standard-schema may be sensitive to readonly
235+
Schema extends readonly [...infer Schemas extends unknown[]] ? XStaticMutableTuple<Schemas> :
236+
Schema extends readonly (infer Schema)[] ? XStaticMutableArray<Schema> :
237+
Schema extends object ? XStaticMutableObject<Schema> :
214238
Schema
215239
)
216240
// ------------------------------------------------------------------
217-
// StaticXEvaluate
218-
// ------------------------------------------------------------------
219-
type StaticXEvaluate<Schema extends unknown,
220-
Result extends unknown = Schema extends object
221-
? { [Key in keyof Schema]: Schema[Key] }
222-
: Schema
223-
> = Result
224-
type StaticXSchema<Schema extends unknown,
225-
NonReadonly extends unknown = StaticXNonReadonly<Schema>,
226-
Keywords extends unknown[] = StaticXKeywords<NonReadonly>,
227-
Reduced extends unknown = StaticXReduce<Keywords>,
228-
Result extends unknown = StaticXEvaluate<Reduced>
229-
> = Result
230-
// ------------------------------------------------------------------
231241
// XStatic
232242
// ------------------------------------------------------------------
233243
/** Statically infers a JSON Schema or Standard Schema */
234-
export type XStatic<Schema extends XSchemaLike, Result extends unknown = (
235-
Schema extends XStandardSchemaV1<infer Validator extends XStandardValidatorV1>
236-
? StaticXStandardSchemaV1<Validator>
237-
: StaticXSchema<Schema>
238-
)> = Result
244+
export type XStatic<Schema extends XSchemaLike,
245+
Mutable extends XSchemaLike = XStaticMutable<Schema>,
246+
Result extends unknown = XStaticType<Mutable>
247+
> = Result

0 commit comments

Comments
 (0)