Skip to content

smikhalevski/doubter

Repository files navigation

Doubter

Playground API Docs


Runtime validation and transformation library.

npm install --save-prod doubter

Important

Docs on the next branch describe the canary release doubter@next. Navigate to the latest branch for docs that describe the latest stable release.


🚀 Features

⏱ Performance

🍿 Comparison with peers

🎯 Data types

🍪 Cookbook

Introduction

Let's create a simple shape of a user:

import * as d from 'doubter';

const userShape = d.object({
  name: d.string(),
  age: d.number()
});
// ⮕ Shape<{ name: string, age: number }>

This is the shape of an object with two required properties "name" and "age". Shapes are the core concept in Doubter, they are validation and transformation pipelines that have an input and an output.

Apply the shape to an input value with the parse method:

userShape.parse({
  name: 'John Belushi',
  age: 30
});
// ⮕ { name: 'John Belushi', age: 30 }

If the provided value is valid, then it is returned as is. If an incorrect value is provided, then a validation error is thrown:

userShape.parse({
  name: 'Peter Parker',
  age: 'seventeen'
});
// ❌ ValidationError: type.number at /age: Must be a number

Currently, the only constraint applied to the "age" property value is that it must be a number. Let's modify the shape to check that age is an integer and that user is an adult:

  const userShape = d.object({
    name: d.string(),
-   age: d.number()
+   age: d.number().int().between(18, 100)
  });

Here we added two operations to the number shape. Operations can check, refine, and alter input values. There are lots of operations available through plugins, and you can easily add your own operation when you need a custom logic.

Now shape would not only check that the "age" is a number, but also assert that it is an integer between 18 and 100:

userShape.parse({
  name: 'Peter Parker',
  age: 16
});
// ❌ ValidationError: number.gte at /age: Must be greater than or equal to 18

If you are using TypeScript, you can infer the type of the value that the shape describes:

type User = d.Input<typeof userShape>;

const user: User = {
  name: 'Dan Aykroyd',
  age: 27
};

Read more about static type inference and runtime type introspection.

Async shapes

Most of the shapes are synchronous, but they may become asynchronous when one of the below is used:

Let's have a look at a shape that synchronously checks that an input value is a string:

const shape1 = d.string();
// ⮕ Shape<string>

shape1.isAsync // ⮕ false

If we add an async operation to the string shape, it would become asynchronous:

const shape2 = d.string().checkAsync(
  value => doAsyncCheck(value)
);
// ⮕ Shape<string>

shape2.isAsync // ⮕ true

The shape that checks that the input value is a Promise instance is synchronous, because it doesn't have to wait for the input promise to be fulfilled before ensuring that input has a proper type:

const shape3 = d.promise();
// ⮕ Shape<Promise<any>>

shape3.isAsync // ⮕ false

But if you want to check that a promise is fulfilled with a number, here when the shape becomes asynchronous:

const shape4 = d.promise(d.number());
// ⮕ Shape<Promise<number>>

shape4.isAsync // ⮕ true

Asynchronous shapes don't support synchronous parsing, and would throw an error if it is used:

shape4.parse(Promise.resolve(42));
// ❌ Error: Shape is async

shape4.parseAsync(Promise.resolve(42));
// ⮕ Promise { 42 } 

On the other hand, synchronous shapes support asynchronous parsing:

d.string().parseAsync('Mars');
// ⮕ Promise { 'Mars' } 

The shape that depends on an asynchronous shape, also becomes asynchronous:

const userShape = d.object({
  avatar: d.promise(d.instanceOf(Blob))
});
// ⮕ Shape<{ avatar: Promise<Blob> }>

userShape.isAsync // ⮕ true

Parsing and trying

All shapes can parse input values and there are several methods for that purpose. Consider a number shape:

const shape1 = d.number();
// ⮕ Shape<number>

The parse method takes an input value and returns an output value, or throws a validation error if parsing fails:

shape.parse(42);
// ⮕ 42

shape.parse('Mars');
// ❌ ValidationError: type.number at /: Must be a number

It isn't always convenient to write a try-catch blocks to handle validation errors. Use the try method in such cases:

shape.try(42);
// ⮕ { ok: true, value: 42 }

shape.try('Mars');
// ⮕ { ok: false, issues: [ … ] }

Read more about issues in Validation errors section.

Sometimes you don't care about validation errors, and want a default value to be returned if things go south. Use the parseOrDefault method for that:

shape.parseOrDefault(42);
// ⮕ 42

shape.parseOrDefault('Mars');
// ⮕ undefined

shape.parseOrDefault('Pluto', 5.3361);
// ⮕ 5.3361

If you need a fallback value for a nested shape consider using the catch method.

For asynchronous shapes there's an alternative for each of those methods: parseAsync, tryAsync, and parseOrDefaultAsync.

Methods listed in this section can be safely detached from the shape instance:

const { parseOrDefault } = d.string();

parseOrDefault('Jill');
// ⮕ 'Jill'

parseOrDefault(42);
// ⮕ undefined

All parsing methods accept options argument.

d.number().parse('42', { earlyReturn: true });
// ⮕ 42

Following options are available:

earlyReturn

If true then parsing is aborted after the first issue is encountered. Refer to Early return section for more details.

context

The custom context that can be accessed from custom check callbacks, refinement predicates, alteration callbacks, converters, and fallback functions. Refer to Parsing context section for more details.

messages

An object that maps an issue code to a default message. Refer to Override default messages section for more details.

Static type inference

Important

Static type inference feature requires TypeScript 4.1 + with enabled strictNullChecks.

Since shapes can transform values, they can have different input and output types. For example, this string shape has the same input an output:

const shape1 = d.string();
// ⮕ Shape<string>

shape1.parse('Pluto');
// ⮕ 'Pluto'

shape1.parse(undefined);
// ❌ ValidationError: type.string at /: Must be a string

Let's derive a new shape that would replace undefined input values with a default value "Mars":

const shape2 = shape1.optional('Mars');
// ⮕ Shape<string | undefined, string>

shape2.parse('Pluto');
// ⮕ 'Pluto'

// 🟡 Replaces undefined with the default value
shape2.parse(undefined);
// ⮕ 'Mars'

Infer the input and output types of shape2:

type Shape2Input = d.Input<typeof shape2>;
// ⮕ string | undefined

type Shape2Output = d.Output<typeof shape2>;
// ⮕ string

Besides static type inference, you can check at runtime what input types and literal values does the shape accept using shape introspection:

shape2.inputs;
// ⮕ [Type.STRING, undefined]

shape2.accepts(d.Type.STRING);
// ⮕ true

shape2.accepts('Mars');
// ⮕ true

shape2.accepts(42);
// ⮕ false

Validation errors

Validation errors which are thrown by parsing methods, and Err objects returned by try and tryAsync methods have the issues property which holds an array of validation issues:

const shape = d.object({ age: d.number() });
// ⮕ Shape<{ age: number }>

const result = shape.try({ age: 'seventeen' });

The result contains the Err object with the array of issues:

{
  ok: false,
  issues: [
    {
      code: 'type.number',
      path: ['age'],
      input: 'seventeen',
      message: 'Must be a number',
      param: undefined,
      meta: undefined
    }
  ]
}
code

The code of the validation issue. In the example above, "type" code refers to a failed number type check. While shapes check input value type and raise type issues, there also various operations that also may raise issues with unique codes, see the table below.

You can add a custom operation to any shape and return an issue with your custom code.

path

The object path as an array of keys, or undefined if there's no path. Keys can be strings, numbers (for example, array indices), symbols, and any other values since they can be Map keys, see d.map.

input

The input value that caused a validation issue. Note that if the shape applies type coercion, conversions, or if there are operations that transform the value, then input may contain an already transformed value.

message

The human-readable issue message. Refer to Localization section for more details.

param

The parameter value associated with the issue. For built-in checks, the parameter value depends on code, see the table below.

meta

The optional metadata associated with the issue. Refer to Annotations and metadata section for more details.


Code Caused by Param
any.deny shape.deny(x) The denied value x
any.exclude shape.exclude(…) The excluded shape
any.refine shape.refine(…) The predicate callback
array.includes d.array().includes(x) The included value x
array.min d.array().min(n) The minimum array length n
array.max d.array().max(n) The maximum array length n
bigint.min d.bigint().min(n) The minimum value n
bigint.max d.bigint().max(n) The maximum value n
date.min d.date().min(n) The minimum value n
date.max d.date().max(n) The maximum value n
number.finite d.number().finite()
number.int d.number().int()
number.gt d.number().gte(x) The minimum value x
number.lt d.number().lte(x) The maximum value x
number.gte d.number().gt(x) The exclusive minimum value x
number.lte d.number().lt(x) The exclusive maximum value x
number.multipleOf d.number().multipleOf(x) The divisor x
object.allKeys d.object().allKeys(keys) The keys array
object.notAllKeys d.object().notAllKeys(keys) The keys array
object.orKeys d.object().orKeys(keys) The keys array
object.xorKeys d.object().xorKeys(keys) The keys array
object.oxorKeys d.object().oxorKeys(keys) The keys array
object.exact d.object().exact() The array of unknown keys
object.plain d.object().plain()
set.min d.set().min(n) The minimum Set size n
set.max d.set().max(n) The maximum Set size n
string.nonBlank d.string().nonBlank()
string.min d.string().min(n) The minimum string length n
string.max d.string().max(n) The maximum string length n
string.regex d.string().regex(re) The regular expression re
string.includes d.string().includes(x) The included string x
string.startsWith d.string().startsWith(x) The substring x
string.endsWith d.string().endsWith(x) The substring x
type.array d.array()
type.bigint d.bigint()
type.boolean d.boolean()
type.const d.const(x) The expected constant value x
type.date d.date()
type.enum d.enum(…) The array of unique value
type.function d.function()
type.instanceOf d.instanceOf(Class) The class constructor Class
type.intersection d.and(…)
type.map d.map()
type.never d.never()
type.number d.number()
type.object d.object()
type.promise d.promise()
type.tuple d.tuple(…) The expected tuple length
type.set d.set()
type.string d.string()
type.symbol d.symbol()
type.union d.or(…) Issues raised by a union

Operations

Important

While operations are a powerful tool, most of the time you don't need to add operations directly. Instead, you can use the higher-level API: checks, refinements, and alterations.

Operations can check and transform the shape output value. Let's create a shape with an operation that trims an input string:

const shape1 = d.string().addOperation(value => {
  return { ok: true, value: value.trim() };
});
// ⮕ StringShape

shape1.parse('  Space  ');
// ⮕ 'Space'

Operations added via addOperation must return a Result:

  • null if the value is valid and unchanged;
  • an Ok object (as in example above) if the value was transformed;
  • an array of Issue objects if the operation has failed.

Multiple operations can be added to shape, and they are executed in the same order they were added. To access all operations that were added use the operations property.

In contrast to conversions and pipes, operations don't change the base shape. So you can mix them with other operations that belong to the prototype of the base shape:

