You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
119 lines
5.5 KiB
119 lines
5.5 KiB
Vue.prototype.$camelCased = function (name) {
|
|
return name[0].toLowerCase() + name.substr(1);
|
|
};
|
|
Vue.prototype.$validInternal = function (validator, name, valid, message) {
|
|
if (validator) {
|
|
var errors = this.getErrors();
|
|
var vm = this;
|
|
message = message || validator.message;
|
|
return valid().then(function (value) {
|
|
var error = Enumerable.from(errors).firstOrDefault(o => o.key.toLowerCase() === name.toLowerCase());
|
|
if (value) {
|
|
if (error) {
|
|
if (Enumerable.from(error.value.errors).any(o => o.errorMessage === message)) {
|
|
error.value.errors = Enumerable.from(error.value.errors).where(o => o.errorMessage !== message).toArray();
|
|
}
|
|
if (error.value.errors.length === 0) {
|
|
var index = _.findIndex(errors, o => o.key.toLowerCase() === name.toLowerCase());
|
|
vm.getErrors().splice(index, 1);
|
|
}
|
|
}
|
|
}
|
|
else {
|
|
if (!error) {
|
|
error = {
|
|
key: name,
|
|
value: {
|
|
errors: [{ errorMessage: message }]
|
|
}
|
|
};
|
|
errors.push(error);
|
|
}
|
|
else {
|
|
if (!Enumerable.from(error.value.errors).any(o => o.errorMessage === message)) {
|
|
error.value.errors.push({ errorMessage: message });
|
|
}
|
|
}
|
|
}
|
|
});
|
|
}
|
|
return Promise.resolve('');
|
|
};
|
|
Vue.prototype.$validProperty = function (name, schema, model, errors) {
|
|
schema = schema || this.getSchema();
|
|
model = model || this.getModel();
|
|
errors = errors || this.getErrors();
|
|
var vm = this;
|
|
var property = schema.properties[name];
|
|
var value = model[name];
|
|
var list = [];
|
|
list.push(
|
|
this.$validInternal(property.required, name, o => Promise.resolve(value !== undefined && value !== null&&value!==''))
|
|
);
|
|
if (value) {
|
|
list.push(
|
|
this.$validInternal(property.email, name, o => Promise.resolve(/^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/.test(value)))
|
|
);
|
|
list.push(
|
|
this.$validInternal(property.url, name, o => Promise.resolve(/^(?:(?:(?:https?|ftp):)?\/\/)(?:\S+(?::\S*)?@)?(?:(?!(?:10|127)(?:\.\d{1,3}){3})(?!(?:169\.254|192\.168)(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)(?:\.(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)*(?:\.(?:[a-z\u00a1-\uffff]{2,})).?)(?::\d{2,5})?(?:[/?#]\S*)?$/i.test(value)))
|
|
);
|
|
list.push(
|
|
this.$validInternal(property.date, name, o => Promise.resolve(!/Invalid|NaN/.test(new Date(value).toString())))
|
|
);
|
|
list.push(
|
|
this.$validInternal(property.number, name, o => Promise.resolve(/^(?:-?\d+|-?\d{1,3}(?:,\d{3})+)?(?:\.\d+)?$/.test(value)))
|
|
);
|
|
list.push(
|
|
this.$validInternal(property.digits, name, o => Promise.resolve(/^\d+$/.test(value)))
|
|
);
|
|
//dateiso
|
|
//creditcard
|
|
//accept
|
|
list.push(
|
|
this.$validInternal(property.pattern, name, o => Promise.resolve(new RegExp(property.pattern.regex).test(value)))
|
|
);
|
|
list.push(
|
|
this.$validInternal(property.minLength, name, o => Promise.resolve(value.length >= property.minLength.min))
|
|
);
|
|
list.push(
|
|
this.$validInternal(property.maxLength, name, o => Promise.resolve(value.length <= property.maxLength.max))
|
|
);
|
|
list.push(
|
|
this.$validInternal(property.rangeLength, name, o => Promise.resolve(value.length >= property.rangeLength.min && value.length <= property.rangeLength.max))
|
|
);
|
|
list.push(
|
|
this.$validInternal(property.range, name, o => Promise.resolve(parseFloat(value) >= property.range.min && parseFloat(value) <= property.range.max))
|
|
);
|
|
list.push(this.$validInternal(property.equalTo, name, o => Promise.resolve(o === model[vm.$camelCased(name)]))
|
|
);
|
|
list.push(
|
|
this.$validInternal(property.remote, name, o => new Promise((resolve, reject) => {
|
|
var url = property.remote.url + '?' + name + '=' + value;
|
|
if (property.remote.additionalFields) {
|
|
var keys = property.remote.additionalFields.split(',');
|
|
for (var i = 0; i < keys.length; i++) {
|
|
var key = keys[i];
|
|
url = url + '&' + key + '=' + model[vm.$camelCased(key)];
|
|
}
|
|
}
|
|
axios.get(url).then(function (response) {
|
|
resolve(response.data === true);
|
|
}).catch(function () {
|
|
resolve(false);
|
|
});
|
|
}))
|
|
);
|
|
}
|
|
return Promise.all(list);
|
|
};
|
|
Vue.prototype.$valid = function (schema, model, errors) {
|
|
schema = schema || this.getSchema();
|
|
model = model || this.getModel();
|
|
errors = errors || this.getErrors();
|
|
var vm = this;
|
|
var list = [];
|
|
_.forIn(schema.properties, function (value, key) {
|
|
list.push(vm.$validProperty(key));
|
|
});
|
|
return Promise.all(list);
|
|
}; |