/**
* @module flitter-orm/src/schema/types/Object
*/
const Type = require('../Type')
const stringify = require('json-stringify-safe')
/**
* Schema type representing a 2-way serializable object
* comprised of keys of other schema types.
* @extends {module:flitter-orm/src/schema/Type~Type}
*/
class ObjectType extends Type {
/**
* Determines whether the specified value is a serializable object.
* @param {*} value
* @returns {boolean}
*/
static validate(value) {
if ( typeof value !== 'object' ) return false
try {
stringify(value)
return true
} catch (e) {
return false
}
}
/**
* Casts the specified value to an object.
* This walks the object through serialization and de-serialization
* for 2 reasons. First, it ensures a clone of the object. Second,
* it strips any non-serializable structures from the object.
*
* @param value
* @returns {object}
*/
static cast(value) {
return JSON.parse(stringify(value))
}
}
module.exports = exports = ObjectType