const shape2 = d
  .string()
  .addOperation(value => {
    return { ok: true, value: value.trim() };
  })
  // 🟡 d.StringShape.prototype.min
  .min(6);

shape2.parse('  Neptune  ');
// ⮕ 'Neptune'

shape2.parse('  Moon  ');
// ❌ ValidationError: string.min at /: Must have the minimum length of 6

Operations can be parameterized. This is particularly useful if you want to reuse the operation multiple times.

const checkRegex: d.OperationCallback = (value, param) => {
  if (param.test(value)) {
    return null;
  }
  return [{ message: 'Must match ' + param }];
};

// 🟡 Pass a param when operation is added
const shape3 = d.string().addOperation(checkRegex, { param: /a/ });
// ⮕ StringShape

shape3.parse('Mars');
// ⮕ 'Mars'

shape3.parse('Venus');
// ❌ ValidationError: unknown at /: Must match /a/

Operations have access to parsing options, so you can provide a custom context to change the operation behaviour:

const shape4 = d.string().addOperation((value, param, options) => {
  return {
    ok: true,
    value: value.substring(options.context.substringStart)
  };
});
// ⮕ StringShape

shape4.parse(
  'Hello, Bill',
  {
    // 🟡 Provide the context during parsing
    context: { substringStart: 7 }
  }
);
// ⮕ 'Bill'

Operations can throw a ValidationError to notify Doubter that parsing issues occurred. While this has the same effect as returning an array of issues, it is recommended to throw a ValidationError as the last resort since catching errors has a high performance penalty.

const shape5 = d.number().addOperation(value => {
  if (value < 32) {
    throw new ValidationError([{ code: 'too_small' }]);
  }
  return null;
});

shape5.try(16);
// ⮕ { ok: false, issues: [{ code: 'too_small' }] }

Tolerance for issues

Operations are executed only if the base shape type requirements are satisfied:

const shape = d.string().addOperation(value => {
  return { ok: true, value: value.trim() };
});

// 🟡 Operation isn't executed because 42 isn't a string
shape.parse(42);
// ❌ ValidationError: type.string at /: Must be a string

For composite shapes, operations may become non-type-safe. Let's consider an object shape with an operation:

const checkUser: d.OpeationCallback = user => {
  if (user.age < user.yearsOfExperience) {
    return [{ code: 'invalid_age' }];
  }
  return null;
};

const userShape = d
  .object({
    age: d.number(),
    yearsOfExperience: d.number()
  })
  .addOperation(checkUser);
// ⮕ Shape<{ age: number, yearsOfExperience: number }>

The checkUser operation is guaranteed to receive an object, but its properties aren't guaranteed to have correct types.

Use tolerance operation option to change how the operation behaves in case there are issues caused by the shape it is added to:

"skip"

If the shape or preceding operations have raised issues, then the operation is skipped but consequent operations are still applied.

"abort"

If the shape or preceding operations have raised issues, then the operation is skipped and consequent operations aren't applied. Also, if this operation itself raises issues then consequent operations aren't applied.

"auto"

The operation is applied regardless of previously raised issues. This is the default behavior.

So to make checkUser operation type-safe, we can use "skip" or "abort".

  const userShape = d
    .object({
      age: d.number(),
      yearsOfExperience: d.number()
    })
-   .addOperation(checkUser);
+   .addOperation(checkUser, { tolerance: 'abort' });

Some shapes cannot guarantee that the input value is of the required type. For example, if any of the underlying shapes in an intersection shape have raised issues, an intersection shape itself cannot guarantee that its operations would receive the value of the expected type, so it doesn't apply any operations if there are issues.

These shapes never apply operations if an underlying shape has raised an issue:

Async operations

Operations callbacks can be asynchronous. They have the same set of arguments as synchronous alternative, by must return a promise. Consequent operations after the asynchronous operation would wait for its result:

const shape = d
  .string()
  .addAsyncOperation(async value => {
    if (await doAsyncCheck(value)) {
      return null;
    }
    return [{ code: 'kaputs' }];
  });

shape.isAsync;
// ⮕ true

shape.parseAsync('Hello');

Adding an async operation to the shape, makes shape itself async, so use parseAsync, tryAsync, or parseOrDefaultAsync.

Checks

Checks are the most common operations that allow constraining the input value beyond type assertions. For example, if you want to constrain a numeric input to be greater than or equal to 5:

const shape = d.number().check(value => {
  if (value < 5) {
    // 🟡 Return an issue, or an array of issues
    return { code: 'kaputs' };
  }
});
// ⮕ NumberShape

shape.parse(10);
// ⮕ 10

shape.parse(3);
// ❌ ValidationError: kaputs at /

A check callback receives the shape output value and must return an issue or an array of issues if the value is invalid. If the value is valid, a check callback must return null, undefined, or an empty array.

Add asynchronous checks using checkAsync. This method has the same semantics as check but returns a promise and makes the shape asynchronous.

Note

You can parameterize checks and set tolerance for issues the same way as any other operation.

Most shapes have a set of built-in checks. The check we've just implemented above is called gte (greater than equals):

d.number().gte(5);

Add as many checks as you need to the shape. You can mix built-in checks with any other custom operations, they are executed in the same order they were added.

d.string().max(4).regex(/a/).try('Pluto');

In the example above, an Err object is returned:

{
  ok: false,
  issues: [
    {
      code: 'string.max',
      path: [],
      input: 'Pluto',
      message: 'Must have the maximum length of 4',
      param: 4,
      meta: undefined
    },
    {
      code: 'string.regex',
      path: [],
      input: 'Pluto',
      message: 'Must match the pattern /a/',
      param: /a/,
      meta: undefined
    }
  ]
}

Note

You can find the list of issue codes and corresponding param values in Validation errors section.

Refinements

Refinements are operations that use a predicate callback to validate an input. For example, the shape below would raise an issue if the input string is less than six characters long.

const shape1 = d.string().refine(value => value.length > 5);
// ⮕ Shape<string>

shape1.parse('Uranus');
// ⮕ 'Uranus'

shape1.parse('Mars');
// ❌ ValidationError: any.refine at /: Must conform the predicate

Add asynchronous refinements using refineAsync. This method has the same semantics as refine but returns a promise and makes the shape asynchronous.

Use refinements to narrow the output type of the shape:

function isMarsOrPluto(value: string): value is 'Mars' | 'Pluto' {
  return value === 'Mars' || value === 'Pluto';
}

d.string().refine(isMarsOrPluto)
// ⮕ Shape<string, 'Mars' | 'Pluto'>

By default, refine raises issues which have the "any.refine" code. You can provide a custom code:

const shape2 = d.string().refine(
  isMarsOrPluto,
  {
    code: 'illegal_planet',
    message: 'Must be Mars or Pluto'
  }
);

shape2.parse('Venus');
// ❌ ValidationError: illegal_planet at /: Must be Mars or Pluto

Note

You can parameterize refinements and set tolerance for issues the same way as any other operation.

Alterations

Alterations are operations that synchronously transform the shape output value without changing its type. For example, let's consider a string shape that trims the value and then checks that it has at least 3 characters:

d.string()
  .alter(value => value.trim())
  .min(3);
// ⮕ StringShape

Add asynchronous alterations using alterAsync. This method has the same semantics as alter but returns a promise and makes the shape asynchronous.

Use any transformation library in conjunction with alternations:

d.number().alter(Math.abs).alter(Math.pow, { param: 3 });

Alteration callbacks must return the value of the same type, so consequent operations are type-safe. If you want to convert the shape output value to another type, consider using conversions.

Note

You can parameterize alterations and set tolerance for issues the same way as any other operation.

Conversions

Conversions are close relatives of alterations that also transform shape output value. The main difference from alterations is that conversions can change the shape output type. Let's consider a shape that takes a string as an input and converts it to a number:

const shape = d.string().convert(parseFloat);
// ⮕ Shape<string, number>

This shape ensures that the input value is a string and passes it to a converter callback:

shape.parse('42');
// ⮕ 42

shape.parse('seventeen');
// ⮕ NaN

Throw a ValidationError inside the callback to notify parser that the conversion cannot be successfully completed:

function toNumber(input: string): number {
  const output = parseFloat(input);

  if (isNaN(output)) {
    throw new d.ValidationError([{ code: 'nan' }]);
  }
  return output;
}

const shape = d.string().convert(toNumber);

shape.parse('42');
// ⮕ 42

shape.parse('seventeen');
// ❌ ValidationError: nan at /

Async conversions

Let's consider a synchronous conversion:

const syncShape1 = d.string().convert(
  value => 'Hello, ' + value
);
// ⮕ Shape<string>

syncShape1.isAsync // ⮕ false

syncShape1.parse('Jill');
// ⮕ 'Hello, Jill'

The converter callback receives and returns a string and so does syncShape1.

Now lets return a promise from the converter callback:

const syncShape2 = d.string().convert(
  value => Promise.resolve('Hello, ' + value)
);
// ⮕ Shape<string, Promise<string>>

syncShape2.isAsync // ⮕ false

syncShape2.parse('Jill');
// ⮕ Promise<string>

Notice that syncShape2 is asymmetric: it expects a string input and converts it to a Promise<string>. syncShape2 is still synchronous, since the converter callback synchronously wraps a value in a promise.

Now let's create an asynchronous shape using the async conversion:

const asyncShape1 = d.string().convertAsync(
  value => Promise.resolve('Hello, ' + value)
);
// ⮕ Shape<string>

// 🟡 Notice that the shape is async
asyncShape1.isAsync // ⮕ true

await asyncShape1.parseAsync('Jill');
// ⮕ Promise { 'Hello, Jill' }

Notice that asyncShape1 converts the input string value to output string but the conversion itself is asynchronous.

A shape is asynchronous if it uses asynchronous conversions. Here's an asynchronous object shape:

const asyncShape2 = d.object({
  foo: d.string().convertAsync(
    value => Promise.resolve(value)
  )
});
// ⮕ Shape<{ foo: string }>

asyncShape2.isAsync // ⮕ true

Refer to Async shapes section for more details on when shapes can become asynchronous.

Early return

By default, Doubter collects all issues during parsing. In some cases, you may want to halt parsing and raise a validation error as soon as the first issue was encountered. To do this, pass the earlyReturn option to the parsing methods.

d.string()
  .max(4)
  .regex(/a/)
  .try('Pluto', { earlyReturn: true });

This would return the Err object with only one issue:

{
  ok: false,
  issues: [
    {
      code: 'string.max',
      path: undefined,
      input: 'Pluto',
      message: 'Must have the maximum length of 4',
      param: 4,
      meta: undefined
    }
  ]
}

Annotations and metadata

Shapes and issues can be enriched with additional metadata.

Add an annotation to a shape:

const shape = d.string().annotate({ description: 'Username' });

shape.annotations;
// ⮕ { description: 'Username' }

annotate returns the clone of the shape with updated annotations. Annotations are merged when you add them:

