Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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>;
- });
- }
Add Comment
Please, Sign In to add comment