Repository URL to install this package:
Version:
1.2.16 ▾
|
const { CoerceChain } = require('chaintools/CoerceChain')
function coerceMyArguments(fixed) {}
function coerceMyObjArgument(obj) {}
const coerceArgs = CoerceChain(coerceMyArguments)
.default('1.234')
.coerceUnless(isFixed)
.coercer(toFixed)
const coerceObj = CoerceChain(coerceMyObjArgument)
const customToArray = x => {
if (isArray(x)) return x
else if (isObj(x)) return objToArray(x)
else if (isMap(x)) return mapToObj(x)
else if (isString(x)) return x.split('')
else if (isNumber(x)) return [x]
else return []
}
const coerceArgs2 = CoerceChain(coerceMyArguments).rules([
{
default: '1.234',
unless: isFixed,
coercer: toFixed,
},
{
default: true,
unless: isBoolean,
coercer: toBoolean,
},
{
// 1. checks if unless property is there
// 2. if not, gets type of default
// 3. gets `unless` type check
// 4. gets `coercer` for the type
default: 'stringy',
},
{
coercer: customToArray,
},
// defaults the value to the value, shorthand of object
Number(1),
])
// @TODO string schema requires I re-fork my validator and swap onInvalid for coersion - only if required
const coerceObj2 = CoerceChain(coerceMyObjArgument).schema({
name: '?String',
eh: {
canada: Array,
count: Number(1),
},
})