default
default(value, [strict=true], [ctx])
Effect
Set the return value of validate
and validateSilent
when the detected value is undefined | null
.
If strict mode is turned on(strict=true), set the return value when the detected value is undefined | null | {}
.
Arguments
value
(*) - The default return value ofvalidate
andvalidateSilent
. It is recommended but not necessarily to be an object. Whenvalue
is a function, then set the return value ofvalue
to be the default return value. The function has a parameter: the original detected value -undefined
,null
or''
.[strict]
(boolean) - Whether turn on strict mode, default isfalse
, means no-strict mode. You can exactly setstrict
totrue
to turn on strict mode.[ctx]
(*) - The execution context ifvalue
is a function.
ATTENTION
It is highly recommended to use a function to set the default return value, to avoid returning the same object when you call
validate
orvalidateSilent
multiple times.
Example
const schema1 = racoon.object().default(
() => ({ a: 1 })
);
schema1.validate(undefined); // pass, return: {a: 1}
schema1.validate(null); // pass, return: {a: 1}
schema1.validate({}); // pass, return: {}
const schema2 = racoon().object().default(
() => ({ a: 1 }),
true
);
schema2.validate(undefined); // pass, return: {a: 1}
schema2.validate(null); // pass, return: {a: 1}
schema2.validate({}); // pass, return: {a: 1}
Last updated
Was this helpful?