shape.annotate({ foo: 'bar' }).annotations;
// ⮕ { description: 'Username', foo: 'bar' }

Validation issues have a meta property that you can use to store an arbitrary data.

You can pass the meta option to any built-in check and its value is assigned to the meta property of the raised validation issue.

const shape = d.number().gt(5, { meta: 'Useful data' });
// ⮕ Shape<number>

const result = shape.try(2);
// ⮕ { ok: false, issues: … }

if (!result.ok) {
  result.issues[0].meta // ⮕ 'Useful data'
}

This comes handy if you want to enhance an issue with an additional data that can be used later during issues processing. For example, during localization.

Parsing context

Inside operation callbacks, check callbacks, refinement predicates, alteration callbacks, converters, fallback functions, and message callbacks you can access options passed to the parser. The context option may store an arbitrary data, which is undefined by default.

For example, here's how you can use context to convert numbers to formatted strings:

const shape = d.number().convert(
  (value, options) => new Intl.NumberFormat(options.context.locale).format(value)
);
// ⮕ Shape<number, string>

shape.parse(
  1000,
  {
    // 🟡 Pass a context
    context: { locale: 'en-US' }
  }
);
// ⮕ '1,000'

Shape piping

With shape piping you to can pass the shape output to another shape.

d.string()
  .convert(parseFloat)
  .to(d.number().lt(5).gt(10));
// ⮕ Shape<string, number>

For example, you can validate that an input value is an instance of a class and then validate its properties using object:

class Planet {
  constructor(readonly name: string) {}
}

const shape = d.instanceOf(Planet).to(
  d.object({
    name: d.string().min(4)
  })
);

shape.parse({ name: 'Pluto' });
// ❌ ValidationError: type.instanceOf at /: Must be a class instance

shape.parse(new Planet('X'));
// ❌ ValidationError: string.min at /name: Must have the minimum length of 4

shape.parse(new Planet('Mars'));
// ⮕ Planet { name: 'Mars' }

Replace, allow, and deny a value

All shapes support replace, allow, and deny methods that change how separate literal values are processed.

Replace a value

You can replace an input value with an output value:

const shape1 = d.enum(['Mars', 'Pluto']).replace('Pluto', 'Jupiter');
// ⮕ Shape<'Mars' | 'Pluto', 'Mars' | 'Jupiter'>

shape1.parse('Mars');
// ⮕ 'Mars'

shape1.parse('Pluto');
// ⮕ 'Jupiter'

With replace you can extend possible input values:

d.const('Venus').replace('Mars', 'Uranus');
// ⮕ Shape<'Venus' | 'Mars', 'Venus' | 'Uranus'>

This would also work with non-literal input types:

d.number().replace(0, 'zero');
// ⮕ Shape<number, number | 'zero'>

replace narrows its arguments to literal type but in TypeScript type system not all values have a separate literal type. For example, there's no literal type for NaN and Infinity values. In such cases replace doesn't exclude the replaced value type from the output type:

d.enum([33, 42]).replace(NaN, 0);
// ⮕ Shape<number, 33 | 42 | 0>

Replaced values aren't processed by the underlying shape:

const shape2 = d.number().gte(3).replace(0, 'zero');
// ⮕ Shape<number | 'zero'>

shape2.parse(2);
// ❌ ValidationError: number.gte at /: Must be greater than 3

// 🟡 Notice that 0 doesn't satisfy the gte constraint
shape2.parse(0);
// ⮕ 'zero'

Allow a value

You can allow a value as both input and output:

d.const('Mars').allow('Pluto');
// ⮕ Shape<'Mars' | 'Pluto'>

allow follows exactly the same semantics as replace.

You can allow a value for a non-literal input types:

const shape = d.number().finite().allow(NaN);
// ⮕ Shape<number>

shape.parse(NaN);
// ⮕ NaN

shape.parse(Infinity);
// ❌ ValidationError: number.finite at /: Must be a finite number

Deny a value

Consider the enum shape:

const shape1 = d.enum(['Mars', 'Pluto', 'Jupiter']);
// ⮕ Shape<'Mars' | 'Pluto' | 'Jupiter'>

To remove a value from this enum you can use the deny method:

shape1.deny('Pluto');
// ⮕ Shape<'Mars' | 'Jupiter'>

Value denial works with any shape. For example, you can deny a specific number:

const shape2 = d.number().deny(42);
// ⮕ Shape<number>

shape2.parse(33);
// ⮕ 33

shape2.parse(42);
// ❌ ValidationError: any.deny at /: Must not be equal to 42

deny prohibits value for both input and output:

const shape3 = d.number().convert(value => value * 2).deny(42);
// ⮕ Shape<number>

shape3.parse(21);
// ❌ ValidationError: any.deny at /: Must not be equal to 42

Optional and non-optional

Marking a shape as optional allows undefined in both its input and output:

d.string().optional();
// ⮕ Shape<string | undefined>

You can provide a default value of any type, so it would be used as an output if input value is undefined:

d.string().optional(42);
// ⮕ Shape<string | undefined, string | 42>

You can achieve the same behaviour using a union:

d.or([
  d.string(),
  d.undefined()
]);
// ⮕ Shape<string | undefined>

Or using allow:

d.string().allow(undefined);
// ⮕ Shape<string | undefined>

You can mark any shape as non-optional which effectively denies undefined values from both input and output. For example, lets consider a union of an optional string and a number:

const shape1 = d.or([
  d.string().optional(),
  d.number()
]);
// ⮕ Shape<string | undefined | number>

shape1.parse(undefined);
// ⮕ undefined

const shape2 = shape1.nonOptional();
// ⮕ Shape<string | number>

shape2.parse(undefined);
// ❌ ValidationError: any.deny at /: Must not be equal to undefined

Nullable and nullish

Marking a shape as nullable allows null for both input and output:

d.string().nullable();
// ⮕ Shape<string | null>

You can provide a default value, so it would be used as an output if input value is null:

d.string().nullable(42);
// ⮕ Shape<string | null, string | 42>

To allow both null and undefined values use nullish:

d.string().nullish();
// ⮕ Shape<string | null | undefined>

nullish also supports the default value:

d.string().nullish(8080);
// ⮕ Shape<string | null | undefined, string | 8080>

Exclude a shape

Shape exclusions work the same way as Exclude helper type in TypeScript. When an exclusion is applied, the output value returned by the underlying shape must not conform the excluded shape.

const shape = d.enum(['Mars', 'Venus', 'Pluto']).exclude(d.const('Pluto'));
// ⮕ Shape<'Mars' | 'Venus' | 'Pluto', 'Mars' | 'Venus'>

shape.parse('Mars');
// ⮕ 'Mars'

shape.parse('Pluto');
// ❌ ValidationError: any.exclude at /: Must not conform the excluded shape

Exclusions work with any shape combinations:

d.or([d.number(), d.string()]).exclude(d.string());
// ⮕ Shape<number | string, number>

Sometimes you need an exclusion at runtime, but don't need it on the type level. For example, let's define a shape that allows any number except the [3, 5] range:

// 🟡 Note that the shape output is inferred as never
d.number().exclude(d.number().min(3).max(5));
// ⮕ Shape<number, never>

Since the excluded shape constrains the number type, the output type is inferred as never. While the excluded shape only restricts a limited range of numbers, there's no way to express this in TypeScript. So here's the workaround:

d.number().not(d.number().min(3).max(5));
// ⮕ Shape<number>

not works exactly like exclude at runtime, but it doesn't perform the exclusion on the type level.

d.enum(['Bill', 'Jill']).not(d.const('Jill'));
// ⮕ Shape<'Bill', 'Jill'>

You can also use d.not to negate an arbitrary shape.

Deep partial

All object-like shapes (objects, arrays, maps, sets, promises, etc.) can be converted to a deep partial alternative using deepPartial method:

const shape1 = d.array(
  d.object({
    name: d.string(),
    age: d.number()
  })
);
// ⮕ Shape<{ name: string, age: number }[]>

shape1.deepPartial();
// ⮕ Shape<Array<{ name?: string, age?: number } | undefined>>

Unions, intersections and lazy shapes can also be converted to deep partial:

const shape2 = d
  .or([
    d.number(),
    d.object({ name: d.string() })
  ])
  .deepPartial()
// ⮕ Shape<number | { name?: string }>

shape2.parse(42);
// ⮕ 42

shape2.parse({ name: undefined });
// ⮕ { name: undefined }

shape2.parse({ name: 'Frodo' });
// ⮕ { name: 'Frodo' }

shape2.parse({ name: 8080 });
// ❌ ValidationError: type.string at /name: Must be a string

Deep partial isn't applied to converted shapes:

const shape2 = d
  .object({
    years: d.array(d.string())
      .convert(years => years.map(parseFloat))
  })
  .deepPartial();
// ⮕ Shape<{ years?: string[] }, { years?: number[] }>

In the example above, array elements don't allow undefined even after deepPartial was applied, this happened because array is converted during parsing.

Note

You can also implement deep partial protocol in your custom shapes.

Fallback value

If issues were detected during parsing a shape can return a fallback value.

const shape1 = d.string().catch('Mars');

shape1.parse('Pluto');
// ⮕ 'Pluto'

shape1.parse(42);
// ⮕ 'Mars'

Pass a callback as a fallback value, it would be executed every time the catch clause is reached:

const shape2 = d.number().catch(Date.now);

shape2.parse(42);
// ⮕ 42

shape2.parse('Pluto');
// ⮕ 1671565311528

shape2.parse('Mars');
// ⮕ 1671565326707

Fallback functions receive an input value, an array of issues and parsing options (so you can access your custom context if needed).

d.string().catch((input, issues, options) => {
  // Return a fallback value
});

A fallback function can throw a ValidationError to indicate that a fallback value cannot be produced. Issues from this error would be incorporated in the parsing result.

const shape3 = d.object({
  name: d.string().catch(() => {
    throw new d.ValidationError([{ code: 'kaputs' }]);
  })
});

shape3.parse({ name: 47 });
// ❌ ValidationError: kaputs at /name

Branded types

In TypeScript, values are considered to be of equivalent type if they are structurally the same. For example, plain strings are assignable to one another:

function bookTicket(flightCode: string): void {
  // Booking logic
}

// 🟡 No type errors, but "Bill" isn't a flight code
bookTicket('Bill');

In some cases, it can be desirable to simulate nominal typing inside TypeScript. For instance, you may wish to write a function that only accepts an input that has been validated by Doubter. This can be achieved with branded types:

const flightCodeShape = d.string().refine(isFlightCode).brand<'flightCode'>();
// ⮕ Shape<string, Branded<string, 'flightCode'>>

type FlightCode = d.Output<typeof flightCodeShape>;

// 🟡 Note that the argument type isn't a plain string
function bookTicket(flightCode: FlightCode): void {
  // Booking logic
}

bookTicket(flightCodeShape.parse('BA2490'));
// Ok, valid flight code

bookTicket('Bill');
// ❌ Error: Expected BRAND to be flightCode

Note

