|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604 |
- 'use strict';
-
-
-
- const SchemaType = require('../schematype');
- const CastError = SchemaType.CastError;
- const MongooseError = require('../error');
- const castString = require('../cast/string');
- const utils = require('../utils');
-
- let Document;
-
-
-
- function SchemaString(key, options) {
- this.enumValues = [];
- this.regExp = null;
- SchemaType.call(this, key, options, 'String');
- }
-
-
- SchemaString.schemaName = 'String';
-
-
- SchemaString.prototype = Object.create(SchemaType.prototype);
- SchemaString.prototype.constructor = SchemaString;
-
-
-
- SchemaString._cast = castString;
-
-
-
- SchemaString.cast = function cast(caster) {
- if (arguments.length === 0) {
- return this._cast;
- }
- if (caster === false) {
- caster = v => {
- if (v != null && typeof v !== 'string') {
- throw new Error();
- }
- return v;
- };
- }
- this._cast = caster;
-
- return this._cast;
- };
-
-
-
- SchemaString.get = SchemaType.get;
-
-
-
- SchemaString._checkRequired = v => (v instanceof String || typeof v === 'string') && v.length;
-
-
-
- SchemaString.checkRequired = SchemaType.checkRequired;
-
-
-
- SchemaString.prototype.enum = function() {
- if (this.enumValidator) {
- this.validators = this.validators.filter(function(v) {
- return v.validator !== this.enumValidator;
- }, this);
- this.enumValidator = false;
- }
-
- if (arguments[0] === void 0 || arguments[0] === false) {
- return this;
- }
-
- let values;
- let errorMessage;
-
- if (utils.isObject(arguments[0])) {
- values = arguments[0].values;
- errorMessage = arguments[0].message;
- } else {
- values = arguments;
- errorMessage = MongooseError.messages.String.enum;
- }
-
- for (let i = 0; i < values.length; i++) {
- if (undefined !== values[i]) {
- this.enumValues.push(this.cast(values[i]));
- }
- }
-
- const vals = this.enumValues;
- this.enumValidator = function(v) {
- return undefined === v || ~vals.indexOf(v);
- };
- this.validators.push({
- validator: this.enumValidator,
- message: errorMessage,
- type: 'enum',
- enumValues: vals
- });
-
- return this;
- };
-
-
-
- SchemaString.prototype.lowercase = function(shouldApply) {
- if (arguments.length > 0 && !shouldApply) {
- return this;
- }
- return this.set(function(v, self) {
- if (typeof v !== 'string') {
- v = self.cast(v);
- }
- if (v) {
- return v.toLowerCase();
- }
- return v;
- });
- };
-
-
-
- SchemaString.prototype.uppercase = function(shouldApply) {
- if (arguments.length > 0 && !shouldApply) {
- return this;
- }
- return this.set(function(v, self) {
- if (typeof v !== 'string') {
- v = self.cast(v);
- }
- if (v) {
- return v.toUpperCase();
- }
- return v;
- });
- };
-
-
-
- SchemaString.prototype.trim = function(shouldTrim) {
- if (arguments.length > 0 && !shouldTrim) {
- return this;
- }
- return this.set(function(v, self) {
- if (typeof v !== 'string') {
- v = self.cast(v);
- }
- if (v) {
- return v.trim();
- }
- return v;
- });
- };
-
-
-
- SchemaString.prototype.minlength = function(value, message) {
- if (this.minlengthValidator) {
- this.validators = this.validators.filter(function(v) {
- return v.validator !== this.minlengthValidator;
- }, this);
- }
-
- if (value !== null && value !== undefined) {
- let msg = message || MongooseError.messages.String.minlength;
- msg = msg.replace(/{MINLENGTH}/, value);
- this.validators.push({
- validator: this.minlengthValidator = function(v) {
- return v === null || v.length >= value;
- },
- message: msg,
- type: 'minlength',
- minlength: value
- });
- }
-
- return this;
- };
-
-
-
- SchemaString.prototype.maxlength = function(value, message) {
- if (this.maxlengthValidator) {
- this.validators = this.validators.filter(function(v) {
- return v.validator !== this.maxlengthValidator;
- }, this);
- }
-
- if (value !== null && value !== undefined) {
- let msg = message || MongooseError.messages.String.maxlength;
- msg = msg.replace(/{MAXLENGTH}/, value);
- this.validators.push({
- validator: this.maxlengthValidator = function(v) {
- return v === null || v.length <= value;
- },
- message: msg,
- type: 'maxlength',
- maxlength: value
- });
- }
-
- return this;
- };
-
-
-
- SchemaString.prototype.match = function match(regExp, message) {
-
-
- const msg = message || MongooseError.messages.String.match;
-
- const matchValidator = function(v) {
- if (!regExp) {
- return false;
- }
-
- const ret = ((v != null && v !== '')
- ? regExp.test(v)
- : true);
- return ret;
- };
-
- this.validators.push({
- validator: matchValidator,
- message: msg,
- type: 'regexp',
- regexp: regExp
- });
- return this;
- };
-
-
-
- SchemaString.prototype.checkRequired = function checkRequired(value, doc) {
- if (SchemaType._isRef(this, value, doc, true)) {
- return !!value;
- }
- return this.constructor._checkRequired(value, doc);
- };
-
-
-
- SchemaString.prototype.cast = function(value, doc, init) {
- if (SchemaType._isRef(this, value, doc, init)) {
-
-
- if (value === null || value === undefined) {
- return value;
- }
-
-
- Document || (Document = require('./../document'));
-
- if (value instanceof Document) {
- value.$__.wasPopulated = true;
- return value;
- }
-
-
- if (typeof value === 'string') {
- return value;
- } else if (Buffer.isBuffer(value) || !utils.isObject(value)) {
- throw new CastError('string', value, this.path);
- }
-
-
-
-
- const path = doc.$__fullPath(this.path);
- const owner = doc.ownerDocument ? doc.ownerDocument() : doc;
- const pop = owner.populated(path, true);
- const ret = new pop.options.model(value);
- ret.$__.wasPopulated = true;
- return ret;
- }
-
- try {
- return this.constructor.cast()(value);
- } catch (error) {
- throw new CastError('string', value, this.path);
- }
- };
-
-
-
- function handleSingle(val) {
- return this.castForQuery(val);
- }
-
- function handleArray(val) {
- const _this = this;
- if (!Array.isArray(val)) {
- return [this.castForQuery(val)];
- }
- return val.map(function(m) {
- return _this.castForQuery(m);
- });
- }
-
- SchemaString.prototype.$conditionalHandlers =
- utils.options(SchemaType.prototype.$conditionalHandlers, {
- $all: handleArray,
- $gt: handleSingle,
- $gte: handleSingle,
- $lt: handleSingle,
- $lte: handleSingle,
- $options: handleSingle,
- $regex: handleSingle,
- $not: handleSingle
- });
-
-
-
- SchemaString.prototype.castForQuery = function($conditional, val) {
- let handler;
- if (arguments.length === 2) {
- handler = this.$conditionalHandlers[$conditional];
- if (!handler) {
- throw new Error('Can\'t use ' + $conditional + ' with String.');
- }
- return handler.call(this, val);
- }
- val = $conditional;
- if (Object.prototype.toString.call(val) === '[object RegExp]') {
- return val;
- }
-
- return this._castForQuery(val);
- };
-
-
-
- module.exports = SchemaString;
|