Array

Array


import { number, array } from '@resourge/schema';

// Validate if array as at least 1 item
array(number()).min(1)

// Custom error message
array(<<Schema>>, 'Custom error message')

Checks if array is empty

Checks if array is empty

array(number()).empty()

Custom error message:

array(number()).empty('Custom error message')

min

Checks if array has a minimum number of items in array

array(number()).min(1)

Custom error message:

array(number()).min(1, 'Custom error message')

max

Checks if array has a maximum number of elements

array(number()).max(10)

Custom error message:

array(number()).max(10, 'Custom error message')

length

Checks if array has length number of elements

array(number()).length(1, 10)

Custom error message:

array(number()).length(1, 10, 'Custom error message')

unique

Checks if array has only unique elements

array(number()).unique()
// with custom message
array(number()).unique('Custom error message')

Custom error message:

array(number()).unique('Custom error message')

uniqueBy

Checks if array has only unique elements by key or function.

Using key:

array(
 object({
  productId: number(),
  productName: string()
 })
).uniqueBy('productId')

Custom error message:

array(
 object({
  productId: number(),
  productName: string()
 })
).uniqueBy('productId', 'Custom error message')

Using function:

array(
 object({
  productId: number(),
  productName: string()
 })
).uniqueBy((obj) => obj.productName)