Skip to content

v3.0.0

Compare
Choose a tag to compare
@github-actions github-actions released this 31 Jul 22:49
· 48 commits to next since this release

Changelog: v2.1.0...v3.0.0

const shape = d.promise();

shape.coerce().parse('aaa');
// ⮕ Promise<'aaa'>

shape.isAsync;
// ⮕ false
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 between param and options arguments. Now param is the part of CheckOptions 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, and required:
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 with FuntionShape.strict(options). FuntionShape doesn't wrap input functions and strinct must be explicitly called to enable wrapping.

  • ApplyOptions.verbose was replaced with ApplyOptions.earlyReturn. Shapes now collect all issues by default, and earlyReturn: true should be used to exit after the first issue is encountered.

  • ObjectShape now has valueShapes property that holds an array of property shapes that is parallel to keys array.

  • ObjectShape.keyof() was replaced with ObjectShape.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