Skip to content
iamchenxin edited this page Jun 6, 2016 · 4 revisions

How to write custom validator.

assume you want to valid some name:

// custom caster . note: 
//this do not validate a literal,just a custom dynamic type you defined
function isTom(v:mixed):string {
  const str = isString(v); // appropriate parent type, Tom's name should be string
  if ( v === 'Tom' ){
    return str; // check pass -> return str
  } else {
    throw new Error('is not Tom'); // check failed -> throw
  }
}

Clone this wiki locally