orm/src/schema/types/String.js

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

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

/**
 * Schema type representing a single string.
 * @extends {module:flitter-orm/src/schema/Type~Type}
 */
class StringType extends Type {
    /**
     * Determines whether the specified value can be cast to a string.
     * @param {*} value
     * @returns {boolean}
     */
    static validate(value) {
        if ( typeof value === 'string' ) return true
        try {
            String(value)
            return true
        } catch (e) {
            return false
        }
    }

    /**
     * Casts the specified value to a string.
     * @param {*} value
     * @returns {string}
     */
    static cast(value) {
        return String(value)
    }
}

module.exports = exports = StringType