Yup

Dead simple Object schema validation

README

Yup


Yup is a schema builder for runtime value parsing and validation. Define a schema, transform a value to match, assert the shape of an existing value, or both. Yup schema are extremely expressive and allow modeling complex, interdependent validations, or value transformation.

You are viewing docs for the v1.0.0 pre-release of yup, pre-v1 docs are available: here


Killer Features:

- Concise yet expressive schema interface, equipped to model simple to complex data models
- Powerful TypeScript support. Infer static types from schema, or ensure schema correctly implement a type
- Built-in async validation support. Model server-side and client-side validation equally well
- Extensible: add your own type-safe methods and schema
- Rich error details, make debugging a breeze

Getting Started


Schema are comprised of parsing actions (transforms) as well as assertions (tests) about the input value.
Validate an input value to parse it and run the configured set of assertions. Chain together methods to build a schema.

  1. ```ts
  2. import { object, string, number, date, InferType } from 'yup';

  3. let userSchema = object({
  4.   name: string().required(),
  5.   age: number().required().positive().integer(),
  6.   email: string().email(),
  7.   website: string().url().nullable(),
  8.   createdOn: date().default(() => new Date()),
  9. });

  10. // parse and assert validity
  11. const user = await userSchema.validate(await fetchUser());

  12. type User = InferType<typeof userSchema>;
  13. /* {
  14.   name: string;
  15.   age: number;
  16.   email?: string | undefined
  17.   website?: string | null | undefined
  18.   createdOn: Date
  19. }*/
  20. ```

Use a schema to coerce or "cast" an input value into the correct type, and optionally
transform that value into more concrete and specific values, without making further assertions.

  1. ```ts
  2. // Attempts to coerce values to the correct type
  3. const parsedUser = userSchema.cast({
  4.   name: 'jimmy',
  5.   age: '24',
  6.   createdOn: '2014-09-23T19:25:25Z',
  7. });
  8. // ✅  { name: 'jimmy', age: 24, createdOn: Date }
  9. ```

Know that your input value is already parsed? You can "strictly" validate an input, and avoid the overhead
of running parsing logic.

  1. ```ts
  2. // ❌  ValidationError "age is not a number"
  3. const parsedUser = await userSchema.validate(
  4.   {
  5.     name: 'jimmy',
  6.     age: '24',
  7.   },
  8.   { strict: true },
  9. );
  10. ```

Table of Contents


<!-- The exported functions are factory methods for constructing schema instances, but without the new keyword.
If you need access to the actual schema classes, they are also exported:

  1. ``` js
  2. import {
  3.   BooleanSchema,
  4.   DateSchema,
  5.   MixedSchema,
  6.   NumberSchema,
  7.   ArraySchema,
  8.   ObjectSchema,
  9.   StringSchema,
  10. } from 'yup';
  11. ``` -->




