Skip to content

Commit 2f1b54a

Browse files
committed
Prototype: FromSchema
1 parent b2eb0de commit 2f1b54a

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

example/prototypes/from-schema.ts

+14
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ const IsSTuple = (value: unknown): value is STuple => Type.ValueGuard.IsObject(v
4141
const IsSArray = (value: unknown): value is SArray => Type.ValueGuard.IsObject(value) && IsExact(value.type, 'array') && !Type.ValueGuard.IsArray(value.items) && Type.ValueGuard.IsObject(value.items)
4242
const IsSConst = (value: unknown): value is SConst => Type.ValueGuard.IsObject(value) && Type.ValueGuard.IsObject(value['const'])
4343
const IsSString = (value: unknown): value is SString => Type.ValueGuard.IsObject(value) && IsExact(value.type, 'string')
44+
const IsSRef = (value: unknown): value is SRef => Type.ValueGuard.IsObject(value) && Type.ValueGuard.IsString(value.$ref)
4445
const IsSNumber = (value: unknown): value is SNumber => Type.ValueGuard.IsObject(value) && IsExact(value.type, 'number')
4546
const IsSInteger = (value: unknown): value is SInteger => Type.ValueGuard.IsObject(value) && IsExact(value.type, 'integer')
4647
const IsSBoolean = (value: unknown): value is SBoolean => Type.ValueGuard.IsObject(value) && IsExact(value.type, 'boolean')
@@ -58,6 +59,7 @@ type SObject = Readonly<{ type: 'object'; properties: SProperties; required?: re
5859
type STuple = Readonly<{ type: 'array'; items: readonly unknown[] }>
5960
type SArray = Readonly<{ type: 'array'; items: unknown }>
6061
type SConst = Readonly<{ const: SValue }>
62+
type SRef = Readonly<{ $ref: string }>
6163
type SString = Readonly<{ type: 'string' }>
6264
type SNumber = Readonly<{ type: 'number' }>
6365
type SInteger = Readonly<{ type: 'integer' }>
@@ -174,6 +176,16 @@ function FromConst<T extends SConst>(T: T) {
174176
return Type.Literal(T.const, T)
175177
}
176178
// ------------------------------------------------------------------
179+
// Ref
180+
// ------------------------------------------------------------------
181+
// prettier-ignore
182+
type TFromRef<T extends SRef> = (
183+
Type.Ensure<Type.TRef<T['$ref']>>
184+
)
185+
function FromRef<T extends SRef>(T: T) {
186+
return Type.Ref(T['$ref'])
187+
}
188+
// ------------------------------------------------------------------
177189
// Object
178190
// ------------------------------------------------------------------
179191
type TFromPropertiesIsOptional<K extends PropertyKey, R extends string | unknown> = unknown extends R ? true : K extends R ? false : true
@@ -208,6 +220,7 @@ export type TFromSchema<T> = (
208220
T extends STuple ? TFromTuple<T> :
209221
T extends SArray ? TFromArray<T> :
210222
T extends SConst ? TFromConst<T> :
223+
T extends SRef ? TFromRef<T> :
211224
T extends SString ? Type.TString :
212225
T extends SNumber ? Type.TNumber :
213226
T extends SInteger ? Type.TInteger :
@@ -227,6 +240,7 @@ export function FromSchema<T>(T: T): TFromSchema<T> {
227240
IsSTuple(T) ? FromTuple(T) :
228241
IsSArray(T) ? FromArray(T) :
229242
IsSConst(T) ? FromConst(T) :
243+
IsSRef(T) ? FromRef(T) :
230244
IsSString(T) ? Type.String(T) :
231245
IsSNumber(T) ? Type.Number(T) :
232246
IsSInteger(T) ? Type.Integer(T) :

0 commit comments

Comments
 (0)