ottoman
Version:
Ottoman Couchbase ODM
145 lines • 5.74 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.stringTypeFactory = exports.StringType = void 0;
const core_type_1 = require("./core-type");
const generate_uuid_1 = require("../../utils/generate-uuid");
const utils_1 = require("../../utils");
const errors_1 = require("../errors");
const cast_strategy_1 = require("../../utils/cast-strategy");
/**
* `String` are plain JavaScript String and accepts the following options:
*
* ## Options
*
* - **required** flag to define if the field is mandatory
* - **validator** that will be applied to the field, allowed function, object or string with the name of the custom validator
* - **default** that will define the initial value of the field, allowed and value or function to generate it
* - **immutable** that will define this field as immutable. Ottoman prevents you from changing immutable fields if the schema as configure like strict
* - **enum** defines a list of allowed values
* - **auto** that will generate the initial value of the field. It allows the value 'uuid' or a function. It cannot be used combined with default
*
* @example
* ```typescript
* const userSchema = new Schema({
* name: { type: String, auto: 'uuid', trim: true },
* gender: { type: String, enum: ['M', 'F'] }
* lastname: Schema.Types.String,
* user: { type: String, lowercase: true, minLength: 4, maxLength: 7 }
* })
* ```
*/
class StringType extends core_type_1.CoreType {
constructor(name, options = {}) {
super(name, StringType.sName, options);
this.options = options;
this._checkIntegrity();
}
get enumValues() {
return this.options.enum;
}
get auto() {
return this.options.auto;
}
get lowercase() {
return !!this.options.lowercase;
}
get uppercase() {
return !!this.options.uppercase;
}
get trim() {
return !!this.options.trim;
}
get minLength() {
return this.options.minLength;
}
get maxLength() {
return this.options.maxLength;
}
buildDefault() {
if (this.auto === 'uuid') {
return (0, generate_uuid_1.generateUUID)();
}
const _value = super.buildDefault();
return typeof _value === 'undefined' ? _value : String(_value);
}
cast(value, strategy = cast_strategy_1.CAST_STRATEGY.DEFAULT_OR_DROP) {
if (value === null) {
return value;
}
let castedValue = String(value);
if ((0, utils_1.is)(castedValue, String) && !(0, utils_1.is)(value, Object)) {
if (this.lowercase) {
castedValue = castedValue.toLowerCase();
}
if (this.uppercase) {
castedValue = castedValue.toUpperCase();
}
if (this.trim) {
castedValue = castedValue.trim();
}
return castedValue;
}
return (0, cast_strategy_1.checkCastStrategy)(value, strategy, this);
}
validate(value, strategy) {
super.validate(value, strategy);
const _wrongType = this.isStrictStrategy(strategy) ? !(0, utils_1.is)(value, String) : (0, utils_1.is)(value, Object);
if (_wrongType) {
throw new errors_1.ValidationError(`Property '${this.name}' must be of type '${this.typeName}'`);
}
if (value === null || value === undefined) {
return value;
}
const errors = [];
const _value = String(value);
this._checkEnum(_value, errors);
this._checkMinLength(_value, errors);
this._checkMaxLength(_value, errors);
this.checkValidator(_value);
if (errors.length) {
throw new errors_1.ValidationError(errors.join('\n'));
}
return _value;
}
_checkEnum(value, errors) {
if (value !== 'undefined' && typeof this.enumValues !== 'undefined') {
const _enumValues = typeof this.enumValues === 'function' ? this.enumValues() : this.enumValues;
if (!_enumValues.includes(value)) {
errors.push(`Property '${this.name}' value must be ${_enumValues.join(',')}`);
}
}
}
_checkMinLength(value, errors) {
const length = this.minLength;
if ((0, utils_1.is)(length, Number) && value.length < length) {
errors.push(`Property '${this.name}' is shorter than the minimum allowed length '${length}'`);
}
}
_checkMaxLength(value, errors) {
const length = this.maxLength;
if ((0, utils_1.is)(length, Number) && value.length > length) {
errors.push(`Property '${this.name}' is longer than the maximum allowed length '${length}'`);
}
}
_checkIntegrity() {
if (this.auto !== undefined) {
if (this.default !== undefined) {
throw new errors_1.BuildSchemaError(`Auto and default values cannot be used at the same time in property '${this.name}'.`);
}
if (this.auto === 'uuid' && this.typeName !== String.name) {
throw new errors_1.BuildSchemaError('Automatic uuid properties must be string typed.');
}
}
if (this.lowercase && this.uppercase) {
throw new errors_1.BuildSchemaError(`'lowercase' and 'uppercase' options cannot be used at the same time within property '${this.name}' definition.`);
}
}
isEmpty(value) {
return [, null, ''].includes(value);
}
}
exports.StringType = StringType;
StringType.sName = String.name;
const stringTypeFactory = (key, opts) => new StringType(key, opts);
exports.stringTypeFactory = stringTypeFactory;
//# sourceMappingURL=string-type.js.map