Skip to content
Alan Berdinelli edited this page Mar 24, 2021 · 7 revisions

Array type validates the value is an Array. Then, if you also define the type of the elements, it checks all the elements are the correct type.

const Schemy = require('schemy');

const Example = new Schemy({
    data: {
        type: []
    }
});

Array of strings

Will fail if any of the elements inside the array is not a string.

const Schemy = require('schemy');

const Example = new Schemy({
    data: {
        type: [String]
    }
});

Array of numbers

Will fail if any of the elements inside the array is not a number.

const Schemy = require('schemy');

const Example = new Schemy({
    data: {
        type: [Number]
    }
});

Array of Schemy

Will fail if any of the elements fails the schema validation.

const Schemy = require('schemy');

const childSchema = new Schemy({
    title: { type: String }
});

const Example = new Schemy({
    data: {
        type: [childSchema]
    }
});

You can also pass schema objects as array items and schemy will validate them for you:

const Example = new Schemy({
    data: [{ title: String }]
});

Min/max number of elements

Since 1.9.0, you can set a min and/or max number of elements within an array:

const Example = new Schemy({
    items: {
        type: [],
        min: 1,
        max: 10
    }
});

Clone this wiki locally