Created
April 8, 2018 19:22
-
-
Save dragomirtitian/a1586cfb70785f7b7da0ef483dca12cd to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type SchemaEntry<A, B extends keyof A> = | |
| Validator<A[B]> | |
| ((obj: Id<A>) => Validator<A[B]>); | |
type Schema<A extends object> = { [k in keyof A]: SchemaEntry<A, k> }; | |
class Result<T> {} | |
export class Validator<A> { | |
constructor( | |
private _validate: ( | |
obj: A, | |
schema: A extends object ? Schema<A> : undefined | |
) => Result<A> | |
) {} | |
} | |
function validator<A extends object>( | |
v:(object: A, schema: Schema<A>) => Result<A> | |
) :Validator<A> | |
function validator<A>( | |
v:(object: A) => Result<A> | |
) :Validator<A> | |
function validator<A>( | |
v:(object: A, schema: any) => Result<A> | |
) { | |
return new Validator<A>(v); | |
} | |
const object = <A extends object>(schema: Schema<A>) => { | |
return validator<A>((o, s) => { | |
// option 1 | |
for (const k in <Schema<A>>s) { | |
if (s.hasOwnProperty(k)) { | |
const v = s[k]; // ok | |
} | |
} | |
// option 2 | |
for (const [k, v] of Object.entries(s)) { | |
// v has expected type | |
} | |
return null as any as Result<A>; | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment