v3.0.0
Changelog: v2.1.0...v3.0.0
-
The concept of checks was replaced with generic operations. While you can implement custom operations there are three methods that simplify adding operations: checks, refinements, and alterations.
-
The plugin system was introduced. All built-in checks, such as
StringShape.length
andNumberShape.gte
, were moved to separate modules. -
Built-in plugins provide lots of new checks, for example
ArrayShape.includes
andObjectShape.allKeys
.DateShape
andBigIntShape
now have various checks as well. -
Previously, type inference was done using
INPUT
andOUTPUT
constants. They were replaced withInput
andOutput
types. -
Error codes now follow
<type>.<check>
notation, for example:number.int
,string.nonBlank
, etc. -
PromiseShape
can now be used in a sync context, if it doesn't parse the resolved value.
const shape = d.promise();
shape.coerce().parse('aaa');
// ⮕ Promise<'aaa'>
shape.isAsync;
// ⮕ false
LazyShape
can now be used to describe cyclic objects:
type Foo {
child?: Foo;
}
const parent: Foo = {};
// 👇 Cyclic objects are handled without stack overflow exceptions
parent.child = parent;
const lazyShape: d.Shape<Foo> = d.lazy(
() => d.object({
child: lazyShape
})
);
lazyShape.parse(parent);
// ⮕ parent
- Built-in type coercion can now be overridden with a custom callback. The callback passed to
corce
method is called only if the input value doesn't conform the requested type. If coercion isn't possible,d.NEVER
must be returned.
const yesNoShape = d.boolean().coerce(value => {
if (value === 'yes') {
return true;
}
if (value === 'no') {
return false;
}
// Coercion is not possible
return d.NEVER;
});
d.array(yesNoShape).parse(['yes', 'no'])
// ⮕ [true, false]
yesNoShape.parse('true')
// ❌ ValidationError: type at /: Must be a boolean
-
Shape._clone
was introduced to enable shape-specific cloning. -
Shape.check
signature was simplified to avoid the ambiguity betweenparam
andoptions
arguments. Nowparam
is the part ofCheckOptions
interface:
check<Param>(cb: CheckCallback<OutputValue, Param>, options: CheckOptions & { param: Param }): this;
check(cb: CheckCallback<OutputValue>, options?: CheckOptions): this;
- Readonly arrays of keys are now supported as an argument for in
pick
,omit
,partial
, andrequired
:
const keys = ['aaa'] as const;
d
.object({
aaa: d.string(),
bbb: d.number()
})
// 👇 No typing errors here anymore
.pick(keys)
// ⮕ Shape<{ aaa: string }>
-
FuntionShape.noWrap()
was replaced withFuntionShape.strict(options)
.FuntionShape
doesn't wrap input functions andstrinct
must be explicitly called to enable wrapping. -
ApplyOptions.verbose
was replaced withApplyOptions.earlyReturn
. Shapes now collect all issues by default, andearlyReturn: true
should be used to exit after the first issue is encountered. -
ObjectShape
now hasvalueShapes
property that holds an array of property shapes that is parallel tokeys
array. -
ObjectShape.keyof()
was replaced withObjectShape.keysShape
getter that returns the cached enum shape of known object keys.
Other changes
Removed members
Removed | Replacement |
---|---|
d.finite() |
d.number().finite() |
d.int() |
d.number().int() |
d.integer() |
d.number().int() |
BrandShape |
Shape<Branded<Value>> |
getSheck
, hasCheck
, and deleteCheck
were removed and don't have a replacement since checks were replaced with generic operations.
Renamed members
Old name | New name |
---|---|
Shape.transform |
Shape.convert |
Shape.transformAsync |
Shape.convertAsync |
CoercibleShape.isCoerced |
CoercibleShape.isCoercing |
ApplyOptions.coerced |
ApplyOptions.coerce |
FunctionShape.isWrapperAsync |
FunctionShape.isAsyncFunction |
FunctionShape.wrap |
FunctionShape.ensure |
FunctionShape.wrapAsync |
FunctionShape.ensureAsync |
ObjectShape.shapes |
ObjectShape.propShapes |
ObjectShape.shapes |
ObjectShape.propShapes |
ObjectShape.shapes |
ObjectShape.propShapes |
ObjectShape.shapes |
ObjectShape.propShapes |
KeysMode |
ObjectKeysMode |
SetShape.shape |
SetShape.valueShape |
NotShape.shape |
NotShape.baseShape |
TransformShape.callback |
ConvertShape.converter |
ReplaceShape.shape |
ReplaceShape.baseShape |
DenyShape.shape |
DenyShape.baseShape |
CatchShape.sahpe |
CatchShape.baseShape |
ExcludeShape.shape |
ExcludeShape.baseShape |
AllowLiteralShape |
AllowShape |
DenyLiteralShape |
DenyShape |
ReplaceLiteralShape |
ReplaceShape |
Literal |
Any |
ConstraintOptions |
IssueOptions |