daily pastebin goal
4%
SHARE
TWEET

Untitled

a guest Apr 16th, 2018 51 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. type SchemaEntry<A, B extends keyof A> =
  2.  
  3.   | Validator<A[B]>
  4.  
  5.   | ((obj: Id<A>) => Validator<A[B]>);
  6.  
  7. type Schema<A extends object> = { [k in keyof A]: SchemaEntry<A, k> };
  8.  
  9. class Result<T> {}
  10.  
  11. export class Validator<A> {
  12.  
  13.   constructor(
  14.  
  15.     private _validate: (
  16.  
  17.       obj: A,
  18.  
  19.       schema: A extends object ? Schema<A> : undefined
  20.  
  21.     ) => Result<A>
  22.  
  23.   ) {}
  24.  
  25. }
  26.  
  27.  
  28. function validator<A extends object>(
  29.   v:(object: A, schema: Schema<A>) => Result<A>
  30. ) :Validator<A>
  31. function validator<A>(
  32.   v:(object: A) => Result<A>
  33. ) :Validator<A>
  34. function validator<A>(
  35.   v:(object: A, schema: any) => Result<A>
  36. ) {
  37.  
  38.   return new Validator<A>(v);
  39.  
  40. }
  41.  
  42.  
  43. const object = <A extends object>(schema: Schema<A>) => {
  44.  
  45.   return validator<A>((o, s) => {
  46.  
  47.     // option 1
  48.  
  49.     for (const k in <Schema<A>>s) {
  50.  
  51.       if (s.hasOwnProperty(k)) {
  52.  
  53.         const v = s[k]; // ok
  54.  
  55.       }
  56.  
  57.     }
  58.  
  59.  
  60.  
  61.     // option 2
  62.  
  63.     for (const [k, v] of Object.entries(s)) {
  64.  
  65.       // v has expected type
  66.  
  67.     }
  68.     return  null as any as Result<A>;
  69.   });
  70.  
  71. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top