Schema - Array
The .array()
method is used to validate whether a value is an array. Additionally, it can be chained with other methods to validate the contents of the array based on specific types, constraints, and requirements.
Available Methods for Arrays
Below is a table describing the available methods that can be chained with .array()
to validate array values.
Method | Description |
---|---|
.min() | Ensures that the array has at least the specified number of elements. |
.max() | Ensures that the array has no more than the specified number of elements. |
.string() | Ensures that the elements inside the array are strings. |
.number() | Ensures that the elements inside the array are numbers. |
.bigInt() | Ensures that the elements inside the array are BigInt values. |
.boolean() | Ensures that the elements inside the array are booleans. |
.object() | Ensures that the elements inside the array are objects matching a specific schema. |
.nullable() | Ensures that the array can accept null values. |
.notRequired() | Ensures that the array elements are not required, and can be undefined or null . |
.oneOf() | Ensures that the elements inside the array match one of the specified values or schemas. |
.buffer() | Ensures that the elements inside the array are buffers. |
.function() | Ensures that the elements inside the array are functions. |
.notOneOf() | Ensures that the elements inside the array do not match any of the specified values or schemas. |
.date() | Ensures that the elements inside the array are valid dates. |
Examples for Each Method
.min()
Validates that the array contains at least the specified number of elements:
const exampleSchema = schema().array({ min: 2 }).string()
const validateA = exampleSchema.validate(["hello", "world"])
const validateB = exampleSchema.validate(["hello"])
console.log(validateA) // true
console.log(validateB) // false
.max()
Validates that the array contains no more than the specified number of elements:
const exampleSchema = schema().array({ max: 3 }).string()
const validateA = exampleSchema.validate(["hello", "world", "test"])
const validateB = exampleSchema.validate(["hello", "world", "test", "extra"])
console.log(validateA) // true
console.log(validateB) // false
.string()
Validates if the elements inside the array are strings:
const exampleSchema = schema().array().string()
const validateA = exampleSchema.validate(["hello", "world"])
const validateB = exampleSchema.validate([1, 2])
console.log(validateA) // true
console.log(validateB) // false
.number()
Validates if the elements inside the array are numbers:
const exampleSchema = schema().array().number()
const validateA = exampleSchema.validate([1, 2, 3])
const validateB = exampleSchema.validate([1, "2"])
console.log(validateA) // true
console.log(validateB) // false
.boolean()
Validates if the elements inside the array are booleans:
const exampleSchema = schema().array().boolean()
const validateA = exampleSchema.validate([true, false])
const validateB = exampleSchema.validate([true, "false"])
console.log(validateA) // true
console.log(validateB) // false
.object()
Validates if the elements inside the array are objects matching a specific schema:
const exampleSchema = schema().array().object({
id: schema().string().UUID(),
fullName: schema().string().minWord(2)
})
const validateA = exampleSchema.validate([
{
id: "3ef7c105-c4ea-444d-bf47-e2e1a49ea613",
fullName: "Full Name"
}
])
const validateB = exampleSchema.validate([
{
id: "3ef7c105-c4ea-444d-bf47-e2e1a49ea613"
}
])
console.log(validateA) // true
console.log(validateB) // false
.nullable()
Validates if the array can accept null
values:
const exampleSchema = schema().array().string().nullable()
const validateA = exampleSchema.validate([null])
const validateB = exampleSchema.validate([undefined])
console.log(validateA) // true
console.log(validateB) // true
.notRequired()
Validates if the array elements are not required:
const exampleSchema = schema().array().string().notRequired()
const validateA = exampleSchema.validate(undefined) // Allowed because it's not required
const validateB = exampleSchema.validate(["value"]) // Valid array
console.log(validateA) // true
console.log(validateB) // true
.oneOf()
Validates if the array elements match one of the specified values or schemas:
const exampleSchema = schema().array().oneOf([schema().string(), true])
const validateA = exampleSchema.validate([true]) // Matches schema().boolean()
const validateB = exampleSchema.validate(["hello"]) // Matches schema().string()
const validateC = exampleSchema.validate([1]) // No match
console.log(validateA) // true
console.log(validateB) // true
console.log(validateC) // false
.notOneOf()
Validates if the array elements do not match any of the specified values or schemas:
const exampleSchema = schema().array().notOneOf([schema().string(), true])
const validateA = exampleSchema.validate([1]) // No match
const validateB = exampleSchema.validate([true]) // Match schema().boolean()
const validateC = exampleSchema.validate(["hello"]) // Match schema().string()
console.log(validateA) // true
console.log(validateB) // false
console.log(validateC) // false
.buffer()
Validates if the elements inside the array are buffers:
const exampleSchema = schema().array().buffer()
const validateA = exampleSchema.validate([Buffer.from("test"), Buffer.from("test")])
const validateB = exampleSchema.validate([true, "test"])
console.log(validateA) // true
console.log(validateB) // false
.function()
Validates if the elements inside the array are functions:
const exampleSchema = schema().array().function()
const validateA = exampleSchema.validate([() => {}, () => {}])
const validateB = exampleSchema.validate([() => {}, "test"])
console.log(validateA) // true
console.log(validateB) // false
Conclusion
By using .array()
with various chained validation methods, you can efficiently validate arrays and their elements in a flexible and powerful way. Whether you're validating strings, numbers, booleans, objects, or even complex date constraints, this approach ensures your data conforms to the expected structure. Methods like .notRequired()
and .nullable()
offer greater flexibility by allowing for optional or null values in your arrays. Additionally, .oneOf()
and .notOneOf()
help to validate whether the array elements match or exclude certain predefined values or schemas, making your validation logic more robust.