Skip to content

Commit

Permalink
fix: schemaOf handles Dates
Browse files Browse the repository at this point in the history
  • Loading branch information
jquense committed Feb 12, 2021
1 parent 0f12920 commit c1fc816
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ type ObjectSchemaOf<T extends AnyObject> = ObjectSchema<
{
[k in keyof T]-?: T[k] extends Array<infer E>
? ArraySchema<SchemaOf<E> | Lazy<SchemaOf<E>>>
: T[k] extends Date
? DateSchema<T[k]>
: T[k] extends AnyObject
? // we can't use ObjectSchema<{ []: SchemaOf<T[k]> }> b/c TS produces a union of two schema
ObjectSchemaOf<T[k] | Lazy<T[k]>>
Expand All @@ -49,6 +51,8 @@ type ObjectSchemaOf<T extends AnyObject> = ObjectSchema<

type SchemaOf<T> = T extends Array<infer E>
? ArraySchema<SchemaOf<E> | Lazy<SchemaOf<E>>>
: T extends Date
? DateSchema<T>
: T extends AnyObject
? ObjectSchemaOf<T>
: BaseSchema<T, Config>;
Expand Down
3 changes: 3 additions & 0 deletions test/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
ref,
lazy,
SchemaOf,
date,
} from '../src';
import type {
AssertsShape,
Expand Down Expand Up @@ -220,6 +221,7 @@ SchemaOf: {
title: string | undefined;
age?: number;
colors: string[];
createdAt: Date;
};

type PersonSchema = SchemaOf<Person>;
Expand All @@ -229,6 +231,7 @@ SchemaOf: {
title: string(),
age: number(),
colors: array(string().defined()),
createdAt: date().defined(),
});
}

Expand Down

0 comments on commit c1fc816

Please sign in to comment.