validate

node v6.11.2
version: 44.0.0
endpointsharetweet
const isObjectOf = schema => obj => { Object.keys(schema).forEach(key => { const value = obj[key] const predicate = schema[key] try { if (predicate(value) === false) { throw new Error('invalid') } } catch(err) { throw new Error(`${key} ${err.message}`) } }) return true } const isArrayOf = predicate => arr => { arr.forEach((value, i) => { try { if (predicate(value) === false) { throw new Error('invalid') } } catch(err) { throw new Error(`[${i}] ${err.message}`) } }) } const isRequired = predicate => value => { if (value === undefined) { throw new Error('missing') } else { return predicate(value) } } const isOptional = predicate => value => { if (value === undefined) { return true } else { return predicate(value) } } const isString = isRequired(s => typeof s === 'string') const isNumber = isRequired(n => typeof n === 'number') const isMyType = isObjectOf({ foo: isOptional(isString), bar: isNumber, arr: isArrayOf(isNumber) }) isMyOtherType = isObjectOf({ baz: isString, myType: isMyType }) isMyOtherType({ baz: 'dop', myType:{ // foo: 'fo', bar: 3, arr: [3, 3] } })
Loading…

no comments

    sign in to comment