Skip to content

Commit 3da57a4

Browse files
committedAug 9, 2018
Refactor away LeafSchema and GenericSchema
1 parent 8ecbe38 commit 3da57a4

File tree

1 file changed

+11
-17
lines changed

1 file changed

+11
-17
lines changed
 

‎index.d.ts

+11-17
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
type AnySchema = NullSchema | BooleanSchema | NumberSchema | StringSchema | AnyEnumSchema | AnyArraySchema | AnyObjectSchema
2+
13
interface NullSchema {
24
type: 'null'
35
}
@@ -14,20 +16,19 @@ interface StringSchema {
1416
type: 'string'
1517
}
1618

17-
type LeafSchema = NullSchema | BooleanSchema | NumberSchema | StringSchema
18-
19+
interface AnyEnumSchema extends EnumSchema<any> {}
1920
interface EnumSchema<T> {
2021
enum: T[]
2122
}
2223

23-
interface ArraySchema<T extends LeafSchema | EnumSchema<any> | ArraySchema<LeafSchema | EnumSchema<any> | ObjectSchema<ObjectProps, any>> | ObjectSchema<ObjectProps, any>> {
24+
interface AnyArraySchema extends ArraySchema<AnySchema> {}
25+
interface ArraySchema<T extends AnySchema> {
2426
type: 'array'
2527
items: T
2628
}
2729

28-
type ObjectProps = { [K in string]: LeafSchema | EnumSchema<any> | ArraySchema<LeafSchema | EnumSchema<any> | ArraySchema<LeafSchema | EnumSchema<any> | ObjectSchema<ObjectProps, any>> | ObjectSchema<ObjectProps, any>> | ObjectSchema<ObjectProps, any> }
29-
30-
interface ObjectSchema<T extends ObjectProps, R extends keyof T> {
30+
interface AnyObjectSchema extends ObjectSchema<Record<string, AnySchema>, string> {}
31+
interface ObjectSchema<T extends Record<string, AnySchema>, R extends keyof T> {
3132
additionalProperties?: boolean
3233
type: 'object'
3334
properties: T
@@ -51,13 +52,6 @@ declare type ExtractSchemaType<Type> = (
5152
: never
5253
)
5354

54-
declare type GenericSchema = (
55-
{ enum: any[] } |
56-
{ type: 'string' | 'number' | 'boolean' | 'null' } |
57-
{ type: 'array', items: GenericSchema } |
58-
{ type: 'object', properties: ObjectProps }
59-
)
60-
6155
declare namespace factory {
6256
interface ValidationError {
6357
field: string
@@ -78,11 +72,11 @@ declare interface Filter<Output> {
7872
}
7973

8074
declare interface Factory {
81-
<T extends ObjectProps, R extends keyof T> (schema: ObjectSchema<T, R>, options?: any): Validator<ObjectSchema<T, R>>
82-
<T extends GenericSchema> (schema: T, options?: any): Validator<T>
75+
<T extends Record<string, AnySchema>, R extends keyof T> (schema: ObjectSchema<T, R>, options?: any): Validator<ObjectSchema<T, R>>
76+
<T extends AnySchema> (schema: T, options?: any): Validator<T>
8377

84-
createFilter<T extends ObjectProps, R extends keyof T> (schema: ObjectSchema<T, R>, options?: any): Filter<ExtractedSchemaObject<T, R>>
85-
createFilter<T extends GenericSchema> (schema: T, options?: any): Filter<ExtractSchemaType<T>>
78+
createFilter<T extends Record<string, AnySchema>, R extends keyof T> (schema: ObjectSchema<T, R>, options?: any): Filter<ExtractedSchemaObject<T, R>>
79+
createFilter<T extends AnySchema> (schema: T, options?: any): Filter<ExtractSchemaType<T>>
8680
}
8781

8882
declare const factory: Factory

0 commit comments

Comments
 (0)
Please sign in to comment.