Branded types don't affect the runtime result of parse. It is a static-type-only construct.

Type coercion

Type coercion is the process of converting value from one type to another (such as a string to a number, an array to a Set, and so on).

When coercion is enabled, input values are implicitly converted to the required input type whenever possible. For example, you can coerce input values to a number type:

const shape = d.number().coerce();
// ⮕ NumberShape

shape.isCoercing // ⮕ true

shape.parse([new String('8080')]);
// ⮕ 8080

shape.parse(null);
// ⮕ 0

Coercion rules differ from JavaScript so the behavior is more predictable and human-like. With Doubter, you can coerce input to the following types:

Custom type coercion

If you want to implement a custom coercion, you can use catch to handle invalid input values:

const yesNoShape = d.boolean().catch((value, issues) => {
  if (value === 'yes') {
    return true;
  }
  if (value === 'no') {
    return false;
  }
  throw new ValidationError(issues);
});

yesNoShape.parse('yes');
// ⮕ true

d.array(yesNoShape).parse([true, 'no']);
// ⮕ [true, false]

yesNoShape.parse('true');
// ❌ ValidationError: type.boolean at /: Must be a boolean

Or you can use d.convert to preprocess all input values:

const yesNoShape = d
  .convert(value => {
    if (value === 'yes') {
      return true;
    }
    if (value === 'no') {
      return false;
    }
    // Let the consequent shape handle this value
    return value;
  })
  .to(d.boolean());

yesNoShape.parse('yes');
// ⮕ true

yesNoShape.parse('true');
// ❌ ValidationError: type.boolean at /: Must be a boolean

Introspection

Doubter provides various features to introspect your shapes at runtime. Let's start by accessing a shape input types using the inputs property:

const shape1 = d.or([d.string(), d.boolean()]);
// ⮕ Shape<string | boolean>

shape1.inputs;
// ⮕ [Type.STRING, Type.BOOLEAN]

inputs array may contain literal values:

d.enum(['Mars', 42]).inputs;
// ⮕ ['Mars', 42]

Literal values are absorbed by their type in unions.

const shape2 = d.or([
  d.enum(['Uranus', 1984]),
  d.number()
]);
// ⮕ Shape<'Uranus' | number>

shape2.inputs;
// ⮕ ['Uranus', Type.NUMBER]

If inputs is an empty array, it means that the shape doesn't accept any input values, and would always raise validation issues.

const shape3 = d.and([d.number(), d.const('Mars')]);
// ⮕ Shape<never>
        
shape3.inputs;
// ⮕ []

To detect the type of the value use Type.of:

Type.of('Mars');
// ⮕ Type.STRING

Type.of(Type.NUMBER);
// ⮕ Type.NUMBER

Types returned from Type.of are a superset of types returned from the typeof operator.

Type.oftypeof
Type.OBJECT'object'
Type.ARRAY
Type.DATE
Type.PROMISE
Type.SET
Type.MAP
Type.NULL
Type.FUNCTION'function'
Type.STRING'string'
Type.SYMBOL'symbol'
Type.NUMBER'number'
Type.BIGINT'bigint'
Type.BOOLEAN'boolean'
Type.UNDEFINED'undefined'
Type.UNKNOWN

Unknown value type

Type.UNKNOWN type emerges when accepted inputs cannot be statically inferred. For example, if d.any, d.unknown, or d.convert are used:

const shape1 = d.convert(parseFloat);
// ⮕ Shape<any>

shape1.inputs;
// ⮕ [Type.UNKNOWN]

Type.UNKNOWN behaves like TypeScript's unknown.

It absorbs other types in unions:

const shape2 = d.or([d.string(), d.unknown()]);
// ⮕ Shape<unknown>

shape2.inputs;
// ⮕ [Type.UNKNOWN]

And it is erased in intersections:

const shape3 = d.and([d.string(), d.unknown()]);
// ⮕ Shape<string>

shape3.inputs;
// ⮕ [Type.STRING]

const shape4 = d.and([d.never(), d.unknown()]);
// ⮕ Shape<never>

shape4.inputs;
// ⮕ []

Check that an input is accepted

To check that the shape accepts a particular input type or value use the accepts method:

const shape1 = d.string();
// ⮕ Shape<string>

shape1.accepts(Type.STRING);
// ⮕ true

shape1.accepts('Venus');
// ⮕ true

Check that a value is accepted:

const shape2 = d.enum(['Mars', 'Venus']);
// ⮕ Shape<'Mars' | 'Venus'>

shape2.accepts('Mars');
// ⮕ true

shape2.accepts('Pluto');
// ⮕ false

// 🟡 Enum doesn't accept arbitrary strings
shape2.accepts(Type.STRING);
// ⮕ false

For example, you can check that the shape is optional by checking that it accepts undefined input value:

const shape3 = d.number().optional();
// ⮕ Shape<number | undefined>

shape3.accepts(1984);
// ⮕ true

shape3.accepts(undefined);
// ⮕ true

// 🟡 Note that null isn't accepted
shape3.accepts(null);
// ⮕ false

The fact that a shape accepts a particular input type or value, does not guarantee that it wouldn't raise a validation issue. For example, consider the pipe from d.any to d.string:

const fuzzyShape = d.any().to(d.string());
// ⮕ Shape<any, string>

fuzzyShape accepts Type.UNKNOWN because it is based on d.any:

fuzzyShape.inputs;
// ⮕ [Type.UNKNOWN]

Since fuzzyShape accepts any values, an undefined is also accepted:

fuzzyShape.accepts(undefined);
// ⮕ true

But parsing undefined with fuzzyShape would produce an error, since undefined doesn't satisfy d.string on the right-hand side of the pipe:

fuzzyShape.parse(undefined);
// ❌ ValidationError: type.string at /: Must be a string

Nested shapes

Object, array, union ond other composite shapes provide access to their nested shapes:

const userShape = d.object({
  name: d.string(),
  age: d.number()
});
// ⮕ Shape<{ name: string, age: number }>

userShape.propShapes.name;
// ⮕ Shape<string>

const userOrNameShape = d.or([userShape, d.string()]);
// ⮕ Shape<{ name: string, age: number } | string>

userOrNameShape.shapes[0];
// ⮕ userShape

Shape.at method derives a sub-shape at the given key, and if there's no such key then null is returned:

userShape.at('age');
// ⮕ Shape<number>

userShape.at('emotionalDamage');
// ⮕ null

This is especially useful with unions and intersections:

const shape = d.or([
  d.object({
    foo: d.string()
  }),
  d.object({
    foo: d.number()
  })
]);

shape.at('foo')
// ⮕ Shape<string | number>

shape.at('bar')
// ⮕ null

Localization

All shape factories and built-in checks support a custom issue messages:

d.string('Hey, string here').min(3, 'Too short');

Pass a function as a message, and it would receive an issue that would be raised, and parsing options. You can assign issue.message or return a message. For example, when using with React you may return a JSX element:

const reactMessage: d.Message = (issue, options) => (
  <span style={{ color: 'red' }}>
    The minimum length is {issue.param}
  </span>
);

d.number().min(5, reactMessage);

Semantics described above are applied to the message option as well:

d.string().length(3, { message: 'Invalid length' })

Override default messages

Default issue messages can be overridden by messages option:

import * as d from 'doubter';

d.string().parse(42, {
  messages: {
    'type.string': 'Yo, not a string!'
  }
});
// ❌ ValidationError: type.string at /: Yo, not a string!

The full list of issue codes can be found in Validation errors section.

Plugins

By default, when you import Doubter, you also get all built-in plugins as well:

import * as d from 'doubter';

d.string().min(2); // ✅ min is defined

d.number().gte(3); // ✅ gte is defined

If you import doubter/core, you would get only core set of shapes without any plugins:

import * as d from 'doubter/core';

d.string().min(2); // ❌ min is undefined

d.number().gte(3); // ❌ gte is undefined

You can cherry-pick plugins that you need:

import * as d from 'doubter/core';
import 'doubter/plugin/string-essentials';

d.string().min(2); // ✅ min is defined

d.number().gte(3); // ❌ gte is undefined

Built-in plugins

Recommended plugins

Integrations

You can combine Doubter with your favourite predicate library using refinements.

For example, create a shape that validates that input is an email using Validator.js:

import * as d from 'doubter';
import isEmail from 'validator/lib/isEmail';

const emailShape = d.string().refine(isEmail, 'Must be an email');
// ⮕ Shape<string>

emailShape.parse('Not an email');
// ❌ ValidationError: any.refine at /: Must be an email

emailShape.parse('foo@bar.com');
// ⮕ 'foo@bar.com'

You can use Doubter alterations with various utility libraries, such as Lodash:

import * as d from 'doubter';
import * as _ from 'lodash';

const shape = d.array(d.number()).alter(_.uniq);

shape.parse([1, 2, 3, 3, 2]);
// ⮕ [1, 2, 3])

Or use native JavaScript methods as alteration callbacks:

const shape = d.number().alter(Math.abs).alter(Math.round).min(3);

shape.parse(-3.1415);
// ⮕ 3

shape.parse(2);
// ❌ ValidationError: number.gte at /: Must be greater than or equal to 3

Authoring a plugin

Plugins use TypeScript's module augmentation to extend functionality of shapes exported from the doubter/core module.

Below is an example, how you can implement a naive email check and extend the StringShape.

import { StringShape } from 'doubter/core';

declare module 'doubter/core' {
  interface StringShape {
    email(): this;
  }
}

StringShape.prototype.email = function () {
  return this.addOperation(value => {
    if (value.includes('@')) {
      return null;
    }
    return [{ code: 'email', message: 'Must be an email' }]
  });
};

Now you can use this check when building a string shape:

const shape = d.string().email();

shape.parse('foo@bar.com');
// ⮕ 'foo@bar.com'

shape.parse('foo');
// ❌ ValidationError: email at /: Must be an email

You can use generic operations, checks, refinements, alterations, conversions, and any other functionality of the shape that is being extended.

Advanced shapes

You can create custom shapes by extending the Shape class.

Shape has several protected methods that you can override to change different aspects of the shape logic.

_apply(input, options, nonce)

Synchronous input parsing is delegated to this method. It receives an input that must be parsed and should return the Result:

  • null if the output value is the same as the input value;
  • an Ok object (as in example above) if the output contains a new value;
  • an array of Issue objects if parsing has failed.
_applyAsync(input, options, nonce)

Asynchronous input parsing is delegated to this method. It has the same semantics as _apply but returns a Promise. You need to override this method only if you have a separate logic for async parsing.

_isAsync()

The value returned from this method is toggles which method is used for parsing:

  • if true then _applyAsync would be used for parsing, and _apply would always throw an error;
  • if false then _apply can be used for parsing along with _applyAsync.
_getInputs()

Must return an array of types and values that can be processed by the shape. Elements of the returned array don't have to be unique. Refer to Introspection section for more details about types.

Let's create a custom shape that parses an input string as a number:

class NumberLikeShape extends d.Shape<string, number> {

