validate.validators.array = function(value) { if(value) { if(!validate.isArray(value)) { return "is not an array"; } } return null; }
var mapped = _.map(err.errors, function(value, key) { if (validate.isArray(value)) { return value.map(function(errObj) { return this.create(status, errObj); }); } else { return errorResponse.create(status, value); } });
export function validateData(data) { let i; let entry; if (validate.isArray(data)) { for (i = 0; i < data.length; i++) { entry = data[i]; if (!(validate.isArray(entry) || validate.isObject(entry))) { console.error('Each entry within the array passed ' + 'through table.appendRows must be an array or object.'); return false; } } } else { console.error('table.appendRows must be passed an array.'); return false; } return true; }
to: function (value, attributes, attributeName, options, constraints) { if (validate.isEmpty(value)) { return { presence: {message: "is required"} }; } if (!validate.isArray(value) && !validate.isString(value)) { return { format: "must be a string or an array strings (phone numbers)" }; } if (validate.isString(value)) { // TODO: Validate number format let isInvalid = false; if (isInvalid) { return { format: "must be a valid phone number" }; } } if (validate.isArray(value)) { let foundInvalid = false; value.forEach(function (phone) { // TODO: Validate number format }); if (foundInvalid) { return { format: "must NOT contain invalid phone number" } } } return null; },
constructor (msg, errors) { super(msg, 500); if (!validate.isArray(errors)) { errors = [errors]; } for (var err of errors) { if (!(err instanceof StatusError)) { throw new Error('errors can only contain StatusError types'); } } this.status = errors[0].status; this.errors = errors; }
ErrorResponse.prototype.error = function(err) { var status = this.status(err); // Check for multiple errors and handle them if (err.hasOwnProperty('errors')) { var errors = []; if (validate.isObject(err.errors)) { /* { id: [err1, err2], name: ['blah', 'bar'] } */ var errorResponse = this; var mapped = _.map(err.errors, function(value, key) { if (validate.isArray(value)) { return value.map(function(errObj) { return this.create(status, errObj); }); } else { return errorResponse.create(status, value); } }); return {errors: mapped}; } else if (validate.isArray(err.errors)) { /* ['error one', 'error two'] */ errs = value.map(function(errObj) { return this.create(status, errObj); }); return {errors: errs}; } } else { var error = this.create(status, err); return {errors: [error]}; } }
validate.validators.array = function array (value, options, key, attributes) { if (attributes[key] && !validate.isArray(attributes[key])) { return `must be an array`; } };
this._send = function (params, isBulk, isPremium) { // Validate params var _validateParams = function () { var constraints = { to: function (value, attributes, attributeName, options, constraints) { if (validate.isEmpty(value)) { return { presence: {message: "is required"} }; } if (!validate.isArray(value) && !validate.isString(value)) { return { format: "must be a string or an array strings (phone numbers)" }; } if (validate.isString(value)) { // TODO: Validate number format let isInvalid = false; if (isInvalid) { return { format: "must be a valid phone number" }; } } if (validate.isArray(value)) { let foundInvalid = false; value.forEach(function (phone) { // TODO: Validate number format }); if (foundInvalid) { return { format: "must NOT contain invalid phone number" } } } return null; }, from: { isString: true }, message: { presence: true } }; if (isBulk) { constraints.enqueue = { inclusion: [true] }; } if (isPremium) { constraints.keyword = { presence: true, isString: true }; constraints.linkId = { presence: true, isString: true }; constraints.retryDurationInHours = { numericality: true } } var error = validate(params, constraints); if (error) { var msg = ""; for (var k in error) { msg += error[k] + "; "; } throw new Error(msg); } }; _validateParams(); // Multiple recipients? if (validate.isArray(params.to)) { if (params.to.length === 1) { params.to = params.to[0]; } else { params.to = params.to.join(); } } return new Promise(function (resolve, reject) { var body = { username: _self.options.username, to: params.to, message: params.message }; if (params.from) { body.from = params.from } if (isBulk) { body.bulkSMSMode = 1; if (params.enqueue) { body.enqueue = 1; } } if (isPremium) { body.keyword = params.keyword; body.linkId = params.linkId; if (params.retryDurationInHours) { body.retryDurationInHours = params.retryDurationInHours; } } var rq = unirest.post(Common.SMS_URL); rq.headers({ apiKey: _self.options.apiKey, Accept: _self.options.format }); rq.send(body); rq.end(function (resp) { if (resp.status === 201) { // API returns CREATED on success!? resolve(resp.body); } else { reject(resp.error || resp.body); } }); }); };
// @flow import validate from 'validate.js'; /* Trivial case */ // $ExpectError validate.foo(); validate.isString(true) validate.isArray(true) validate({ }, { foofield: { required: true, } }, { format: 'grouped' }) /* validate.validators */ validate.validators.new_validator = () => {}; // $ExpectError validate.validators.new_validator = true;
validate.validators.array = function (value, attributes, attributeName, options, constraints) { if (value === undefined || validate.isArray(value)) { return null; } return 'must be array or null'; };