Array Schema
Create a schema that contains a list of values of any type.
See at Base Schema for the inherited methods you may call like:
title()
detail()
required()
forbidden()
default()
stripEmpty()
raw()
1. Sanitize
1.1. split(matcher)
This setting allows to automatically split strings or objects which could be converted to strings into array by splitting them on a defined matcher:
const schema = new ArraySchema().split(',') // split by string
const schema = new ArraySchema().split(/\D+/) // split by regular expression
schema.split() // remove the setting
Like seen above the separator may be a string
or RegExp
.
const ref = new Reference(',')
const schema = new ArraySchema().split(ref)
References are also possible here.
1.2. toArray(bool)
This option allows to give an element directly if only one should be there. The list around it will be made automatically.
const schema = new ArraySchema().toArray()
schema.toArray(false) // remove the setting
So if a single number 3
is given it will be converted to [3]
. As always a reference can be used
here, too.
const ref = new Reference(true)
const schema = new ArraySchema().toArray(ref)
1.3. sanitize(bool)
This option in combination with some checks will automatically fix your value instead of an alert.
You may remove it as all flags by giving false
to it.
1.4. sanitize().unique(bool)
Remove duplicate elements to get only unique values:
const schema = new ArraySchema().sanitize().unique()
schema.sanitize(true).unique(false) // remove the settings
And references are used on both like:
const ref = new Reference(true)
const schema = new ArraySchema().sanizize(ref).unique(ref)
1.5. filter(bool) / filter(Schema)
Filter the list of elements to remove all empty elements or all that don't match the given schema definition.
const schema = new ArraySchema().filter() // remove empty elements
const schema = new ArraySchema().filter(new NumberSchema()) // only numbers are allowed
schema.filter(false) // to remove the setting
1.6. shuffle(bool)
All list items will be shuffled to get a random order:
const schema = new ArraySchema().shuffle()
schema.shuffle(false) // remove the setting
A reference may be used as flag:
const ref = new Reference(true)
const schema = new ArraySchema().shuffle(ref)
1.7. sort(bool)
Sort a list alphabetically by it's contents:
const schema = new ArraySchema().sort()
schema.sort(false) // remove the setting
A reference may be used as flag:
const ref = new Reference(true)
const schema = new ArraySchema().sort(ref)
1.8. reverse(bool)
Additionally to sort()
or separately the order of the list items will be reversed:
const schema = new ArraySchema().reverse()
schema.reverse(false) // remove the settings
A reference may be used as flag:
const ref = new Reference(true)
const schema = new ArraySchema().reverse(ref)
1.9. format(string)
Convert the list into a text string in one of the following formats:
json
- use standard json formatpretty
- use more readable, human stylesimple
- make a comma separated list
const schema = new ArraySchema().format('simple')
schema.format() // remove the setting
A reference may be used as flag:
const ref = new Reference('human')
const schema = new ArraySchema().format(ref)
2. Checking
2.1. unique(bool)
If called without sanitize it will alert if duplicate values are contained.
const schema = new ArraySchema().unique()
schema.unique(false) // remove the setting
And references are used on both like:
const ref = new Reference(true)
const schema = new ArraySchema().unique(ref)
2.2. min(limit) / max(limit) / length(limit)
Specifies the number of items in the array which are allowed.
limit
gives thenumber
of items
const schema = new ArraySchema().min(1).max(3)
schema.min().max() // to remove both settings
References are also possible:
const ref = new Reference(5)
const schema = new ArraySchema().length(ref)
3. Deeper checks
3.1. item(check)
Specify the schema for one or multiple items of the array list. If you call this once you define the check for element number 0. If you call it multiple times you define the continuing element numbers 1, 2, ... The last given check is used for all further elements automatically.
Example
This defines a list containing multiple elements of the same schema:
const schema = new ArraySchema().item(new AnySchema())
schema.item() // to remove all item settings
And here we need a string as the first list element with other data for the rest and exactly 3 entries.
const schema = new ArraySchema().length(3)
.item(new StringSchema())
.item(new AnySchema())