  protected _apply(input: unknown, options: d.ParseOptions, nonce: number): d.Result<number> {

    // 1️⃣ Validate the input and return issues if it is invalid
    if (typeof input !== 'string' || isNaN(parseFloat(input))) {
      return [{
        code: 'kaputs',
        message: 'Must be a number-like',
        input,
      }];
    }

    // 2️⃣ Apply operations to the output value
    return this._applyOperations(input, parseFloat(input), options, null) as d.Result;
  }
}

Now let's use this shape alongside with other built-in shapes:

const shape = d.array(new NumberLikeShape());
// ⮕ Shape<string[], number[]>

shape.parse(['42', '33']);
// ⮕ [42, 33]

shape.parse(['seventeen']);
// ❌ ValidationError: kaputs at /0: Must be a number-like

Implementing deep partial support

To enable deepPartial support, your shape must implement DeepPartialProtocol.

class MyShape
  extends Shape
  implements DeepPartialProtocol<MyDeepPartialShape> {

  deepPartial(): MyDeepPartialShape {
    // Create and return a deep partial version of MyShape
  }
}

This is sufficient to enable type inference and runtime support for deepPartial method.

Performance

The chart below showcases the performance comparison of Doubter and its peers, in terms of millions of operations per second (greater is better).

Performance comparison chart

Tests were conducted using TooFast on Apple M1 with Node.js v20.4.0.

To reproduce the performance test suite results, clone this repo and run:

npm ci
npm run build
npm run perf -- -t overall
Detailed results
Success path

  Loose validation
    ● doubter    7.9 MHz ± 0.5%    128.4 B  ± 0.11%
    ● Ajv       15.8 MHz ± 1.33%   156.2 B  ± 0.01%
    ● zod        1.1 MHz ± 0.5%      4.2 kB ± 0.01%
    ● myzod      2.4 MHz ± 0.5%    506.4 B  ± 0.04%
    ● valita     4.4 MHz ± 0.5%    117.9 B  ± 0.07%
    ● valibot    3.0 MHz ± 0.5%      1.3 kB ± 0.01%

  Strict validation
    ● doubter    4.3 MHz ± 0.5%    149.9 B  ± 0.06%
    ● Ajv       13.1 MHz ± 1.15%   152.3 B  ± 0.01%
    ● zod        1.2 MHz ± 0.5%      4.2 kB ± 0.01%
    ● myzod      2.5 MHz ± 0.5%    316.7 B  ± 0.13%
    ● valita     4.3 MHz ± 0.5%    120.7 B  ± 0.46%
    ● valibot    3.0 MHz ± 0.5%      1.3 kB ± 0%

Failure path

  Loose validation
    ● doubter    4.2 MHz ± 0.6%      1.2 kB ± 0.01%
    ● Ajv       13.3 MHz ± 1.11%   356.4 B  ± 0.01%
    ● zod      175.0 kHz ± 1.04%    11.0 kB ± 0.22%
    ● myzod     76.9 kHz ± 0.5%      2.8 kB ± 0.09%
    ● valita     3.1 MHz ± 0.5%      1.5 kB ± 0%
    ● valibot    3.0 MHz ± 0.53%     1.3 kB ± 0.02%

  Strict validation
    ● doubter    2.9 MHz ± 0.5%      1.2 kB ± 0.01%
    ● Ajv       12.6 MHz ± 1.25%   331.6 B  ± 0%
    ● zod      178.0 kHz ± 1.08%    10.8 kB ± 0.22%
    ● myzod     64.5 kHz ± 0.5%      2.8 kB ± 0.17%
    ● valita     3.0 MHz ± 0.5%      1.4 kB ± 0%
    ● valibot    3.0 MHz ± 0.5%      1.3 kB ± 0%

Comparison with peers

The table below highlights features that are unique to Doubter and its peers.

DoubterZodValita

Shapes and parsing
Static type inference 🟢🟢🟢
Early return 🟢🔴🔴
Custom issue codes 🟢🔴🔴
Replace/allow/deny 🟢🔴🔴
Exclude/not 🟢🔴🔴
Discriminated unions 🟢 🌕 1🟢
Introspection at runtime 🟢 🌕 2🔴
Annotations/metadata 🟢🔴🔴
Partial objects 🟢🟢🟢
Deep partial 🟢 🌕 3🔴
Circular objects 🟢🔴🔴
Derive sub-shapes 🟢🔴🔴
Object key relationships 🟢🔴🔴
Parsing context 🟢🔴🔴

Async flow
Async shapes 🟢🟢🔴
Async refinements 🟢🟢🔴
Async conversions 🟢🟢🔴
Async checks 🟢🔴🔴
Async alterations 🟢🔴🔴
Check that shape is async 🟢🔴🔴

Type coercion
String 🟢 🌕 4🔴
Number 🟢 🌕 4🔴
Boolean 🟢 🌕 4🔴
BigInt 🟢 🌕 4🔴
Date 🟢 🌕 4🔴
Set 🟢🔴🔴
Map 🟢🔴🔴
Array 🟢🔴🔴
Enum 🟢🔴🔴
Const 🟢🔴🔴

Other
Plugin-centric 🟢🔴🔴
Tree-shakeable 🟢🔴🟢
  1. Zod uses z.union for regular unions and z.discriminatedUnion for discriminated unions, and discriminator key must be supplied manually as an argument. Doubter uses d.union to describe both regular unions and discriminated unions, and discriminator key is detected automatically.

  2. Zod schemas are class instances so introspection is possible, but there's no way to get a list of types accepted by a schema.

  3. Zod supports deepPartial for objects only. Doubter allows any shape to implement DeepPartialProtocol and all shapes (except for primitives) support it out-of-the-box.

  4. Zod coerces input values using wrapper constructors. Doubter uses custom converters for type coercion. For example, with Zod null is coerced to "null", while with Doubter null is coerced to an empty string.

any

d.any returns a Shape instance.

An unconstrained value that is inferred as any:

d.any();
// ⮕ Shape<any>

Use any to create shapes that are unconstrained at runtime but constrained at compile time:

d.any<{ foo: string }>();
// ⮕ Shape<{ foo: string }>

Create a shape that is constrained by a narrowing predicate:

d.any((value): value is string => typeof value === 'string');
// ⮕ Shape<any, string>

array

d.array returns an ArrayShape instance.

Constrains a value to be an array:

d.array();
// ⮕ Shape<any[]>

Restrict array element types:

d.array(d.number());
// ⮕ Shape<number[]>

Constrain the length of an array:

d.array(d.string()).min(1).max(10);

Limit both minimum and maximum array length at the same time:

d.array(d.string()).length(5);

Convert array values during parsing:

d.array(d.string().convert(parseFloat));
// ⮕ Shape<string[], number[]>

Make an array readonly:

d.array(d.string()).readonly();
// ⮕ Shape<string[], readonly string[]>

Coerce to an array

Iterables and array-like objects are converted to array via Array.from(value):

const shape = d.array(d.string()).coerce();

shape.parse(new Set(['John', 'Jack']));
// ⮕ ['John', 'Jack']

shape.parse({ 0: 'Bill', 1: 'Jill', length: 2 });
// ⮕ ['Bill', 'Jill']

Scalars, non-iterable and non-array-like objects are wrapped into an array:

shape.parse('Rose');
// ⮕ ['Rose']

bigint

d.bigint returns a BigIntShape instance.

Constrains a value to be a bigint.

d.bigint();
// ⮕ Shape<bigint>

Coerce to a bigint

null and undefined are converted to 0:

const shape = d.bigint().coerce();

shape.parse(null);
// ⮕ BigInt(0)

Number, string and boolean values are converted via BigInt(value):

shape.parse('18588');
// ⮕ BigInt(18588)

shape.parse('Unexpected')
// ❌ ValidationError: type.bigint at /: Must be a bigint

Arrays with a single element are unwrapped and the value is coerced:

shape.parse([0xdea]);
// ⮕ BigInt(3562)

shape.parse([BigInt(1), BigInt(2)]);
// ❌ ValidationError: type.bigint at /: Must be a bigint

boolean, bool

d.boolean returns a BooleanShape instance.

Constrains a value to be boolean.

d.boolean();
// or
d.bool();
// ⮕ Shape<boolean>

Coerce to a boolean

null, undefined, 'false' and 0 are converted to false:

const shape = d.boolean().coerce();

shape.parse(null);
// ⮕ false

'true' and 1 are converted to true:

shape.parse('true');
// ⮕ true

shape.parse('yes');
// ❌ ValidationError: type.boolean at /: Must be a boolean

Arrays with a single element are unwrapped and the value is coerced:

shape.parse([undefined]);
// ⮕ false

shape.parse([0, 1]);
// ❌ ValidationError: type.boolean at /: Must be a boolean

const

d.const returns a ConstShape instance.

Constrains a value to be an exact value:

d.const('Mars');
// ⮕ Shape<'Mars'>

There are shortcuts for null, undefined and nan constants.

Consider using enum if you want to check that an input is one of multiple values.

Coerce to a const

d.const coerces an input depending on the type of the given constant value. const uses bigint, number, string, boolean, or Date coercion rules if given constant matches one of these types. For example, if a given constant value is a string then the string coercion rules are applied:

const shape1 = d.const(BigInt(42)).coerce();

shape1.parse([new String('42')]);
// ⮕ BigInt(42)

Constant values of other types aren't coerced, but d.const would try to unwrap arrays with a single element to check the element equals to the given constant:

const users = new Set(['Bill']);

const shape2 = d.const(users).coerce();

shape1.parse([users]);
// ⮕ users

shape1.parse(new Set(['Bill']));
// ❌ ValidationError: type.set at /: Must be equal to [object Set]

convert, convertAsync

Both d.convert and d.convertAsync return a ConvertShape instance.

Converts the input value:

const shape = d.convert(parseFloat);
// ⮕ Shape<any, number>

Use convert in conjunction with shape piping:

shape.to(d.number().min(3).max(5));

Apply async conversions with convertAsync:

d.convertAsync(value => Promise.resolve('Hello, ' + value));
// ⮕ Shape<any, string>

For more information, see Conversions section.

date

d.date returns a DateShape instance.

Constrains a value to be a valid date.

d.date();
// ⮕ Shape<Date>

Constrain the minimum and maximum dates:

d.date().after('2003-03-12').before('2030-01-01');

Convert date to ISO string or timestamp:

d.date().toISOString().parse(new Date());
// ⮕ '2023-07-10T19:31:52.395Z'

d.date().toTimestamp().parse(new Date());
// ⮕ 1689017512395

Coerce to a Date

Strings and numbers are converted via new Date(value) and if an invalid date is produced then an issue is raised:

const shape = d.date().coerce();

shape.parse('2023-01-22');
// ⮕ Date

shape.parse('Yesterday');
// ❌ ValidationError: type.date at /: Must be a Date

Arrays with a single element are unwrapped and the value is coerced:

shape.parse([1674352106419]);
// ⮕ Date

