jquense/yup · error · TypeError
`array.of()` sub-schema must be a valid yup schema not: ${pr
Error message
`array.of()` sub-schema must be a valid yup schema not: ${printValue(schema)} What it means
ArraySchema.of() defines the schema for each array element and validates that the argument is a real yup schema using isSchema(). If not, it throws a TypeError that includes printValue(schema) so the developer can see what was actually passed. This catches passing plain constructors, strings like 'string', or wrong objects.
Source
Thrown at src/array.ts:195
if (schema.innerType)
// @ts-expect-error readonly
next.innerType = next.innerType
? // @ts-expect-error Lazy doesn't have concat and will break
next.innerType.concat(schema.innerType)
: schema.innerType;
return next;
}
of<U>(
schema: ISchema<U, TContext>,
): ArraySchema<U[] | Optionals<TIn>, TContext, TFlags> {
// FIXME: this should return a new instance of array without the default to be
let next = this.clone();
if (!isSchema(schema))
throw new TypeError(
'`array.of()` sub-schema must be a valid yup schema not: ' +
printValue(schema),
);
// @ts-expect-error readonly
next.innerType = schema;
next.spec = {
...next.spec,
types: schema as ISchema<InnerType<TIn>, TContext>,
};
return next as any;
}
length(
length: number | Reference<number>,
message: Message<{ length: number }> = locale.length,View on GitHub (pinned to ff31eee8a2)
Solutions
- Pass a yup schema instance: yup.array().of(yup.string().required()).
- If building schemas from string names, verify the registry lookup returned a schema before calling of().
- Fix typos in schema factory names and confirm imports resolve (undefined import).
Example fix
// before
yup.array().of('string')
// after
yup.array().of(yup.string()) Defensive patterns
Strategy: type-guard
Validate before calling
import { isSchema } from 'yup';
const arrayOf = (item) => {
if (!isSchema(item)) throw new TypeError(`array.of() needs a yup schema, got: ${JSON.stringify(item)}`);
return yup.array().of(item);
}; Type guard
const isYupSchema = (v) => isSchema(v);
Prevention
- Always construct element schemas via yup.* factories, never strings or native constructors.
- Validate registry lookups before passing results to of().
- Centralize array schema creation in one helper.
When it happens
Trigger: yup.array().of('string'), yup.array().of(String), yup.array().of({ type: 'string' }), or of(x) where x is undefined due to a failed lookup / typo'd import (e.g. yup.stringg).
Common situations: Schema registries/factories that map string names to schemas returning undefined; migrating from other validator libs where element definitions are plain objects; dynamic schema builders hitting missing keys.
Related errors
- conditions must return a schema object
- lazy() functions must return a valid schema
- either `then:` or `otherwise:` is required for `when()` cond
- ref must be a string, got:
- `${name}` must be a Date or a value that can be `cast()` to
AI-assisted analysis of jquense/yup@ff31eee8a2 (2026-08-31).
Data as JSON: /api/errors/51a0bd28ff0cce3e.
Report an issue: GitHub.