ottoman
Version:
Ottoman Couchbase ODM
93 lines • 3.98 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.checkCastStrategy = exports.cast = exports.ensureTypes = exports.ensureArrayItemsType = exports.CAST_STRATEGY = void 0;
const schema_1 = require("../schema");
/**
* Cast Strategies
* 'keep' | 'drop' | 'throw' | 'defaultOrDrop' | 'defaultOrKeep'
*
* @desc
* When trying to cast a value to a given type if it fail Cast Strategy will behave this ways for:
* + 'keep' -> The original value will be returned
* + 'drop' -> Return undefined and the object field will be removed.
* + 'throw' -> Throw exception 'Value couldn't be casted to given type'
* + 'defaultOrDrop' -> Try to return default value if exists, if no default value was provided for the current field then it will be removed
* + 'defaultOrDrop' -> Try to return default value if exists, if no default value was provided for the current field then it will keep original value.
*/
var CAST_STRATEGY;
(function (CAST_STRATEGY) {
CAST_STRATEGY["KEEP"] = "keep";
CAST_STRATEGY["DROP"] = "drop";
CAST_STRATEGY["THROW"] = "throw";
CAST_STRATEGY["DEFAULT_OR_DROP"] = "defaultOrDrop";
CAST_STRATEGY["DEFAULT_OR_KEEP"] = "defaultOrKeep";
})(CAST_STRATEGY = exports.CAST_STRATEGY || (exports.CAST_STRATEGY = {}));
const ensureArrayItemsType = (array, field, strategy) => array.map((item) => (0, exports.ensureTypes)(item, field, strategy));
exports.ensureArrayItemsType = ensureArrayItemsType;
const ensureTypes = (item, field, strategy) => {
if (field) {
return field.cast(item, strategy);
}
};
exports.ensureTypes = ensureTypes;
const cast = (data, schema, options = {
strategy: CAST_STRATEGY.DEFAULT_OR_DROP,
strict: true,
skip: [],
}) => {
const skip = options.skip || [];
const strategy = options.strategy || CAST_STRATEGY.DEFAULT_OR_DROP;
const strict = options.strict || false;
if (strict && (strategy === CAST_STRATEGY.KEEP || strategy === CAST_STRATEGY.DEFAULT_OR_KEEP)) {
throw new schema_1.ValidationError(`Cast Strategy 'keep' or 'defaultOrKeep' isn't support when strict is set to true.`);
}
const result = {};
let _data = Object.assign({}, data);
const _schema = schema instanceof schema_1.Schema ? schema : new schema_1.Schema(schema);
if (strategy === CAST_STRATEGY.DEFAULT_OR_DROP ||
strategy === CAST_STRATEGY.DEFAULT_OR_KEEP ||
strategy === CAST_STRATEGY.THROW) {
_data = (0, schema_1.applyDefaultValue)(_data, _schema);
}
for (const key in _data) {
if (_data.hasOwnProperty(key)) {
if (_schema.fields[key] && !skip.includes(key)) {
const field = _schema.fields[key];
const value = (0, exports.ensureTypes)(_data[key], field, strategy);
switch (strategy) {
case CAST_STRATEGY.KEEP:
result[key] = value;
break;
case CAST_STRATEGY.DROP:
case CAST_STRATEGY.DEFAULT_OR_DROP:
default:
if (value !== undefined) {
result[key] = value;
}
}
}
else {
if (!strict || skip.includes(key)) {
result[key] = _data[key];
}
}
}
}
return result;
};
exports.cast = cast;
const checkCastStrategy = (value, strategy, type) => {
switch (strategy) {
case CAST_STRATEGY.KEEP:
case CAST_STRATEGY.DEFAULT_OR_KEEP:
return value;
case CAST_STRATEGY.THROW:
throw new schema_1.ValidationError(`Property '${type.name}' must be of type '${type.typeName}'`);
case CAST_STRATEGY.DROP:
case CAST_STRATEGY.DEFAULT_OR_DROP:
default:
return undefined;
}
};
exports.checkCastStrategy = checkCastStrategy;
//# sourceMappingURL=cast-strategy.js.map