shape.parse(['2021-12-03', '2023-01-22']);
// ❌ ValidationError: type.date at /: Must be a Date

enum

d.enum returns an EnumShape instance.

Constrains a value to be equal to one of predefined values:

d.enum(['Mars', 'Pluto', 'Jupiter']);
// ⮕ Shape<'Mars', 'Pluto', 'Jupiter'>

Or use a native TypeScript enum to limit possible values:

enum Planet {
  MARS,
  PLUTO,
  JUPITER
}

d.enum(Planet);
// ⮕ Shape<Planet>

Or use an object with a const assertion:

const planets = {
  MARS: 'Mars',
  PLUTO: 'Pluto',
  JUPITER: 'Jupiter'
} as const;

d.enum(plants);
// ⮕ Shape<'Mars', 'Pluto', 'Jupiter'>

Coerce to an enum

If an enum is defined via a native TypeScript enum or via a const object, then enum element names are coerced to corresponding values:

enum Users {
  JILL,
  SARAH,
  JAMES
}

const shape1 = d.enum(Users).coerce();

shape1.parse('SARAH');
// ⮕ 1

Arrays with a single element are unwrapped and the value is coerced:

shape1.parse(['JAMES']);
// ⮕ 2

shape1.parse([1]);
// ⮕ 1

shape1.parse([1, 2]);
// ❌ ValidationError: type.enum at /: Must be equal to one of 0,1,2

Other values follow const coercion rules:

const shape2 = d.enum([1970, new Date(0)]).coerce();

shape2.parse(new String('1970'));
// ⮕ 1970

shape2.parse(0);
// ⮕ Date { Jan 1, 1970 }

function, fn

d.function returns a FunctionShape instance.

Constrain a value to be a function with the given signature.

A function that has no arguments and returns any:

d.function()
// ⮕ Shape<() => any>

// or use a shorter alias
d.fn();

Provide an array of argument shapes:

d.fn([d.string(), d.number()]);
// ⮕ Shape<(arg1: string, arg2: number) => any>

Or provide a shape that constrains an array of arguments:

d.fn(d.array(d.string()));
// ⮕ Shape<(...args: string[]) => any>

Any shape that constrains an array type would do, you can even use a union:

d.fn(
  d.or([
    d.array(d.string()),
    d.tuple([d.string(), d.number()])
  ])
);
// ⮕ Shape<(...args: string[] | [string, number]) => any>

To constrain the return value of a function shape, use the return method.

d.fn().return(d.string());
// ⮕ Shape<() => string>

To constrain a value of this:

d.fn().this(
  d.object({ userId: d.string })
);
// ⮕ Shape<(this: { userId: string }) => any>

Parsing a function

Function shapes check that an input value is a function:

const shape1 = d.fn();

shape1.parse(() => 42);
// ⮕ () => any

shape1.parse('Mars');
// ❌ ValidationError: type.function at /: Must be a function

By default, the input function is returned as-is during parsing. If you want a parsed function to be type-safe at runtime use strict method to ensure the parsed function signature.

const callbackShape = d.fn([d.string()])
  .return(d.number().int())
  .strict();

const callback = callbackShape.parse(value => parseInt(value));
// ⮕ (arg: string) => number

callback ensures that the argument is string and the returned value is a number, or throws a ValidationError if types are invalid at runtime.

Ensuring function signature

You can ensure a function signature type-safety at runtime.

Let's declare a function shape that takes two number arguments and returns a number as well:

const sumShape = d.fn([d.number(), d.number()]).return(d.number());
// ⮕ Shape<(arg1: number, arg2: number) => number>

Now let's ensure a signature of a particular function:

const sum = sumShape.ensure(
  (arg1, arg2) => arg1 + arg2
);
// ⮕ (arg1: number, arg2: number) => number

sum(2, 3);
// ⮕ 5

sum would throw a ValidationError if the required signature is violated at runtime:

sum(2, '3');
// ❌ ValidationError: type.number at /arguments/1: Must be a number

sum(NaN, 2);
// ❌ ValidationError: type.number at /arguments/0: Must be an number

sum(1, 2, 3);
// ❌ ValidationError: array.max at /arguments: Must have the maximum length of 2

Using function shape you can parse this and return values as well.

const callbackShape = d.fn([d.number().int()])
  .this(d.array(d.string()))
  .return(d.string());
// ⮕ Shape<(this: string[], arg: number) => string>

const callback = callbackShape.ensure(function (index) {
  // 🟡 May be undefined if index is out of bounds
  return this[index];
});

When called with a valid index, a string is returned:

callback.call(['Jill', 'Sarah'], 1);
// ⮕ 'Sarah'

But if an index is out of bounds, an error is thrown:

callback.call(['James', 'Bob'], 33);
// ❌ ValidationError: type.string at /return: Must be a string

An error is thrown if an argument isn't an integer:

callback.call(['Bill', 'Tess'], 3.14);
// ❌ ValidationError: number.int at /arguments/0: Must be an integer

Coercing arguments

Function shapes go well with type coercion:

const plus2Shape = d.fn([d.number().coerce()]).return(d.number());
// ⮕ Shape<(arg: number) => number>

const plus2 = plus2Shape.ensure(arg => arg + 2);
// ⮕ (arg: number) => number

While plus2 requires a single integer parameter, we can call it at runtime with a number-like string and get an expected numeric result because an argument is coerced:

plus2('40');
// ⮕ 42

Transforming arguments and return values

Here's a function shape that converts a string argument to a number:

const shape = d.fn([d.string().convert(parseFloat)]);
// ⮕ Shape<(arg: number) => any, (arg: string) => any>

Note that the input and output functions described by this shape have different signatures. Let's implement of this function:

function inputFunction(arg: number): any {
  return arg + 2;
}

const outputFunction = shape.ensure(inputFunction);
// ⮕ (arg: string) => any

The pseudocode below demonstrates the inner workings of the outputFunction:

function outputFunction(...inputArgs) {

  const outputThis = shape.thisShape.parse(this);

  const outputArgs = shape.argsShape.parse(inputArgs);

  const inputResult = inputFunction.apply(outputThis, outputArgs);
  
  const outputResult = shape.resultShape.parse(inputResult);
  
  return outputResult;
}

instanceOf

d.instanceOf returns an InstanceShape instance.

Constrains a value to be an object that is an instance of a class:

class User {
  name?: string;
}

d.instanceOf(User);
// ⮕ Shape<User>

intersection, and

d.intersection returns an IntersectionShape instance.

Creates a shape that checks that the input value conforms to all shapes.

d.intersection([
  d.object({
    name: d.string()
  }),
  d.object({
    age: d.number()
  })
]);
// ⮕ Shape<{ name: string } & { age: number }>

Or use a shorter alias and:

d.and([
  d.array(d.string()),
  d.array(d.enum(['Peter', 'Paul']))
]);
// ⮕ Shape<string[] & Array<'Peter' | 'Paul'>>

Intersecting objects

When working with objects, extend objects instead of intersecting them whenever possible, since object shapes are more performant than object intersection shapes.

There's a logical difference between extended and intersected objects. Let's consider two shapes that both contain the same key:

const shape1 = d.object({
  foo: d.string(),
  bar: d.boolean(),
});

const shape2 = d.object({
  // 🟡 Notice that the type of foo property in shape2 differs from shape1.
  foo: d.number()
});

When you extend an object properties of the left object are overwritten with properties of the right object:

const shape = shape1.extend(shape2);
// ⮕ Shape<{ foo: number, bar: boolean }>

The intersection requires the input value to conform both shapes at the same time, it's not possible since there are no values that can satisfy the string | number type. So the type of property foo becomes never and no value would be able to satisfy the resulting intersection shape.

const shape = d.and([shape1, shape2]);
// ⮕ Shape<{ foo: never, bar: boolean }>

lazy

d.lazy returns a LazyShape instance.

With lazy you can declare recursive shapes. To showcase how to use it, let's create a shape that validates JSON data:

type JSON =
  | number
  | string
  | boolean
  | null
  | JSON[]
  | { [key: string]: JSON };

const jsonShape: d.Shape<JSON> = d.lazy(() =>
  d.or([
    d.number(),
    d.string(),
    d.boolean(),
    d.null(),
    d.array(jsonShape),
    d.record(jsonShape)
  ])
);

jsonShape.parse({ name: 'Jill' });
// ⮕ { name: 'Jill' }

jsonShape.parse({ tag: Symbol() });
// ❌ ValidationError: type.union at /tag: Must conform the union

Note that the JSON type is defined explicitly, because it cannot be inferred from the shape which references itself directly in its own initializer.

You can also use d.lazy like this:

const jsonShape: d.Shape<JSON> = d.or([
  d.number(),
  d.string(),
  d.boolean(),
  d.null(),
  d.array(d.lazy(() => jsonShape)),
  d.record(d.lazy(() => jsonShape))
]);

Circular object references

Doubter supports circular object references out-of-the-box:

interface User {
  friends: User[];
}

const hank: User = {
  friends: []
};

// 🟡 The circular reference
hank.friends.push(hank);

const userShape1: d.Shape<User> = d.lazy(() =>
  d.object({
    friends: d.array(userShape1)
  })
);

userShape1.parse(hank);
// ⮕ hank

userShape1.parse(hank).friends[0];
// ⮕ hank

You can replace circular references with a replacement value:

const userShape2: d.Shape<User> = d.lazy(() =>
  d.object({
    friends: d.array(userShape2)
  })
).circular('Me and Myself');

userShape1.parse(hank);
// ⮕ hank

userShape2.parse(hank).friends[0];
// ⮕ 'Me and Myself'

You can provide a callback that returns a value that is used as a replacement value for circular references. Or it can throw a ValidationError from the callback to indicate that circular references aren't allowed:

const userShape3: d.Shape<User> = d.lazy(() =>
  d.object({
    friends: d.array(userShape3)
  })
).circular((input, options) => {
  throw new d.ValidationError([{ code: 'kaputs' }]);
});

userShape1.parse(hank);
// ❌ ValidationError: kaputs at /friends/0

By default, Doubter neither parses nor validates an object if it was already seen, and returns such object as is. This behaviour was chosen as the default for d.lazy because otherwise the result would be ambiguous when conversions are introduced.

interface Foo {
  bar?: Foo;
}

const foo: Foo = {};

foo.bar = foo;

const fooShape: d.Shape<Foo, string> = d.lazy(() =>
  d.object({
    bar: fooShape.optional(),
  })
).convert(output => {
  //        ⮕ {bar?: Foo} | {bar?: string}
  return 'hello';
});

fooShape.parse(foo);
// ⮕ 'hello'

map

d.map returns a MapShape instance.

Constrains an input to be a Map instance:

d.map(d.string(), d.number());
// ⮕ Shape<Map<string, number>>

Mark a Map as readonly:

d.map(d.string(), d.number()).readonly();
// ⮕ Shape<Map<string, number>, ReadonlyMap<string, number>>

Note

