Module
x/value_schema/docs/migration/v3-to-v4.md
simple, easy-to-use, and declarative input validator; supports Node.js, TypeScript, Deno, and Bun
Latest
v3 to v4
{ifUndefined: undefined}
behavior of In v3, {ifUndefined: undefined}
is equivalent to {}
.
Both of them cause error if input value is undefined
.
In v4, the former accepts undefined
(and keeps it as-is), the latter doesn’t.
import vs from "value-schema";
const input = {};
// The below code throws an error in v3, but outputs `{foo: undefined}` in v4.
{
const output = vs.applySchemaObject({
foo: vs.number({
ifUndefined: undefined,
}),
}, input);
console.log(output);
}
// The below code throws an error in both versions.
{
const output = vs.applySchemaObject({
foo: vs.number({}),
}, input);
console.log(output);
}
how to migrate
- Replace
{ifUndefined: undefined}
with{}
.
ValueSchemaError.prototype.cause
to ValueSchemaError.prototype.rule
rename Error.prototype.cause
has come in ES2022.
It conflicts ValueSchemaError.prototype.cause
, therefore ValueSchemaError.prototype.cause
has been renamed to ValueSchemaError.prototype.rule
, that means “the rule that input value didn’t satisfy”.
In addition, CAUSE.foo
has been renamed to RULE.foo
.
// v3
import vs from "value-schema";
try {
const input = {};
const output = vs.applySchemaObject({
foo: vs.number(),
}, input);
}
catch (err) { // ValueSchemaError
if (err.cause === vs.CAUSE.UNDEFINED) {
console.error("undefined!");
}
}
// v4
import vs from "value-schema";
try {
const input = {};
const output = vs.applySchemaObject({
foo: vs.number(),
}, input);
}
catch (err) { // ValueSchemaError
if (err.rule === vs.RULE.UNDEFINED) { // cause -> rule
console.error("undefined!");
}
}
how to migrate
- Replace
err.cause
witherr.rule
. - Replace
vs.CAUSE
withvs.RULE
.
converter()
to transform()
rename Option converter
has been renamed to transform
.
Just a rename, so input parameters and return values are not changed.
how to migrate
- Replace
converter
withtransform
. - Replace
vs.RULE.CONVERTER
withvs.RULE.TRANSFORM
.