default

default(value, [strict=false], [ctx])

作用

设置当被检测值为undefined | null时,validatevalidateSilent方法的返回值。

若为严格模式(strict=true), 则设置被检测值为undefined | null | []时的默认返回值。

参数

  • value (*) - validatevalidateSilent默认值。推荐但不必须为array类型。当value为函数时,则设置函数的返回值为默认值,函数有一个参数:被检测原始值——undefined, null[].

  • [strict] (boolean) - 是否开启严格模式,默认为false不开启。若明确设置strict=true则开启严格模式。

  • [ctx] (*) - 当value为函数时的执行上下文this

注意

强烈建议使用函数返回默认数组,避免多次调用validatevalidateSilent得到的返回值为同一个引用数组。

示例

const schema1 = racoon.array().default(
  () => [1]
);
schema1.validate(undefined); // pass, 返回 [1]
schema1.validate(null); // pass, 返回 [1]
schema1.validate([]); // pass, 返回 []

const schema2 = racoon().array().default(
  () => [1],
  true
);
schema2.validate(undefined); // pass, 返回 [1]
schema2.validate(null); // pass, 返回 [1]
schema2.validate([]); // pass, 返回 [1]

最后更新于