Marking a Map as readonly, only affects type checking. At runtime, you would still be able to set and delete items.

Coerce to a Map

Arrays, iterables and array-like objects that withhold entry-like elements (a tuple with two elements) are converted to Map entries via Array.from(value):

const shape = d.map(d.string(), d.number()).coerce();

shape.parse([
  ['Mars', 0.1199],
  ['Pluto', 5.3361]
]);
// ⮕ Map { 'Mars' → 0.1199, 'Pluto' → 5.3361 }

shape.parse(['Jake', 'Bill']);
// ❌ ValidationError: type.map at /: Must be a Map

Other objects are converted to an array of entries via new Map(Object.entries(value)):

shape.parse({
  Jake: 31,
  Jill: 28
});
// ⮕ Map { 'Jake' → 31, 'Jill' → 28 }

nan

d.nan returns a ConstShape instance.

The shape that requires an input to be NaN:

d.nan();
// ⮕ Shape<number>

If you want to constrain a number and allow NaN values, use number:

d.number().nan();
// ⮕ Shape<number>

never

d.never returns a NeverShape instance.

The shape that always raises a validation issue regardless of an input value:

d.never();
// ⮕ Shape<never>

not

d.not returns an ExcludeShape instance.

The shape that allows any value that doesn't conform the negated shape:

const shape = d.not(d.string())
// ⮕ Shape<any>

shape.parse(42);
// ⮕ 42

shape.parse('Bill');
// ❌ ValidationError: any.exclude at /: Must not conform the excluded shape

More about exclusions in the Exclude a shape section.

null

d.null returns a ConstShape instance.

The shape that requires an input to be null:

d.null();
// ⮕ Shape<null>

number

d.number returns a NumberShape instance.

The shape that requires an input to be a number.

d.number();
// ⮕ Shape<number>

Allow NaN input values:

d.number().nan();
// ⮕ Shape<number>

Replace NaN with a default value:

d.number().nan(0).parse(NaN);
// ⮕ 0

Limit the allowed range:

// The number must be greater than 5 and less then or equal to 10
d.number().gt(0.5).lte(2.5)
// ⮕ Shape<number>

Constrain a number to be a multiple of a divisor:

// Number must be divisible by 5 without a remainder
d.number().multipleOf(5);

Constrain the number to be an integer:

d.number().int();

Constrain the input to be a finite number (not NaN, Infinity or -Infinity):

d.number().finite();

Coerce to a number

null and undefined values are converted to 0:

const shape = d.number().coerce();

shape.parse(null);
// ⮕ 0

Strings, boolean values and Date objects are converted using +value:

shape.parse('42');
// ⮕ 42

shape.parse('seventeen');
// ❌ ValidationError: type.number at /: Must be a number

Arrays with a single element are unwrapped and the value is coerced:

shape.parse([new Date('2023-01-22')]);
// ⮕ 1674345600000

shape.parse([1997, 1998]);
// ❌ ValidationError: type.number at /: Must be a number

object

d.object returns an ObjectShape instance.

Constrains a value to be an object with a set of properties:

d.object({
  name: d.string(),
  age: d.number()
});
// ⮕ Shape<{ name: string, age: number }>

Make an object readonly:

d.object({
  name: d.string()
}).readonly();
// ⮕ Shape<{ name: string }, { readonly name: string }>

Optional properties

If the inferred type of the property shape is a union with undefined then the property becomes optional:

d.object({
  name: d.string().optional(),
  age: d.number()
});
// ⮕ Shape<{ name?: string | undefined, age: number }>

Or you can define optional properties as a union with d.undefined:

d.object({
  name: d.or([d.string(), d.undefined()]),
});
// ⮕ Shape<{ name?: string | undefined }>

If the conversion result extends undefined then the output property becomes optional:

d.object({
  name: d.string().convert(
    value => value !== 'Google' ? value : undefined
  ),
});
// ⮕ Shape<{ name: string }, { name?: string | undefined }>

Index signature

Add an index signature to the object type, so all properties that aren't listed explicitly are validated with the rest shape:

const shape = d.object({
  foo: d.string(),
  bar: d.number()
});
// ⮕ Shape<{ foo: string, bar: number }>

const restShape = d.or([
  d.string(),
  d.number()
]);
// ⮕ Shape<string | number>

shape.rest(restShape);
// ⮕ Shape<{ foo: string, bar: number, [key: string]: string | number }>

Unlike an index signature in TypeScript, a rest shape is applied only to keys that aren't explicitly specified among object property shapes.

Unknown keys

Keys that aren't defined explicitly can be handled in several ways:

  • constrained by the rest shape;
  • stripped;
  • preserved as is, this is the default behavior;
  • prohibited.

Force an object to have only known keys. If an unknown key is met, a validation issue is raised.

d.object({
  foo: d.string(),
  bar: d.number()
}).exact();

Strip unknown keys, so the object is cloned if an unknown key is met, and only known keys are preserved.

d.object({
  foo: d.string(),
  bar: d.number()
}).strip();

Derive the new shape and override the strategy for unknown keys:

const shape = d.object({ foo: d.string() }).exact();

// Unknonwn keys are now preserved
shape.preserve();

Picking and omitting properties

Picking keys from an object creates the new shape that contains only listed keys:

const shape1 = d.object({
  foo: d.string(),
  bar: d.number()
});

const shape2 = shape1.pick(['foo']);
// ⮕ Shape<{ foo: string }>

Omitting keys of an object creates the new shape that contains all keys except listed ones:

const shape = d.object({
  foo: d.string(),
  bar: d.number()
});

shape.omit(['foo']);
// ⮕ Shape<{ bar: number }>

Extending objects

Add new properties to the object shape:

const shape = d.object({
  name: d.string()
});

shape.extend({
  age: d.number()
});
// ⮕ Shape<{ name: string, age: number }>

Merging object shapes preserves the index signature of the left-hand shape:

const fooShape = d.object({
  foo: d.string()
}).rest(d.or([d.string(), d.number()]));

const barShape = d.object({
  bar: d.number()
});

fooShape.extend(barShape);
// ⮕ Shape<{ foo: string, bar: number, [key: string]: string | number }>

Making objects partial and required

Object properties are optional if their type extends undefined. Derive an object shape that would have its properties all marked as optional:

const shape1 = d.object({
  foo: d.string(),
  bar: d.number()
});

shape1.partial()
// ⮕ Shape<{ foo?: string | undefined, bar?: number | undefined }>

Specify which fields should be marked as optional:

const shape2 = d.object({
  foo: d.string(),
  bar: d.number()
});

shape2.partial(['foo'])
// ⮕ Shape<{ foo?: string | undefined, bar: number }>

In the same way, properties that are optional can be made required:

const shape3 = d.object({
  foo: d.string().optional(),
  bar: d.number()
});

shape3.required(['foo'])
// ⮕ Shape<{ foo: string, bar: number }>

Note that required would force the value of both input and output to be non-undefined.

Object keys

Derive a shape that constrains keys of an object:

const shape = d.object({
  name: d.string(),
  age: d.number()
});

shape.keysShape;
// ⮕ Shape<'name' | 'age'>

Key relationships

Declare relationships between object keys using allKeys notAllKeys orKeys xorKeys oxorKeys

const shape = d.object({
  foo: d.string(),
  bar: d.number(),
  baz: d.boolean()
})
  .partial()
  .xorKeys(['foo', 'bar']);

shape.parse({ foo: 'Mars', bar: 42 });
// ❌ ValidationError: object.xorKeys at /: Must contain exactly one key: foo,bar

promise

d.promise returns a PromiseShape instance.

The shape that checks that an input is an instance of Promise.

d.promise();
// ⮕ Shape<Promise<any>>

Constrain a resolved value of a promise:

d.promise(d.string());
// ⮕ Shape<Promise<string>>

Convert a value inside a promise:

const shape = d.promise(
  d.string().convert(parseFloat)
);
// ⮕ Shape<Promise<string>, Promise<number>>

Coerce to a Promise

All values are converted to a promise by wrapping it in Promise.resolve():

const shape = d.promise(d.number()).coerce();

shape.parseAsync(42);
// ⮕ Promise<number>

record

d.record returns a RecordShape instance.

Constrain keys and values of a dictionary-like object:

d.record(d.number())
// ⮕ Shape<Record<string, number>>

Constrain both keys and values of a dictionary-like object:

d.record(d.string(), d.number())
// ⮕ Shape<Record<string, number>>

Pass any shape that extends Shape<string> as a key constraint:

const keysShape = d.enum(['foo', 'bar']);
// ⮕ Shape<'foo' | 'bar'>

d.record(keysShape, d.number());
// ⮕ Shape<Record<'foo' | 'bar', number>>

Make a record readonly:

d.record(d.number()).readonly();
// ⮕ Shape<Record<string, number>, Readonly<Record<string, number>>>

set

d.set returns a SetShape instance.

Constrains an input to be a Set instance:

d.set(d.number());
// ⮕ Shape<Set<number>>

Constrain the size of a Set:

d.set(d.string()).min(1).max(10);

Limit both minimum and maximum size at the same time:

d.set(d.string()).size(5);

Mark a Set as readonly:

d.set(d.string()).readonly();
// ⮕ Shape<Set<string>, ReadonlySet<string>>

Note

Marking a Set as readonly, only affects type checking. At runtime, you would still be able to add and delete items.

Coerce to a Set

Arrays, iterables and array-like objects converted to Set values via Array.from(value):

const shape = d.set(d.string()).coerce();

shape.parse(['Boris', 'K']);
// ⮕ Set { 'Boris', 'K' }

Scalars, non-iterable and non-array-like objects are wrapped into an array:

shape.parse('J');
// ⮕ Set { 'J' }

string

d.string returns a StringShape instance.

Constrains a value to be string.

d.string();
// ⮕ Shape<string>

Constrain the string length limits:

d.string().min(1).max(10);

Limit both minimum and maximum string length at the same time:

d.string().length(5);

Constrain a string with a regular expression:

d.string().regex(/foo|bar/);

Coerce to a string

null and undefined are converted to an empty string:

const shape = d.string().coerce();

shape.parse(null);
// ⮕ ''

Finite numbers, boolean and bigint values are converted via String(value):

shape.parse(BigInt(2398955));
// ⮕ '2398955'

shape.parse(8080);
// ⮕ '8080'

shape.parse(-Infinity);
// ❌ ValidationError: type.string at /: Must be a string

Valid dates are converted to an ISO formatted string:

shape.parse(new Date(1674352106419));
// ⮕ '2023-01-22T01:48:26.419Z'

shape.parse(new Date(NaN));
// ❌ ValidationError: type.string at /: Must be a string

Arrays with a single element are unwrapped and the value is coerced:

shape.parse([undefined]);
// ⮕ ''

shape.parse(['Jill', 'Sarah']);
// ❌ ValidationError: type.string at /: Must be a string

symbol

d.symbol returns a SymbolShape instance.

The shape that constrains a value to be an arbitrary symbol.