- API
  - [yup](#yup)
    - [reach(schema: Schema, path: string, value?: object, context?: object): Schema](#reachschema-schema-path-string-value-object-context-object-schema)
    - [addMethod(schemaType: Schema, name: string, method: ()=> Schema): void](#addmethodschematype-schema-name-string-method--schema-void)
    - [ref(path: string, options: { contextPrefix: string }): Ref](#refpath-string-options--contextprefix-string--ref)
    - [lazy((value: any) => Schema): Lazy](#lazyvalue-any--schema-lazy)
- [`ValidationError(errors: string | Array, value: any, path: string)`](#validationerrorerrors-string--arraystring-value-any-path-string)
  - [Schema](#schema)
    - [Schema.clone(): Schema](#schemaclone-schema)
    - [Schema.label(label: string): Schema](#schemalabellabel-string-schema)
    - [Schema.meta(metadata: object): Schema](#schemametametadata-object-schema)
    - [Schema.describe(options?: ResolveOptions): SchemaDescription](#schemadescribeoptions-resolveoptions-schemadescription)
    - [Schema.concat(schema: Schema): Schema](#schemaconcatschema-schema-schema)
- [`Schema.validate(value: any, options?: object): Promise, ValidationError>`](#schemavalidatevalue-any-options-object-promiseinfertypeschema-validationerror) - [`Schema.validateSync(value: any, options?: object): InferType`](#schemavalidatesyncvalue-any-options-object-infertypeschema) - [`Schema.validateAt(path: string, value: any, options?: object): Promise, ValidationError>`](#schemavalidateatpath-string-value-any-options-object-promiseinfertypeschema-validationerror) - [`Schema.validateSyncAt(path: string, value: any, options?: object): InferType`](#schemavalidatesyncatpath-string-value-any-options-object-infertypeschema) - [`Schema.isValid(value: any, options?: object): Promise`](#schemaisvalidvalue-any-options-object-promiseboolean)
    - [Schema.isValidSync(value: any, options?: object): boolean](#schemaisvalidsyncvalue-any-options-object-boolean)
- [`Schema.cast(value: any, options = {}): InferType`](#schemacastvalue-any-options---infertypeschema) - [`Schema.isType(value: any): value is InferType`](#schemaistypevalue-any-value-is-infertypeschema)
    - [Schema.strict(enabled: boolean = false): Schema](#schemastrictenabled-boolean--false-schema)
    - [Schema.strip(enabled: boolean = true): Schema](#schemastripenabled-boolean--true-schema)
    - [Schema.withMutation(builder: (current: Schema) => void): void](#schemawithmutationbuilder-current-schema--void-void)
    - [Schema.default(value: any): Schema](#schemadefaultvalue-any-schema)
    - [Schema.getDefault(options?: object): Any](#schemagetdefaultoptions-object-any)
    - [Schema.nullable(): Schema](#schemanullable-schema)
    - [Schema.nonNullable(): Schema](#schemanonnullable-schema)
    - [Schema.defined(): Schema](#schemadefined-schema)
    - [Schema.optional(): Schema](#schemaoptional-schema)
    - [Schema.required(message?: string | function): Schema](#schemarequiredmessage-string--function-schema)
    - [Schema.notRequired(): Schema Alias: optional()](#schemanotrequired-schema-alias-optional)
    - [Schema.typeError(message: string): Schema](#schematypeerrormessage-string-schema)
- [`Schema.oneOf(arrayOfValues: Array, message?: string | function): Schema` Alias: `equals`](#schemaoneofarrayofvalues-arrayany-message-string--function-schema-alias-equals) - [`Schema.notOneOf(arrayOfValues: Array, message?: string | function)`](#schemanotoneofarrayofvalues-arrayany-message-string--function)
    - [Schema.when(keys: string | string[], builder: object | (values: any[], schema) => Schema): Schema](#schemawhenkeys-string--string-builder-object--values-any-schema--schema-schema)
    - [Schema.test(name: string, message: string | function | any, test: function): Schema](#schematestname-string-message-string--function--any-test-function-schema)
    - [Schema.test(options: object): Schema](#schematestoptions-object-schema)
    - [Schema.transform((currentValue: any, originalValue: any) => any): Schema](#schematransformcurrentvalue-any-originalvalue-any--any-schema)
  - mixed
  - string
    - [string.required(message?: string | function): Schema](#stringrequiredmessage-string--function-schema)
    - [string.length(limit: number | Ref, message?: string | function): Schema](#stringlengthlimit-number--ref-message-string--function-schema)
    - [string.min(limit: number | Ref, message?: string | function): Schema](#stringminlimit-number--ref-message-string--function-schema)
    - [string.max(limit: number | Ref, message?: string | function): Schema](#stringmaxlimit-number--ref-message-string--function-schema)
    - [string.matches(regex: Regex, message?: string | function): Schema](#stringmatchesregex-regex-message-string--function-schema)
    - [string.matches(regex: Regex, options: { message: string, excludeEmptyString: bool }): Schema](#stringmatchesregex-regex-options--message-string-excludeemptystring-bool--schema)
    - [string.email(message?: string | function): Schema](#stringemailmessage-string--function-schema)
    - [string.url(message?: string | function): Schema](#stringurlmessage-string--function-schema)
    - [string.uuid(message?: string | function): Schema](#stringuuidmessage-string--function-schema)
    - [string.ensure(): Schema](#stringensure-schema)
    - [string.trim(message?: string | function): Schema](#stringtrimmessage-string--function-schema)
    - [string.lowercase(message?: string | function): Schema](#stringlowercasemessage-string--function-schema)
    - [string.uppercase(message?: string | function): Schema](#stringuppercasemessage-string--function-schema)
  - number
    - [number.min(limit: number | Ref, message?: string | function): Schema](#numberminlimit-number--ref-message-string--function-schema)
    - [number.max(limit: number | Ref, message?: string | function): Schema](#numbermaxlimit-number--ref-message-string--function-schema)
    - [number.lessThan(max: number | Ref, message?: string | function): Schema](#numberlessthanmax-number--ref-message-string--function-schema)
    - [number.moreThan(min: number | Ref, message?: string | function): Schema](#numbermorethanmin-number--ref-message-string--function-schema)
    - [number.positive(message?: string | function): Schema](#numberpositivemessage-string--function-schema)
    - [number.negative(message?: string | function): Schema](#numbernegativemessage-string--function-schema)
    - [number.integer(message?: string | function): Schema](#numberintegermessage-string--function-schema)
    - [number.truncate(): Schema](#numbertruncate-schema)
    - [number.round(type: 'floor' | 'ceil' | 'trunc' | 'round' = 'round'): Schema](#numberroundtype-floor--ceil--trunc--round--round-schema)
  - boolean
  - date
    - [date.min(limit: Date | string | Ref, message?: string | function): Schema](#dateminlimit-date--string--ref-message-string--function-schema)
    - [date.max(limit: Date | string | Ref, message?: string | function): Schema](#datemaxlimit-date--string--ref-message-string--function-schema)
  - array
    - [array.of(type: Schema): this](#arrayoftype-schema-this)
    - [array.json(): this](#arrayjson-this)
    - [array.length(length: number | Ref, message?: string | function): this](#arraylengthlength-number--ref-message-string--function-this)
    - [array.min(limit: number | Ref, message?: string | function): this](#arrayminlimit-number--ref-message-string--function-this)
    - [array.max(limit: number | Ref, message?: string | function): this](#arraymaxlimit-number--ref-message-string--function-this)
    - [array.ensure(): this](#arrayensure-this)
    - [array.compact(rejector: (value) => boolean): Schema](#arraycompactrejector-value--boolean-schema)
  - tuple
  - object
- [`object.shape(fields: object, noSortEdges?: Array<[string, string]>): Schema`](#objectshapefields-object-nosortedges-arraystring-string-schema)
    - [object.json(): this](#objectjson-this)
    - [object.concat(schemaB: ObjectSchema): ObjectSchema](#objectconcatschemab-objectschema-objectschema)
    - [object.pick(keys: string[]): Schema](#objectpickkeys-string-schema)
    - [object.omit(keys: string[]): Schema](#objectomitkeys-string-schema)
    - [object.from(fromKey: string, toKey: string, alias: boolean = false): this](#objectfromfromkey-string-tokey-string-alias-boolean--false-this)
    - [object.noUnknown(onlyKnownKeys: boolean = true, message?: string | function): Schema](#objectnounknownonlyknownkeys-boolean--true-message-string--function-schema)
    - [object.camelCase(): Schema](#objectcamelcase-schema)
    - [object.constantCase(): Schema](#objectconstantcase-schema)



Schema basics


Schema definitions, are comprised of parsing "transforms" which manipulate inputs into the desired shape and type, "tests", which make assertions over parsed data. Schema also store a bunch of "metadata", details about the schema itself, which can be used to improve error messages, build tools that dynamically consume schema, or serialize schema into another format.

In order to be maximally flexible yup allows running both parsing and assertions separately to match specific needs

Parsing: Transforms


Each built-in type implements basic type parsing, which comes in handy when parsing serialized data, such as JSON.
Additionally types implement type specific transforms that can be enabled.

  1. ```ts
  2. const num = number().cast('1'); // 1

  3. const obj = object({
  4.   firstName: string().lowercase().trim(),
  5. })
  6.   .camelCase()
  7.   .cast('{"first_name": "jAnE "}'); // { firstName: 'jane' }
  8. ```

Custom transforms can be added

  1. ```ts
  2. const reversedString = string()
  3.   .transform((currentValue) => currentValue.split('').reverse().join(''))
  4.   .cast('dlrow olleh'); // "hello world"
  5. ```

Transforms form a "pipeline", where the value of a previous transform is piped into the next one.
If the end value is undefined yup will apply the schema default if it's configured.

Watch out! values are not guaranteed to be valid types in transform functions. Previous transforms

may have failed. For example a number transform may be receive the input value, NaN, or a number.


Validation: Tests


Yup has robust support for assertions, or "tests", over input values. Tests assert that inputs conform to some
criteria. Tests are distinct from transforms, in that they do not change or alter the input (or its type)
and are usually reserved for checks that are hard, if not impossible, to represent in static types.

  1. ```ts
  2. string()
  3.   .min(3, 'must be at least 3 characters long')
  4.   .email('must be a valid email')
  5.   .validate('no'); // ValidationError
  6. ```

As with transforms, tests can be customized on the fly

  1. ```ts
  2. const jamesSchema = string().test(
  3.   'is-james',
  4.   (d) => `${d.path} is not James`,
  5.   (value) => value == null || value === 'James',
  6. );

  7. jamesSchema.validateSync('James'); // "James"

  8. jamesSchema.validateSync('Jane'); // ValidationError "this is not James"
  9. ```

Heads up: unlike transforms, value in a custom test is guaranteed to be the correct type

(in this case an optional string). It still may be undefined or null depending on your schema

in those cases, you may want to return true for absent values unless your transform makes presence

related assertions


Customizing errors


In the simplest case a test function returns true or false depending on the whether the check
passed. In the case of a failing test, yup will throw
a [ValidationError](#validationerrorerrors-string--arraystring-value-any-path-string) with your (or the default)
message for that test. ValidationErrors also contain a bunch of other metadata about the test,
including it's name, what arguments (if any) it was called with, and the path to the failing field
in the case of a nested validation.

Error messages can also be constructed on the fly to customize how the schema fails.

  1. ```ts
  2. const order = object({
  3.   no: number().required().
  4.   sku: string().test({
  5.     name: 'is-sku',
  6.     skipAbsent: true,
  7.     test(value, ctx) {
  8.       if (!value.startsWith('s-')) {
  9.         return ctx.createError({ message: 'SKU missing correct prefix' })
  10.       }
  11.       if (!value.endsWith('-42a')) {
  12.         return ctx.createError({ message: 'SKU missing correct suffix' })
  13.       }
  14.       if (value.length < 10) {
  15.         return ctx.createError({ message: 'SKU is not the right length' })
  16.       }
  17.       return true
  18.     }
  19.   })
  20. })

  21. order.validate({ no: 1234, sku: 's-1a45-14a' })
  22. ```

Composition and Reuse


Schema are immutable, each method call returns a new schema object. Reuse and pass them around without
fear of mutating another instance.

  1. ```ts
  2. const optionalString = string().optional();

  3. const definedString = optionalString.defined();

  4. const value = undefined;
  5. optionalString.isValid(value); // true
  6. definedString.isValid(value); // false
  7. ```

TypeScript integration


Yup schema produce static TypeScript interfaces. Use InferType to extract that interface:

  1. ```ts
  2. import * as yup from 'yup';

  3. const personSchema = yup.object({
  4.   firstName: yup.string().defined(),
  5.   nickName: yup.string().default('').nullable(),
  6.   sex: yup
  7.     .mixed()
  8.     .oneOf(['male', 'female', 'other'] as const)
  9.     .defined(),
  10.   email: yup.string().nullable().email(),
  11.   birthDate: yup.date().nullable().min(new Date(1900, 0, 1)),
  12. });

  13. interface Person extends yup.InferType<typeof personSchema> {
  14.   // using interface instead of type generally gives nicer editor feedback
  15. }
  16. ```

Schema defaults


A schema's default is used when casting produces an undefined output value. Because of this,
setting a default affects the output type of the schema, essentially marking it as "defined()".

  1. ```ts
  2. import { string } from 'yup';

  3. const value: string = string().default('hi').validate(undefined);

  4. // vs

  5. const value: string | undefined = string().validate(undefined);
  6. ```

Ensuring a schema matches an existing type


In some cases a TypeScript type already exists, and you want to ensure that
your schema produces a compatible type:

  1. ```ts
  2. import { object, number, string, ObjectSchema } from 'yup';

  3. interface Person {
  4.   name: string;
  5.   age?: number;
  6.   sex: 'male' | 'female' | 'other' | null;
  7. }

  8. // will raise a compile-time type error if the schema does not produce a valid Person
  9. const schema: ObjectSchema<Person> = object({
  10.   name: string().defined(),
  11.   age: number().optional(),
  12.   sex: string<'male' | 'female' | 'other'>().nullable().defined();
  13. });

  14. // ❌ errors:
  15. // "Type 'number | undefined' is not assignable to type 'string'."
  16. const badSchema: ObjectSchema<Person> = object({
  17.   name: number(),
  18. });

  19. ```

Extending built-in schema with new methods


You can use TypeScript's interface merging behavior to extend the schema types
if needed. Type extensions should go in an "ambient" type definition file such as your
globals.d.ts. Remember to actually extend the yup type in your application code!

Watch out! merging only works if the type definition is _exactly_ the same, including

generics. Consult the yup source code for each type to ensure you are defining it correctly


  1. ```ts
  2. // globals.d.ts
  3. declare module 'yup' {
  4.   interface StringSchema<TType, TContext, TDefault, TFlags> {
  5.     append(appendStr: string): this;
  6.   }
  7. }

  8. // app.ts
  9. import { addMethod, string } from 'yup';

  10. addMethod(string, 'append', function append(appendStr: string) {
  11.   return this.transform((value) => `${value}${appendStr}`);
  12. });

  13. string().append('~~~~').cast('hi'); // 'hi~~~~'
  14. ```

TypeScript configuration


You must have the strictNullChecks compiler option enabled for type inference to work.

We also recommend settings strictFunctionTypes to false, for functionally better types. Yes
this reduces overall soundness, however TypeScript already disables this check
for methods and constructors (note from TS docs):

During development of this feature, we discovered a large number of inherently

unsafe class hierarchies, including some in the DOM. Because of this,

the setting only applies to functions written in function syntax, not to those in method syntax:


Your mileage will vary, but we've found that this check doesn't prevent many of
real bugs, while increasing the amount of onerous explicit type casting in apps.

Error message customization


Default error messages can be customized for when no message is provided with a validation test.
If any message is missing in the custom dictionary the error message will default to Yup's one.

  1. ``` js
  2. import { setLocale } from 'yup';

  3. setLocale({
  4.   mixed: {
  5.     default: 'Não é válido',
  6.   },
  7.   number: {
  8.     min: 'Deve ser maior que ${min}',
  9.   },
  10. });

  11. // now use Yup schemas AFTER you defined your custom dictionary
  12. let schema = yup.object().shape({
  13.   name: yup.string(),
  14.   age: yup.number().min(18),
  15. });

  16. try {
  17.   await schema.validate({ name: 'jimmy', age: 11 });
  18. } catch (err) {
  19.   err.name; // => 'ValidationError'
  20.   err.errors; // => ['Deve ser maior que 18']
  21. }
  22. ```

localization and i18n


If you need multi-language support, yup has got you covered. The function setLocale accepts functions that can be used to
generate error objects with translation keys and values. These can be fed it into your favorite i18n library.

  1. ``` js
  2. import { setLocale } from 'yup';

  3. setLocale({
  4.   // use constant translation keys for messages without values
  5.   mixed: {
  6.     default: 'field_invalid',
  7.   },
  8.   // use functions to generate an error object that includes the value from the schema
  9.   number: {
  10.     min: ({ min }) => ({ key: 'field_too_short', values: { min } }),
  11.     max: ({ max }) => ({ key: 'field_too_big', values: { max } }),
  12.   },
  13. });

  14. // ...

  15. let schema = yup.object().shape({
  16.   name: yup.string(),
  17.   age: yup.number().min(18),
  18. });

  19. try {
  20.   await schema.validate({ name: 'jimmy', age: 11 });
  21. } catch (err) {
  22.   messages = err.errors.map((err) => i18next.t(err.key));
  23. }
  24. ```

API


yup


The module export.

  1. ```ts
  2. // core schema
  3. import {
  4.   mixed,
  5.   string,
  6.   number,
  7.   boolean,
  8.   bool,
  9.   date,
  10.   object,
  11.   array,
  12.   ref,
  13.   lazy,
  14. } from 'yup';

  15. // Classes
  16. import {
  17.   Schema,
  18.   MixedSchema,
  19.   StringSchema,
  20.   NumberSchema,
  21.   BooleanSchema,
  22.   DateSchema,
  23.   ArraySchema,
  24.   ObjectSchema,
  25. } from 'yup';

  26. // Types
  27. import type { InferType, ISchema, AnySchema, AnyObjectSchema } from 'yup';
  28. ```

reach(schema: Schema, path: string, value?: object, context?: object): Schema


For nested schemas, reach will retrieve an inner schema based on the provided path.

For nested schemas that need to resolve dynamically, you can provide a value and optionally
a context object.

  1. ``` js
  2. import { reach } from 'yup';

  3. let schema = object({
  4.   nested: object({
  5.     arr: array(object({ num: number().max(4) })),
  6.   }),
  7. });

  8. reach(schema, 'nested.arr.num');
  9. reach(schema, 'nested.arr[].num');
  10. reach(schema, 'nested.arr[1].num');
  11. reach(schema, 'nested["arr"][1].num');
  12. ```

addMethod(schemaType: Schema, name: string, method: ()=> Schema): void


Adds a new method to the core schema types. A friendlier convenience method for schemaType.prototype[name] = method.

  1. ```ts
  2. import { addMethod, date } from 'yup';

  3. addMethod(date, 'format', function format(formats, parseStrict) {
  4.   return this.transform((value, originalValue, ctx) => {
  5.     if (ctx.isType(value)) return value;

  6.     value = Moment(originalValue, formats, parseStrict);

  7.     return value.isValid() ? value.toDate() : new Date('');
  8.   });
  9. });
  10. ```

If you want to add a method to ALL schema types, extend the abstract base class: Schema

  1. ```ts
  2. import { addMethod, Schema } from 'yup';

  3. addMethod(Schema, 'myMethod', ...)
  4. ```

ref(path: string, options: { contextPrefix: string }): Ref


Creates a reference to another sibling or sibling descendant field. Refs are resolved
at _validation/cast time_ and supported where specified. Refs are evaluated in the proper order so that
the ref value is resolved before the field using the ref (be careful of circular dependencies!).

  1. ``` js
  2. import { ref, object, string } from 'yup';

  3. let schema = object({
  4.   baz: ref('foo.bar'),
  5.   foo: object({
  6.     bar: string(),
  7.   }),
  8.   x: ref('$x'),
  9. });

  10. schema.cast({ foo: { bar: 'boom' } }, { context: { x: 5 } });
  11. // => { baz: 'boom',  x: 5, foo: { bar: 'boom' } }
  12. ```

lazy((value: any) => Schema): Lazy


Creates a schema that is evaluated at validation/cast time. Useful for creating
recursive schema like Trees, for polymorphic fields and arrays.

CAUTION! When defining parent-child recursive object schema, you want to reset the default()
to null on the child—otherwise the object will infinitely nest itself when you cast it!

  1. ``` js
  2. let node = object({
  3.   id: number(),
  4.   child: yup.lazy(() => node.default(undefined)),
  5. });

  6. let renderable = yup.lazy((value) => {
  7.   switch (typeof value) {
  8.     case 'number':
  9.       return number();
  10.     case 'string':
  11.       return string();
  12.     default:
  13.       return mixed();
  14.   }
  15. });

  16. let renderables = array().of(renderable);
  17. ```

#### `ValidationError(errors: string | Array, value: any, path: string)`

Thrown on failed validations, with the following properties

- name: "ValidationError"
- type: the specific test type or test "name", that failed.
- value: The field value that was tested;
- params?: The test inputs, such as max value, regex, etc;
- path: a string, indicating where there error was thrown. path is empty at the root level.
- errors: array of error messages
- inner: in the case of aggregate errors, inner is an array of ValidationErrors throw earlier in the
  validation chain. When the abortEarly option is false this is where you can inspect each error thrown,
  alternatively, errors will have all of the messages from each inner error.

Schema


Schema is the abstract base class that all schema type inherit from. It provides a number of base methods and properties
to all other schema types.

Note: unless you are creating a custom schema type, Schema should never be used directly. For unknown/any types use [mixed()](#mixed)


Schema.clone(): Schema


Creates a deep copy of the schema. Clone is used internally to return a new schema with every schema state change.

Schema.label(label: string): Schema


Overrides the key name which is used in error messages.

Schema.meta(metadata: object): Schema


Adds to a metadata object, useful for storing data with a schema, that doesn't belong
the cast object itself.

Schema.describe(options?: ResolveOptions): SchemaDescription


Collects schema details (like meta, labels, and active tests) into a serializable
description object.

  1. ```ts
  2. const schema = object({
  3.   name: string().required(),
  4. });

  5. const description = schema.describe();
  6. ```

For schema with dynamic components (references, lazy, or conditions), describe requires
more context to accurately return the schema description. In these cases provide options

  1. ```ts
  2. import { ref, object, string, boolean } from 'yup';

  3. let schema = object({
  4.   isBig: boolean(),
  5.   count: number().when('isBig', {
  6.     is: true,
  7.     then: (schema) => schema.min(5),
  8.     otherwise: (schema) => schema.min(0),
  9.   }),
  10. });

  11. schema.describe({ value: { isBig: true } });
  12. ```

And below is are the description types, which differ a bit depending on the schema type.

  1. ```ts
  2. interface SchemaDescription {
  3.   type: string;
  4.   label?: string;
  5.   meta: object | undefined;
  6.   oneOf: unknown[];
  7.   notOneOf: unknown[];
  8.   nullable: boolean;
  9.   optional: boolean;
  10.   tests: Array<{ name?: string; params: ExtraParams | undefined }>;

  11.   // Present on object schema descriptions
  12.   fields: Record<string, SchemaFieldDescription>;

  13.   // Present on array schema descriptions
  14.   innerType?: SchemaFieldDescription;
  15. }

  16. type SchemaFieldDescription =
  17.   | SchemaDescription
  18.   | SchemaRefDescription
  19.   | SchemaLazyDescription;

  20. interface SchemaRefDescription {
  21.   type: 'ref';
  22.   key: string;
  23. }

  24. interface SchemaLazyDescription {
  25.   type: string;
  26.   label?: string;
  27.   meta: object | undefined;
  28. }
  29. ```

Schema.concat(schema: Schema): Schema


Creates a new instance of the schema by combining two schemas. Only schemas of the same type can be concatenated.
concat is not a "merge" function in the sense that all settings from the provided schema, override ones in the
base, including type, presence and nullability.

  1. ```ts
  2. mixed<string>().defined().concat(mixed<number>().nullable());

  3. // produces the equivalent to:

  4. mixed<number>().defined().nullable();
  5. ```

#### `Schema.validate(value: any, options?: object): Promise, ValidationError>`

Returns the parses and validates an input value, returning the parsed value or throwing an error. This method is asynchronous and returns a Promise object, that is fulfilled with the value, or rejected
with a ValidationError.

  1. ``` js
  2. value = await schema.validate({ name: 'jimmy', age: 24 });
  3. ```

Provide options to more specifically control the behavior of validate.

  1. ``` js
  2. interface Options {
  3.   // when true, parsing is skipped an the input is validated "as-is"
  4.   strict: boolean = false;
  5.   // Throw on the first error or collect and return all
  6.   abortEarly: boolean = true;
  7.   // Remove unspecified keys from objects
  8.   stripUnknown: boolean = false;
  9.   // when `false` validations will be performed shallowly
  10.   recursive: boolean = true;
  11.   // External values that can be provided to validations and conditionals
  12.   context?: object;
  13. }
  14. ```

#### `Schema.validateSync(value: any, options?: object): InferType`

Runs validatations synchronously _if possible_ and returns the resulting value,
or throws a ValidationError. Accepts all the same options as validate.

Synchronous validation only works if there are no configured async tests, e.g tests that return a Promise.
For instance this will work:

  1. ``` js
  2. let schema = number().test(
  3.   'is-42',
  4.   "this isn't the number i want",
  5.   (value) => value != 42,
  6. );

  7. schema.validateSync(23); // throws ValidationError
  8. ```

however this will not:

  1. ``` js
  2. let schema = number().test('is-42', "this isn't the number i want", (value) =>
  3.   Promise.resolve(value != 42),
  4. );

  5. schema.validateSync(42); // throws Error
  6. ```

#### `Schema.validateAt(path: string, value: any, options?: object): Promise, ValidationError>`

Validate a deeply nested path within the schema. Similar to how reach works,
but uses the resulting schema as the subject for validation.

Note! The value here is the _root_ value relative to the starting schema, not the value at the nested path.


  1. ``` js
  2. let schema = object({
  3.   foo: array().of(
  4.     object({
  5.       loose: boolean(),
  6.       bar: string().when('loose', {
  7.         is: true,
  8.         otherwise: (schema) => schema.strict(),
  9.       }),
  10.     }),
  11.   ),
  12. });

  13. let rootValue = {
  14.   foo: [{ bar: 1 }, { bar: 1, loose: true }],
  15. };

  16. await schema.validateAt('foo[0].bar', rootValue); // => ValidationError: must be a string

  17. await schema.validateAt('foo[1].bar', rootValue); // => '1'
  18. ```

#### `Schema.validateSyncAt(path: string, value: any, options?: object): InferType`

Same as validateAt but synchronous.

#### `Schema.isValid(value: any, options?: object): Promise`

Returns true when the passed in value matches the schema. isValid
is asynchronous and returns a Promise object.

Takes the same options as validate().

Schema.isValidSync(value: any, options?: object): boolean


Synchronously returns true when the passed in value matches the schema.

Takes the same options as validateSync() and has the same caveats around async tests.

#### `Schema.cast(value: any, options = {}): InferType`

Attempts to coerce the passed in value to a value that matches the schema. For example: '5' will
cast to 5 when using the number() type. Failed casts generally return null, but may also
return results like NaN and unexpected strings.

Provide options to more specifically control the behavior of validate.

  1. ``` js
  2. interface CastOptions<TContext extends {}> {
  3.   // Remove undefined properties from objects
  4.   stripUnknown: boolean = false;

  5.   // Throws a TypeError if casting doesn't produce a valid type
  6.   // note that the TS return type is inaccurate when this is `false`, use with caution
  7.   assert?: boolean = true;

  8.   // External values that used to resolve conditions and references
  9.   context?: TContext;
  10. }
  11. ```

#### `Schema.isType(value: any): value is InferType`

Runs a type check against the passed in value. It returns true if it matches,
it does not cast the value. When nullable() is set null is considered a valid value of the type.
You should use isType for all Schema type checks.

Schema.strict(enabled: boolean = false): Schema


Sets the strict option to true. Strict schemas skip coercion and transformation attempts,
validating the value "as is".

Schema.strip(enabled: boolean = true): Schema


Marks a schema to be removed from an output object. Only works as a nested schema.

  1. ``` js
  2. let schema = object({
  3.   useThis: number(),
  4.   notThis: string().strip(),
  5. });

  6. schema.cast({ notThis: 'foo', useThis: 4 }); // => { useThis: 4 }
  7. ```

Schema with strip enabled have an inferred type of never, allowing them to be
removed from the overall type:

  1. ```ts
  2. let schema = object({
  3.   useThis: number(),
  4.   notThis: string().strip(),
  5. });

  6. InferType<typeof schema>; /*
  7. {
  8.    useThis?: number | undefined
  9. }
  10. */
  11. ```

Schema.withMutation(builder: (current: Schema) => void): void


First the legally required Rich Hickey quote:

If a tree falls in the woods, does it make a sound?

>

If a pure function mutates some local data in order to produce an immutable return value, is that ok?


withMutation allows you to mutate the schema in place, instead of the default behavior which clones before each change. Generally this isn't necessary since the vast majority of schema changes happen during the initial
declaration, and only happen once over the lifetime of the schema, so performance isn't an issue.
However certain mutations _do_ occur at cast/validation time, (such as conditional schema using [when()](#Schemawhenkeys-string--arraystring-builder-object--value-schema-schema-schema)), or
when instantiating a schema object.

  1. ``` js
  2. object()
  3.   .shape({ key: string() })
  4.   .withMutation((schema) => {
  5.     return arrayOfObjectTests.forEach((test) => {
  6.       schema.test(test);
  7.     });
  8.   });
  9. ```

Schema.default(value: any): Schema


Sets a default value to use when the value is undefined.
Defaults are created after transformations are executed, but before validations, to help ensure that safe
defaults are specified. The default value will be cloned on each use, which can incur performance penalty
for objects and arrays. To avoid this overhead you can also pass a function that returns a new default.
Note that null is considered a separate non-empty value.

  1. ``` js
  2. yup.string.default('nothing');

  3. yup.object.default({ number: 5 }); // object will be cloned every time a default is needed

  4. yup.object.default(() => ({ number: 5 })); // this is cheaper

  5. yup.date.default(() => new Date()); // also helpful for defaults that change over time
  6. ```

Schema.getDefault(options?: object): Any


Retrieve a previously set default value. getDefault will resolve any conditions that may alter the default. Optionally pass options with context (for more info on context see Schema.validate).

Schema.nullable(): Schema


Indicates that null is a valid value for the schema. Without nullable()
null is treated as a different type and will fail Schema.isType() checks.

  1. ```ts
  2. const schema = number().nullable();

  3. schema.cast(null); // null

  4. InferType<typeof schema>; // number | null
  5. ```

Schema.nonNullable(): Schema


The opposite of nullable, removes null from valid type values for the schema.
Schema are non nullable by default.

  1. ```ts
  2. const schema = number().nonNullable();

  3. schema.cast(null); // TypeError

  4. InferType<typeof schema>; // number
  5. ```

Schema.defined(): Schema


Require a value for the schema. All field values apart from undefined meet this requirement.

  1. ```ts
  2. const schema = string().defined();

  3. schema.cast(undefined); // TypeError

  4. InferType<typeof schema>; // string
  5. ```

Schema.optional(): Schema


The opposite of defined() allows undefined values for the given type.

  1. ```ts
  2. const schema = string().optional();

  3. schema.cast(undefined); // undefined

  4. InferType<typeof schema>; // string | undefined
  5. ```

Schema.required(message?: string | function): Schema


Mark the schema as required, which will not allow undefined or null as a value. required
negates the effects of calling optional() and nullable()

Watch out! [string().required](#stringrequiredmessage-string--function-schema)) works a little

different and additionally prevents empty string values ('') when required.


Schema.notRequired(): Schema Alias: optional()


Mark the schema as not required. This is a shortcut for schema.nonNullable().defined();

Schema.typeError(message: string): Schema


Define an error message for failed type checks. The ${value} and ${type} interpolation can
be used in the message argument.

#### `Schema.oneOf(arrayOfValues: Array, message?: string | function): Schema` Alias: `equals`

Only allow values from set of values. Values added are removed from any notOneOf values if present.
The ${values} interpolation can be used in the message argument. If a ref or refs are provided,
the ${resolved} interpolation can be used in the message argument to get the resolved values that were checked
at validation time.

Note that undefined does not fail this validator, even when undefined is not included in arrayOfValues.
If you don't want undefined to be a valid value, you can use Schema.required.

  1. ``` js
  2. let schema = yup.mixed().oneOf(['jimmy', 42]);

  3. await schema.isValid(42); // => true
  4. await schema.isValid('jimmy'); // => true
  5. await schema.isValid(new Date()); // => false
  6. ```

#### `Schema.notOneOf(arrayOfValues: Array, message?: string | function)`

Disallow values from a set of values. Values added are removed from oneOf values if present.
The ${values} interpolation can be used in the message argument. If a ref or refs are provided,
the ${resolved} interpolation can be used in the message argument to get the resolved values that were checked
at validation time.

  1. ``` js
  2. let schema = yup.mixed().notOneOf(['jimmy', 42]);

  3. await schema.isValid(42); // => false
  4. await schema.isValid(new Date()); // => true
  5. ```

Schema.when(keys: string | string[], builder: object | (values: any[], schema) => Schema): Schema


Adjust the schema based on a sibling or sibling children fields. You can provide an object
literal where the key is is value or a matcher function, then provides the true schema and/or
otherwise for the failure condition.

is conditions are strictly compared (===) if you want to use a different form of equality you
can provide a function like: is: (value) => value == true.

You can also prefix properties with $ to specify a property that is dependent
on context passed in by validate() or cast instead of the input value.

when conditions are additive.

  1. ``` js
  2. let schema = object({
  3.   isBig: boolean(),
  4.   count: number()
  5.     .when('isBig', {
  6.       is: true, // alternatively: (val) => val == true
  7.       then: (schema) => schema.min(5),
  8.       otherwise: (schema) => schema.min(0),
  9.     })
  10.     .when('$other', ([other], schema) =>
  11.       other === 4 ? schema.max(6) : schema,
  12.     ),
  13. });

  14. await schema.validate(value, { context: { other: 4 } });
  15. ```

You can also specify more than one dependent key, in which case each value will be spread as an argument.

  1. ``` js
  2. let schema = object({
  3.   isSpecial: boolean(),
  4.   isBig: boolean(),
  5.   count: number().when(['isBig', 'isSpecial'], {
  6.     is: true, // alternatively: (isBig, isSpecial) => isBig && isSpecial
  7.     then: (schema) => schema.min(5),
  8.     otherwise: (schema) => schema.min(0),
  9.   }),
  10. });

  11. await schema.validate({
  12.   isBig: true,
  13.   isSpecial: true,
  14.   count: 10,
  15. });
  16. ```

Alternatively you can provide a function that returns a schema, called with an array of values for each provided key the current schema.

  1. ``` js
  2. let schema = yup.object({
  3.   isBig: yup.boolean(),
  4.   count: yup.number().when('isBig', ([isBig], schema) => {
  5.     return isBig ? schema.min(5) : schema.min(0);
  6.   }),
  7. });

  8. await schema.validate({ isBig: false, count: 4 });
  9. ```

Schema.test(name: string, message: string | function | any, test: function): Schema


Adds a test function to the validation chain. Tests are run after any object is cast.
Many types have some tests built in, but you can create custom ones easily.
In order to allow asynchronous custom validations _all_ (or no) tests are run asynchronously.
A consequence of this is that test execution order cannot be guaranteed.

All tests must provide a name, an error message and a validation function that must return
true when the current value is valid and false or a ValidationError otherwise.
To make a test async return a promise that resolves true or false or a ValidationError.

For the message argument you can provide a string which will interpolate certain values
if specified using the ${param} syntax. By default all test messages are passed a path value
which is valuable in nested schemas.

The test function is called with the current value. For more advanced validations you can
use the alternate signature to provide more options (see below):

  1. ``` js
  2. let jimmySchema = string().test(
  3.   'is-jimmy',
  4.   '${path} is not Jimmy',
  5.   (value, context) => value === 'jimmy',
  6. );

  7. // or make it async by returning a promise
  8. let asyncJimmySchema = string()
  9.   .label('First name')
  10.   .test(
  11.     'is-jimmy',
  12.     ({ label }) => `${label} is not Jimmy`, // a message can also be a function
  13.     async (value, testContext) =>
  14.       (await fetch('/is-jimmy/' + value)).responseText === 'true',
  15.   );

  16. await schema.isValid('jimmy'); // => true
  17. await schema.isValid('john'); // => false
  18. ```

Test functions are called with a special context value, as the second argument, that exposes some useful metadata
and functions. For non arrow functions, the test context is also set as the function this. Watch out, if you access
it via this it won't work in an arrow function.

- testContext.path: the string path of the current validation
- testContext.schema: the resolved schema object that the test is running against.
- testContext.options: the options object that validate() or isValid() was called with
- testContext.parent: in the case of nested schema, this is the value of the parent object
- testContext.originalValue: the original value that is being tested
- testContext.createError(Object: { path: String, message: String, params: Object }): create and return a
  validation error. Useful for dynamically setting the path, params, or more likely, the error message.
  If either option is omitted it will use the current path, or default message.

Schema.test(options: object): Schema


Alternative test(..) signature. options is an object containing some of the following options:

  1. ``` js
  2. Options = {
  3.   // unique name identifying the test
  4.   name: string;
  5.   // test function, determines schema validity
  6.   test: (value: any) => boolean;
  7.   // the validation error message
  8.   message: string;
  9.   // values passed to message for interpolation
  10.   params: ?object;
  11.   // mark the test as exclusive, meaning only one test of the same name can be active at once
  12.   exclusive: boolean = false;
  13. }
  14. ```

In the case of mixing exclusive and non-exclusive tests the following logic is used.
If a non-exclusive test is added to a schema with an exclusive test of the same name
the exclusive test is removed and further tests of the same name will be stacked.

If an exclusive test is added to a schema with non-exclusive tests of the same name
the previous tests are removed and further tests of the same name will replace each other.

  1. ``` js
  2. let max = 64;
  3. let schema = yup.string().test({
  4.   name: 'max',
  5.   exclusive: true,
  6.   params: { max },
  7.   message: '${path} must be less than ${max} characters',
  8.   test: (value) => value == null || value.length <= max,
  9. });
  10. ```

Schema.transform((currentValue: any, originalValue: any) => any): Schema


Adds a transformation to the transform chain. Transformations are central to the casting process,
default transforms for each type coerce values to the specific type (as verified by [isType()](#Schemaistypevalue-any-boolean)). transforms are run before validations and only applied when the schema is not marked as strict (the default). Some types have built in transformations.

Transformations are useful for arbitrarily altering how the object is cast, however, you should take care
not to mutate the passed in value. Transforms are run sequentially so each value represents the
current state of the cast, you can use the originalValue param if you need to work on the raw initial value.

  1. ``` js
  2. let schema = string().transform((value, originalvalue) => {
  3.   return this.isType(value) && value !== null ? value.toUpperCase() : value;
  4. });

  5. schema.cast('jimmy'); // => 'JIMMY'
  6. ```

Each types will handle basic coercion of values to the proper type for you, but occasionally
you may want to adjust or refine the default behavior. For example, if you wanted to use a different
date parsing strategy than the default one you could do that with a transform.

  1. ``` js
  2. module.exports = function (formats = 'MMM dd, yyyy') {
  3.   return date().transform((value, originalValue, context) => {
  4.     // check to see if the previous transform already parsed the date
  5.     if (context.isType(value)) return value;

  6.     // the default coercion failed so let's try it with Moment.js instead
  7.     value = Moment(originalValue, formats);

  8.     // if it's valid return the date object, otherwise return an `InvalidDate`
  9.     return value.isValid() ? value.toDate() : new Date('');
  10.   });
  11. };
  12. ```

mixed


Creates a schema that matches all types, or just the ones you configure. Inherits from [Schema](#Schema).
Mixed types extends {} by default instead of any or unknown. This is because in TypeScript {} means
anything that isn't null or undefined which yup treats distinctly.

  1. ```ts
  2. import { mixed, InferType } from 'yup';

  3. let schema = mixed().nullable();

  4. schema.validateSync('string'); // 'string';

  5. schema.validateSync(1); // 1;

  6. schema.validateSync(new Date()); // Date;

  7. InferType<typeof schema>; // {} | undefined

  8. InferType<typeof schema.nullable().defined()>; // {} | null
  9. ```

Custom types can be implemented by passing a type check function. This will also
narrow the TypeScript type for the schema.

  1. ```ts
  2. import { mixed, InferType } from 'yup';

  3. let objectIdSchema = yup
  4.   .mixed((input): input is ObjectId => input instanceof ObjectId)
  5.   .transform((value: any, input, ctx) => {
  6.     if (ctx.typeCheck(value)) return value;
  7.     return new ObjectId(value);
  8.   });

  9. await objectIdSchema.validate(ObjectId('507f1f77bcf86cd799439011')); // ObjectId("507f1f77bcf86cd799439011")

  10. await objectIdSchema.validate('507f1f77bcf86cd799439011'); // ObjectId("507f1f77bcf86cd799439011")

  11. InferType<typeof objectIdSchema>; // ObjectId
  12. ```

string


Define a string schema. Inherits from [Schema](#Schema).

  1. ``` js
  2. let schema = yup.string();

  3. await schema.isValid('hello'); // => true
  4. ```

By default, the cast logic of string is to call toString on the value if it exists.

empty values are not coerced (use ensure() to coerce empty values to empty strings).

Failed casts return the input value.

string.required(message?: string | function): Schema


The same as the mixed() schema required, except that empty strings are also considered 'missing' values.

string.length(limit: number | Ref, message?: string | function): Schema


Set a required length for the string value. The ${length} interpolation can be used in the message argument

string.min(limit: number | Ref, message?: string | function): Schema


Set a minimum length limit for the string value. The ${min} interpolation can be used in the message argument

string.max(limit: number | Ref, message?: string | function): Schema


Set a maximum length limit for the string value. The ${max} interpolation can be used in the message argument

string.matches(regex: Regex, message?: string | function): Schema


Provide an arbitrary regex to match the value against.

  1. ``` js
  2. let schema = string().matches(/(hi|bye)/);

  3. await schema.isValid('hi'); // => true
  4. await schema.isValid('nope'); // => false
  5. ```

string.matches(regex: Regex, options: { message: string, excludeEmptyString: bool }): Schema


An alternate signature for string.matches with an options object. excludeEmptyString, when true,
short circuits the regex test when the value is an empty string, making it a easier to avoid
matching nothing without complicating the regex.

  1. ``` js
  2. let schema = string().matches(/(hi|bye)/, { excludeEmptyString: true });

  3. await schema.isValid(''); // => true
  4. ```

string.email(message?: string | function): Schema


Validates the value as an email address using the same regex as defined by the HTML spec.

WATCH OUT: Validating email addresses is nearly impossible with just code. Different
clients and servers accept different things and many diverge from the various specs defining
"valid" emails. The ONLY real way to validate an email address is to send a verification email
to it and check that the user got it. With that in mind, yup picks a relatively simple regex
that does not cover all cases, but aligns with browser input validation behavior since HTML
forms are a common use case for yup.

If you have more specific needs please override the email method with your own logic or regex:

  1. ```ts
  2. yup.addMethod(yup.string, 'email', function validateEmail(message) {
  3.   return this.matches(myEmailRegex, {
  4.     message,
  5.     name: 'email',
  6.     excludeEmptyString: true,
  7.   });
  8. });
  9. ```

string.url(message?: string | function): Schema


Validates the value as a valid URL via a regex.

string.uuid(message?: string | function): Schema


Validates the value as a valid UUID via a regex.

string.ensure(): Schema


Transforms undefined and null values to an empty string along with
setting the default to an empty string.

string.trim(message?: string | function): Schema


Transforms string values by removing leading and trailing whitespace. If
strict() is set it will only validate that the value is trimmed.

string.lowercase(message?: string | function): Schema


Transforms the string value to lowercase. If strict() is set it
will only validate that the value is lowercase.

string.uppercase(message?: string | function): Schema


Transforms the string value to uppercase. If strict() is set it
will only validate that the value is uppercase.

number


Define a number schema. Inherits from [Schema](#Schema).

  1. ``` js
  2. let schema = yup.number();

  3. await schema.isValid(10); // => true
  4. ```

The default cast logic of number is: [parseFloat](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseFloat).

Failed casts return NaN.

number.min(limit: number | Ref, message?: string | function): Schema


Set the minimum value allowed. The ${min} interpolation can be used in the
message argument.

number.max(limit: number | Ref, message?: string | function): Schema


Set the maximum value allowed. The ${max} interpolation can be used in the
message argument.

number.lessThan(max: number | Ref, message?: string | function): Schema


Value must be less than max. The ${less} interpolation can be used in the
message argument.

number.moreThan(min: number | Ref, message?: string | function): Schema


Value must be strictly greater than min. The ${more} interpolation can be used in the
message argument.

number.positive(message?: string | function): Schema


Value must be a positive number.

number.negative(message?: string | function): Schema


Value must be a negative number.

number.integer(message?: string | function): Schema


Validates that a number is an integer.

number.truncate(): Schema


Transformation that coerces the value to an integer by stripping off the digits
to the right of the decimal point.

number.round(type: 'floor' | 'ceil' | 'trunc' | 'round' = 'round'): Schema


Adjusts the value via the specified method of Math (defaults to 'round').

boolean


Define a boolean schema. Inherits from [Schema](#Schema).

  1. ``` js
  2. let schema = yup.boolean();

  3. await schema.isValid(true); // => true
  4. ```

date


Define a Date schema. By default ISO date strings will parse correctly,
for more robust parsing options see the extending schema types at the end of the readme.
Inherits from [Schema](#Schema).

  1. ``` js
  2. let schema = yup.date();

  3. await schema.isValid(new Date()); // => true
  4. ```

The default cast logic of date is pass the value to the Date constructor, failing that, it will attempt
to parse the date as an ISO date string.

Failed casts return an invalid Date.

date.min(limit: Date | string | Ref, message?: string | function): Schema


Set the minimum date allowed. When a string is provided it will attempt to cast to a date first
and use the result as the limit.

date.max(limit: Date | string | Ref, message?: string | function): Schema


Set the maximum date allowed, When a string is provided it will attempt to cast to a date first
and use the result as the limit.

array


Define an array schema. Arrays can be typed or not, When specifying the element type, cast and isValid
will apply to the elements as well. Options passed into isValid are also passed to child schemas.

Inherits from [Schema](#Schema).

  1. ``` js
  2. let schema = yup.array().of(yup.number().min(2));

  3. await schema.isValid([2, 3]); // => true
  4. await schema.isValid([1, -24]); // => false

  5. schema.cast(['2', '3']); // => [2, 3]
  6. ```

You can also pass a subtype schema to the array constructor as a convenience.

  1. ``` js
  2. array().of(yup.number());
  3. // or
  4. array(yup.number());
  5. ```

Arrays have no default casting behavior.

array.of(type: Schema): this


Specify the schema of array elements. of() is optional and when omitted the array schema will
not validate its contents.

array.json(): this


Attempt to parse input string values as JSON using [JSON.parse](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse).

array.length(length: number | Ref, message?: string | function): this


Set a specific length requirement for the array. The ${length} interpolation can be used in the message argument.

array.min(limit: number | Ref, message?: string | function): this


Set a minimum length limit for the array. The ${min} interpolation can be used in the message argument.

array.max(limit: number | Ref, message?: string | function): this


Set a maximum length limit for the array. The ${max} interpolation can be used in the message argument.

array.ensure(): this


Ensures that the value is an array, by setting the default to [] and transforming null and undefined
values to an empty array as well. Any non-empty, non-array value will be wrapped in an array.

  1. ``` js
  2. array().ensure().cast(null); // => []
  3. array().ensure().cast(1); // => [1]
  4. array().ensure().cast([1]); // => [1]
  5. ```

array.compact(rejector: (value) => boolean): Schema


Removes falsey values from the array. Providing a rejecter function lets you specify the rejection criteria yourself.

  1. ``` js
  2. array().compact().cast(['', 1, 0, 4, false, null]); // => [1, 4]

  3. array()
  4.   .compact(function (v) {
  5.     return v == null;
  6.   })
  7.   .cast(['', 1, 0, 4, false, null]); // => ['', 1, 0, 4, false]
  8. ```

tuple


Tuples, are fixed length arrays where each item has a distinct type.

Inherits from [Schema](#Schema).

  1. ``` js
  2. import { tuple, string, number, InferType } from 'yup';

  3. let schema = tuple([
  4.   string().label('name'),
  5.   number().label('age').positive().integer(),
  6. ]);

  7. await schema.validate(['James', 3]); // ['James', 3]

  8. await schema.validate(['James', -24]); // => ValidationError: age must be a positive number

  9. InferType<typeof schema> // [string, number] | undefined
  10. ```

tuples have no default casting behavior.

object


Define an object schema. Options passed into isValid are also passed to child schemas.
Inherits from [Schema](#Schema).

  1. ``` js
  2. yup.object({
  3.   name: string().required(),
  4.   age: number().required().positive().integer(),
  5.   email: string().email(),
  6.   website: string().url(),
  7. });
  8. ```

object schema do not have any default transforms applied.

Object schema defaults


Object schema come with a default value already set, which "builds" out the object shape, a
sets any defaults for fields:

  1. ``` js
  2. const schema = object({
  3.   name: string().default(''),
  4. });

  5. schema.default(); // -> { name: '' }
  6. ```

This may be a bit surprising, but is usually helpful since it allows large, nested
schema to create default values that fill out the whole shape and not just the root object. There is
one gotcha! though. For nested object schema that are optional but include non optional fields
may fail in unexpected ways:

  1. ``` js
  2. const schema = object({
  3.   id: string().required(),
  4.   names: object({
  5.     first: string().required(),
  6.   }),
  7. });

  8. schema.isValid({ id: 1 }); // false! names.first is required
  9. ```

This is because yup casts the input object before running validation
which will produce:

{ id: '1', names: { first: undefined }}


During the validation phase names exists, and is validated, finding names.first missing.
If you wish to avoid this behavior do one of the following:

- Set the nested default to undefined: names.default(undefined)
- mark it nullable and default to null: names.nullable().default(null)

#### `object.shape(fields: object, noSortEdges?: Array<[string, string]>): Schema`

Define the keys of the object and the schemas for said keys.

Note that you can chain shape method, which acts like Object.assign.

  1. ```ts
  2. object({
  3.   a: string(),
  4.   b: number(),
  5. }).shape({
  6.   b: string(),
  7.   c: number(),
  8. });
  9. ```

would be exactly the same as:

  1. ```ts
  2. object({
  3.   a: string(),
  4.   b: string(),
  5.   c: number(),
  6. });
  7. ```

object.json(): this


Attempt to parse input string values as JSON using [JSON.parse](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse).

object.concat(schemaB: ObjectSchema): ObjectSchema


Creates a object schema, by applying all settings and fields from schemaB to the base, producing a new schema.
The object shape is shallowly merged with common fields from schemaB taking precedence over the base
fields.

object.pick(keys: string[]): Schema


Create a new schema from a subset of the original's fields.

  1. ``` js
  2. const person = object({
  3.   age: number().default(30).required(),
  4.   name: string().default('pat').required(),
  5.   color: string().default('red').required(),
  6. });

  7. const nameAndAge = person.pick(['name', 'age']);
  8. nameAndAge.getDefault(); // => { age: 30, name: 'pat'}
  9. ```

object.omit(keys: string[]): Schema


Create a new schema with fields omitted.

  1. ``` js
  2. const person = object({
  3.   age: number().default(30).required(),
  4.   name: string().default('pat').required(),
  5.   color: string().default('red').required(),
  6. });

  7. const nameAndAge = person.omit(['color']);
  8. nameAndAge.getDefault(); // => { age: 30, name: 'pat'}
  9. ```

object.from(fromKey: string, toKey: string, alias: boolean = false): this


Transforms the specified key to a new key. If alias is true then the old key will be left.

  1. ``` js
  2. let schema = object({
  3.   myProp: mixed(),
  4.   Other: mixed(),
  5. })
  6.   .from('prop', 'myProp')
  7.   .from('other', 'Other', true);

  8. schema.cast({ prop: 5, other: 6 }); // => { myProp: 5, other: 6, Other: 6 }
  9. ```

object.noUnknown(onlyKnownKeys: boolean = true, message?: string | function): Schema


Validate that the object value only contains keys specified in shape, pass false as the first
argument to disable the check. Restricting keys to known, also ena