orm/src/schema/types/Array.js

/**
 * @module flitter-orm/src/schema/types/Array
 */

const Type = require('../Type')

/**
 * Schema type representing an array of a single other
 * schema type.
 * @extends {module:flitter-orm/src/schema/Type~Type}
 */
class ArrayType extends Type {
    /**
     * Checks if an item can be cast to an array.
     * @param {*} value
     * @returns {boolean} - always true
     */
    static validate(value) {
        return true
    }

    /**
     * Casts a value to an array if it is not already.
     * @param {Array<*>|*} value
     * @returns {Array<*>}
     */
    static cast(value) {
        if ( !Array.isArray(value) ) return [value]
        return value
    }
}

module.exports = exports = ArrayType