d.symbol();
// ⮕ Shape<symbol>

To constrain an input to an exact symbol, use const:

const TAG = Symbol('tag');

d.const(TAG);
// ⮕ Shape<typeof TAG>

Or use an enum to allow several exact symbols:

const FOO = Symbol('foo');
const BAR = Symbol('bar');

d.enum([FOO, BAR]);
// ⮕  Shape<typeof FOO | typeof BAR>

tuple

d.tuple returns an ArrayShape instance.

Constrains a value to be a tuple where elements at particular positions have concrete types:

d.tuple([d.string(), d.number()]);
// ⮕ Shape<[string, number]>

Specify a rest tuple elements:

d.tuple([d.string(), d.number()], d.boolean());
// ⮕ Shape<[string, number, ...boolean]>

// Or
d.tuple([d.string(), d.number()]).rest(d.boolean());
// ⮕ Shape<[string, number, ...boolean]>

Make a tuple readonly:

d.tuple([d.string()]).readonly();
// ⮕ Shape<[string], readonly [string]>

Tuples follow array type coercion rules.

undefined

d.undefined returns a ConstShape instance.

The shape that requires an input to be undefined:

d.undefined();
// ⮕ Shape<undefined>

union, or

d.union returns a UnionShape instance.

A constraint that allows a value to be one of the given types:

d.union([d.string(), d.number()]);
// ⮕ Shape<string | number>

Use a shorter alias or:

d.or([d.string(), d.number()]);

Discriminated unions

A discriminated union is a union of object shapes that all share a particular key.

Doubter automatically applies various performance optimizations to union shapes and discriminated union detection is one of them. As an example, let's create a discriminated union of objects representing various business types.

Sole entrepreneur goes first:

const entrepreneurShape = d.object({
  bisinessType: d.const('entrepreneur'),
  name: d.string(),
  age: d.number().int().gte(18)
});
// ⮕ Shape<{ type: 'entrepreneur', name: string, age: number }>

We're going to use bisinessType property as the discriminator in our union. Now let's define a shape for a company:

const companyShape = d.object({
  businessType: d.or([
    d.const('llc'),
    d.enum(['corporation', 'partnership'])
  ]),
  headcount: d.number().int().positive()
});
// ⮕ Shape<{ type: 'llc' | 'corporation' | 'partneership', headcount: number }>

Notice that we declared businessType as a composite shape. This would work just fine until shape restricts its input to a set of literal values.

The final step is to define a discriminated union shape:

const businessShape = d.union([entrepreneurShape, companyShape]);

union would detect that all object shapes in the union have the businessType property with distinct values and would enable a discriminated union optimization.

Discriminated unions raise fewer issues because only one shape from the union can be applied to an input:

businessType.parse({
  businessType: 'corporation',
  headcount: 0
});
// ❌ ValidationError: number.gte at /headcount: Must be greater than 0

Issues raised by a union

If there are multiple shapes in the union that have raised issues during parsing, then union returns a grouping issue.

const shape = d.or([
  d.object({
    name: d.string()
  }),
  d.object({
    age: d.number()
  })
]);
// ⮕ Shape<{ name: string } | { age: number }>

shape.try({ name: 47, age: null });

The result of try would contain a grouping issue:

{
  code: 'type.union',
  path: [],
  input: {
    name: 47,
    age: null
  },
  message: 'Must conform the union',
  param: {
    inputs: [Type.OBJECT],
    issueGroups: [
      [
        {
          code: 'type.string',
          path: ['name'],
          input: 47,
          message: 'Must be a string'
        }
      ],
      [
        {
          code: 'type.number',
          path: ['age'],
          input: null,
          message: 'Must be a number'
        }
      ]
    ]
  }
}
inputs

An array of all input types and literal values that the union accepts.

issueGroups

An array of issue groups where each group contains issues raised by a separate shape in the union; or null.

Union checks the input only against shapes that accept the input value type. If there were no shapes in the union that accept the provided input value type, then issueGroups is null. For example, if you have a number | string union and parse a boolean value, there's no shape that accepts boolean input type. So the raised union issue would have issueGroups set to null.

path of issues in issueGroups is relative to the grouping issue.

When union detects that only one of its shapes accepts the provided input value then issues produced by this shape are returned as is:

d.or([d.number(), d.string().min(6)]).try('Okay')

In this example, only d.string can parse the 'Okay' input value, so the result of try would contain a single string-related issue:

{
  code: 'string.min',
  path: [],
  input: 'Okay',
  message: 'Must have the minimum length of 6',
  param: 6
}

This behaviour is applied to discriminated unions as well.

unknown

d.unknown returns a Shape instance.

An unconstrained value that is inferred as unknown:

d.unknown();
// ⮕ Shape<unknown>

void

d.void returns a ConstShape instance.

The shape that requires an input to be undefined that is typed as void:

d.void();
// ⮕ Shape<void>

Cookbook

Type-safe URL query params

Let's define a shape that describes the query with name and age params:

const queryShape = d
  .object({
    name: d.string(),
    age: d.number().int().nonNegative().coerce().catch()
  })
  .partial();
// ⮕ Shape<{ name?: string | undefined, age?: number | undefined }>

🎯 Key takeaways

  1. Query params are strings. Since name is constrained by d.string it doesn't require additional attention. On the other hand, age is an integer, so type coercion must be enabled.

  2. We also added catch, so when age cannot be parsed as a positive integer, Doubter returns undefined instead of raising a validation issue.

  3. The object shape is marked as partial, so absence of any query param won't raise a validation issue. You can mark individual params as optional and provide a default value.

Now, let's parse the query string with qs and then apply our shape:

import qs from 'qs';

const query = queryShape.parse(qs.parse('name=Frodo&age=50'));
// ⮕ { name: 'Frodo', age: 50 }

age is set to undefined if it is invalid:

queryShape.parse(qs.parse('age=-33'));
// ⮕ { age: undefined }

Type-safe environment variables

If you're developing an app that consumes environment variables you most likely want to validate them.

const envShape = d
  .object({
    NODE_ENV: d.enum(['test', 'production']),
    HELLO_DATE: d.date().coerce().optional(),
  })
  .strip();

🎯 Key takeaways

  1. Since env variables are strings, we should enable type coercion to convert the value of HELLO_DATE to a Date instance.

  2. NODE_ENV is the required env variable, while HELLO_DATE is optional. If HELLO_DATE is provided and cannot be coerced to a date, a validation error would be raised.

  3. Unknown env variables are stripped, so they won't be visible inside the app. This prevents an accidental usage of an unvalidated env variable.

const env = envShape.parse(process.env);
// ⮕ { NODE_ENV: 'test' | 'production', HELLO_DATE?: Date }

Type-safe CLI arguments

If you're developing a console app you may want to validate arguments passed via CLI. For example, lets write an app that processes the following CLI parameters:

node app.js --name Bill --age 42

First, install argcat, and use it to convert an array of CLI arguments to an object:

import { parseArgs } from 'argcat';

const args = parseArgs(process.argv.slice(2));
// ⮕ { '': [], name: ['Bill'], age: ['42'] }

Now let's define the shape of the parsed object:

const optionsShape = d
  .object({
    name: d.string().coerce(),
    age: d.number().int().nonNegative().coerce(),
  })
  .strip();

strip removes all unknown keys from an object. It is used here to prevent unexpected arguments to be accessible inside the app. You may want to throw an error if unknown keys are detected or ignore them. Refer to Unknown keys section to find out how this can be done.

Parse CLI arguments using optionsShape with enabled type coercion:

const options = optionsShape.parse(args);
// ⮕ { name: 'Bill', age: 42 }

Type-safe localStorage

localStorage is a key-value storage which allows persistence of string keys and string values on the client. Let's write two functions that can read and write JSON objects from and to localStorage in a type-safe manner.

First lets define a shape of the data stored in the localStorage. In this example localStorage would allow only one key 'user' that would correspond to an object with name and age properties:

import * as d from 'doubter';

const userShape = d.object({
  name: d.string(),
  age: d.number().int().positive()
});

const localStorageItemsShape = d.object({
  user: userShape
});

Let's infer a type of the data in the localStorage:

type LocalStorageItems = d.Input<typeof localStorageItemsShape>;

You can read more about d.Input and d.Output in Static type inference section. In this example, we don't have any alterations or conversions, so the localStorageItemsShape has the same input and output.

Now it's time to create a function that reads items in a type-safe manner:

function getItem<K extends keyof LocalStorageItems>(key: K): LocalStorageItems[K] | null {
  const valueShape = localStorageItemsShape.at(key);
  const value = localStorage.getItem(key);
  
  if (valueShape === null) {
    throw new Error('Unknown key: ' + key);
  }
  if (value === null) {
    return null;
  }
  return valueShape.parse(JSON.parse(value));
}

Read more about Shape.at method in the Nested shapes section. The same approach can be taken to implement writes:

function setItem<K extends keyof LocalStorageItems>(key: K, value: LocalStorageItems[K]): void {
  const valueShape = localStorageItemsShape.at(key);

  if (valueShape === null) {
    throw new Error('Unknown key: ' + key);
  }
  localStorage.setItem(key, JSON.stringify(valueShape.parse(value)));
}

Note that we prevent writes of the unknown keys as well as reads. Now, let's use those functions:

setItem('user', { name: 'John', age: 42 });

getItem('user');
// ⮕ { name: 'John', age: 42 }

setItem('user', { name: 'Bill', age: -100 });
// ❌ ValidationError: number.gte at /: Must be greater than 0

getItem('account');
// ❌ Error: Unknown key: account

Rename object keys

First, create a shape that describes the key transformation. In this example we are going to convert the enumeration of keys to an uppercase string:

const keysShape = d.enum(['foo', 'bar']).convert(
  value => value.toUpperCase() as 'FOO' | 'BAR'
);
// ⮕ Shape<'foo' | 'bar', 'FOO' | 'BAR'>

Then, create a d.record shape that constrains keys and values or a dictionary-like object:

const shape = d.record(keysShape, d.number());
// ⮕ Shape<Record<'foo' | 'bar', number>, Record<'FOO' | 'BAR', number>>

Parse the input object, the output would be a new object with transformed keys:

shape.parse({ foo: 1, bar: 2 });
// ⮕ { FOO: 1, BAR: 2 }

Conditionally applied shapes

If you need to apply a different shape depending on an input value, you can use convert.

const stringShape = d.string().min(5);

const numberShape = d.number().positive();

const shape = d.convert(value => {
  if (typeof value === 'string') {
    return stringShape.parse(value)
  } else {
    return numberShape.parse(value);
  }
});

parse would throw a ValidationError that is captured by the enclosing convert.

shape.parse('Pluto');
// ⮕ 'Pluto'

shape.parse('Mars');
// ❌ ValidationError: string.min at /: Must have the minimum length of 5

shape.parse(42);
// ⮕ 42

shape.parse(-273.15);
// ❌ ValidationError: number.gte at /: Must be greater than 0

:octocat: ❤️