15 KiB
Migration guide for @sapphire/shapeshift v3.x to v4.x
Introduction
In version 4.x of @sapphire/shapeshift we have changed the entire API to support supplying custom messages for your errors in order to allow for better localization and customization of the error messages. This guide will help you migrate your code from v3.x to v4.x.
Before we start we should note that with the amount of changes that have been made it is entirely possible that we have forgot to mention something in this guide. We therefore strongly recommend the usage of some form of type checking (i.e. through TypeScript) for your code base to ensure that all your code is compliant and correctly migrated. If you find that we have failed to mention anything, we would also greatly appreciate a Pull Request to update this document.
Function changes
In general the biggest change between v3.x and v4.x is that where before you would write validators like this:
import { s } from '@sapphire/shapeshift';
const validator = s.string;
Wherein string would be a getter for our StringValidator class, you would now write validators like this:
import { s } from '@sapphire/shapeshift';
const validator = s.string();
This is to allow for passing arguments to the method, currently only a custom message but potentially more in the
future. These kind of methods accept a single parameter which is an object with a message key. This message represents
the custom message that will be available on the error object when the validation fails.
An example of this would be:
import { s } from '@sapphire/shapeshift';
const validator = s.string({ message: 'This is a custom message' });
For a (non-exhaustive) list of all the methods there we changed please see List of function changes below.
Type only changes
Three types were removed that were previously marked as @deprecated in v3.x. We took the oppertunity of the major
release to remove these deprecated types. For a list of all the types that were removed please see List of Type Only
changes below.
References
List of function changes
s.anyis nows.any()to allow for custom options as argument.s.array(T).lengthEqualis nows.array(T).lengthEqual()to allow for custom options as argument.s.array(T).lengthGreaterThanis nows.array(T).lengthGreaterThan()to allow for custom options as argument.s.array(T).lengthGreaterThanOrEqualis nows.array(T).lengthGreaterThanOrEqual()to allow for custom options as argument.s.array(T).lengthLessThanis nows.array(T).lengthLessThan()to allow for custom options as argument.s.array(T).lengthLessThanOrEqualis nows.array(T).lengthLessThanOrEqual()to allow for custom options as argument.s.array(T).lengthNotEqualis nows.array(T).lengthNotEqual()to allow for custom options as argument.s.array(T).lengthRangeis nows.array(T).lengthRange()to allow for custom options as argument.s.array(T).lengthRangeExclusiveis nows.array(T).lengthRangeExclusive()to allow for custom options as argument.s.array(T).lengthRangeInclusiveis nows.array(T).lengthRangeInclusive()to allow for custom options as argument.s.array(T).uniqueis nows.array(T).unique()to allow for custom options as argument.s.arrayis nows.array()to allow for custom options as argument.s.bigint.divisibleByis nows.bigint().divisibleBy()to allow for custom options as argument.s.bigint.equalis nows.bigint().equal()to allow for custom options as argument.s.bigint.greaterThanis nows.bigint().greaterThan()to allow for custom options as argument.s.bigint.greaterThanOrEqualis nows.bigint().greaterThanOrEqual()to allow for custom options as argument.s.bigint.lessThanis nows.bigint().lessThan()to allow for custom options as argument.s.bigint.lessThanOrEqualis nows.bigint().lessThanOrEqual()to allow for custom options as argument.s.bigint.notEqualis nows.bigint().notEqual()to allow for custom options as argument.s.bigint().absis nows.bigint().abs()to allow for custom options as second argument.s.bigint().negativeis nows.bigint().negative()to allow for custom options as second argument.s.bigint().positiveis nows.bigint().positive()to allow for custom options as second argument.s.bigintis nows.bigint()to allow for custom options as argument.s.bigInt64Arrayis nows.bigInt64Array()to allow for custom options as argument.s.bigUint64Arrayis nows.bigUint64Array()to allow for custom options as argument.s.boolean.falseis nows.boolean().false()to allow for custom options as second argument.s.boolean.trueis nows.boolean().true()to allow for custom options as second argument.s.booleanis nows.boolean()to allow for custom options as argument.s.default(...)now gets a second parameter to allow for custom options as argument.s.default(...).default(...)now gets a second parameter to allow for custom options as argument.s.date.equalis nows.date().equal()to allow for custom options as argument.s.date.greaterThanis nows.date().greaterThan()to allow for custom options as argument.s.date.greaterThanOrEqualis nows.date().greaterThanOrEqual()to allow for custom options as argument.s.date.invalidis nows.date().invalid()to allow for custom options as argument.s.date.lessThanis nows.date().lessThan()to allow for custom options as argument.s.date.lessThanOrEqualis nows.date().lessThanOrEqual()to allow for custom options as argument.s.date.notEqualis nows.date().notEqual()to allow for custom options as argument.s.date.validis nows.date().valid()to allow for custom options as argument.s.dateis nows.date()to allow for custom options as argument.s.enum(1, 2, 3)is nows.enum([1, 2, 3])to allow for custom options as second argument.s.float32Arrayis nows.float32Array()to allow for custom options as argument.s.float64Arrayis nows.float64Array()to allow for custom options as argument.s.int16Arrayis nows.int16Array()to allow for custom options as argument.s.int32Arrayis nows.int32Array()to allow for custom options as argument.s.int8Arrayis nows.int8Array()to allow for custom options as argument.s.neveris nows.never()to allow for custom options as argument.s.nullis nows.null()to allow for custom options as argument.s.nullableis nows.nullable()to allow for custom options as argument.s.nullishis nows.nullish()to allow for custom options as argument.s.nullishis nows.nullish()to allow for custom options as argument.s.number.absis nows.number().abs()to allow for custom options as argument.s.number.ceilis nows.number().ceil()to allow for custom options as argument.s.number.divisibleByis nows.number().divisibleBy()to allow for custom options as argument.s.number.equalis nows.number().equal()to allow for custom options as argument.s.number.finiteis nows.number().finite()to allow for custom options as argument.s.number.flooris nows.number().floor()to allow for custom options as argument.s.number.froundis nows.number().fround()to allow for custom options as argument.s.number.greaterThanis nows.number().greaterThan()to allow for custom options as argument.s.number.greaterThanOrEqualis nows.number().greaterThanOrEqual()to allow for custom options as argument.s.number.intis nows.number().int()to allow for custom options as argument.s.number.lessThanis nows.number().lessThan()to allow for custom options as argument.s.number.lessThanOrEqualis nows.number().lessThanOrEqual()to allow for custom options as argument.s.number.negativeis nows.number().negative()to allow for custom options as argument.s.number.notEqualis nows.number().notEqual()to allow for custom options as argument.s.number.positiveis nows.number().positive()to allow for custom options as argument.s.number.roundis nows.number().round()to allow for custom options as argument.s.number.safeIntis nows.number().safeInt()to allow for custom options as argument.s.number.signis nows.number().sign()to allow for custom options as argument.s.number.truncis nows.number().trunc()to allow for custom options as argument.s.numberis nows.number()to allow for custom options as argument.s.object.ignoreis nows.object().ignore()to allow for custom options as argument.s.object.partialis nows.object().partial()to allow for custom options as argument.s.object.passthroughis nows.object().passthrough()to allow for custom options as argument.s.object.requiredis nows.object().required()to allow for custom options as argument.s.object.strictis nows.object().strict()to allow for custom options as argument.s.optionalis nows.optional()to allow for custom options as argument.s.required(...)now gets a second parameter to allow for custom options as argument.s.setis nows.set()to allow for custom options as argument.s.string.dateis nows.string().date()to allow for custom options as argument.s.string.emailis nows.string().email()to allow for custom options as argument.s.string.ipv4is nows.string().ipv4()to allow for custom options as argument.s.string.ipv6is nows.string().ipv6()to allow for custom options as argument.s.string().ipis nows.string().ip()to allow for custom options as argument.s.string().lengthEqualis nows.string().lengthEqual()to allow for custom options as argument.s.string().lengthGreaterThanis nows.string().lengthGreaterThan()to allow for custom options as argument.s.string().lengthGreaterThanOrEqualis nows.string().lengthGreaterThanOrEqual()to allow for custom options as argument.s.string().lengthLessThanis nows.string().lengthLessThan()to allow for custom options as argument.s.string().lengthLessThanOrEqualis nows.string().lengthLessThanOrEqual()to allow for custom options as argument.s.string().lengthNotEqualis nows.string().lengthNotEqual()to allow for custom options as argument.s.string().phoneis nows.string().phone()to allow for custom options as argument.s.string().regexis nows.string().regex()to allow for custom options as argument.s.string().urlis nows.string().url()to allow for custom options as argument.s.stringis nows.string()to allow for custom options as argument.s.tuple(1, 2, 3)is nows.tuple([1, 2, 3])to allow for custom options as second argument.s.typedArray(T).byteLengthEqualis nows.typedArray(T).byteLengthEqual()to allow for custom options as argument.s.typedArray(T).byteLengthGreaterThanis nows.typedArray(T).byteLengthGreaterThan()to allow for custom options as argument.s.typedArray(T).byteLengthGreaterThanOrEqualis nows.typedArray(T).byteLengthGreaterThanOrEqual()to allow for custom options as argument.s.typedArray(T).byteLengthLessThanis nows.typedArray(T).byteLengthLessThan()to allow for custom options as argument.s.typedArray(T).byteLengthLessThanOrEqualis nows.typedArray(T).byteLengthLessThanOrEqual()to allow for custom options as argument.s.typedArray(T).byteLengthNotEqualis nows.typedArray(T).byteLengthNotEqual()to allow for custom options as argument.s.typedArray(T).byteLengthRangeis nows.typedArray(T).byteLengthRange()to allow for custom options as argument.s.typedArray(T).byteLengthRangeExclusiveis nows.typedArray(T).byteLengthRangeExclusive()to allow for custom options as argument.s.typedArray(T).byteLengthRangeInclusiveis nows.typedArray(T).byteLengthRangeInclusive()to allow for custom options as argument.s.typedArray(T).lengthEqualis nows.typedArray(T).lengthEqual()to allow for custom options as argument.s.typedArray(T).lengthGreaterThanis nows.typedArray(T).lengthGreaterThan()to allow for custom options as argument.s.typedArray(T).lengthGreaterThanOrEqualis nows.typedArray(T).lengthGreaterThanOrEqual()to allow for custom options as argument.s.typedArray(T).lengthLessThanis nows.typedArray(T).lengthLessThan()to allow for custom options as argument.s.typedArray(T).lengthLessThanOrEqualis nows.typedArray(T).lengthLessThanOrEqual()to allow for custom options as argument.s.typedArray(T).lengthNotEqualis nows.typedArray(T).lengthNotEqual()to allow for custom options as argument.s.typedArray(T).lengthRangeis nows.typedArray(T).lengthRange()to allow for custom options as argument.s.typedArray(T).lengthRangeExclusiveis nows.typedArray(T).lengthRangeExclusive()to allow for custom options as argument.s.typedArray(T).lengthRangeInclusiveis nows.typedArray(T).lengthRangeInclusive()to allow for custom options as argument.s.uint16Arrayis nows.uint16Array()to allow for custom options as argument.s.uint32Arrayis nows.uint32Array()to allow for custom options as argument.s.uint8Arrayis nows.uint8Array()to allow for custom options as argument.s.uint8ClampedArrayis nows.uint8ClampedArray()to allow for custom options as argument.s.undefinedis nows.undefined()to allow for custom options as argument.s.union(1, 2, 3).requiredis nows.union(1, 2, 3).required()to allow for custom options as argument.s.union(1, 2, 3)is nows.union([1, 2, 3])to allow for custom options as second argument.s.unknownis nows.unknown()to allow for custom options as argument.uniqueArrayis now a function (instead of a constant) to allow for custom options as argument.dateInvalidis now a function (instead of a constant) to allow for custom options as argument.dateValidis now a function (instead of a constant) to allow for custom options as argument.numberFiniteis now a function (instead of a constant) to allow for custom options as argument.numberIntis now a function (instead of a constant) to allow for custom options as argument.numberNaNis now a function (instead of a constant) to allow for custom options as argument.numberNotNaNis now a function (instead of a constant) to allow for custom options as argument.numberSafeIntis now a function (instead of a constant) to allow for custom options as argument.
List of Type Only changes
PickDefinedutility type has been removed.PickUndefinedMakeOptionalutility type has been removed.NonNullObjectutility type has been removed.