Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
rules[index] = newRule;
}
});
const values = {};
values[name] = value;
validator.errors = undefined;
validator.isValidating = true;
validator.dirty = true;
const currentActionId = actionId;
validator.actionId = currentActionId;
actionId++;
const result = this.getValidateResult();
result.formData[name] = value;
this.props.onValidate(result.status, result.formData);
const self = this;
new AsyncValidate(schema).validate(values, (errors)=> {
const validators = self.validators;
// in case component is unmount and remount
const nowValidator = validators[name];
// prevent concurrency call
if (nowValidator && nowValidator.actionId === currentActionId) {
validator.errors = errors;
validator.isValidating = false;
validator.dirty = false;
const r = self.getValidateResult();
r.formData[name] = value;
self.props.onValidate(r.status, r.formData);
if (fn) {
fn();
}
}
});
/* eslint no-console:0 no-unused-vars:0 */
import Schema from 'async-validator';
const schema = new Schema({
validator0: {
validator(rule, value) {
return true;
},
},
validator1: {
validator(rule, value) {
return false;
},
},
validator2: {
validator(rule, value) {
return false;
},
message: 'Customize error messages1',
},
allRules[name] = _this6.getRules(fieldMeta, action);
allValues[name] = newField.value;
allFields[name] = newField;
});
this.setFields(allFields); // in case normalize
Object.keys(allValues).forEach(function (f) {
allValues[f] = _this6.fieldsStore.getFieldValue(f);
});
if (callback && isEmptyObject(allFields)) {
callback(isEmptyObject(alreadyErrors) ? null : alreadyErrors, this.fieldsStore.getFieldsValue(fieldNames));
return;
}
var validator = new AsyncValidator(allRules);
if (validateMessages) {
validator.messages(validateMessages);
}
validator.validate(allValues, options, function (errors) {
var errorsGroup = _objectSpread({}, alreadyErrors);
if (errors && errors.length) {
errors.forEach(function (e) {
var fieldName = e.field;
var field = get(errorsGroup, fieldName);
if (_typeof(field) !== 'object' || Array.isArray(field)) {
set(errorsGroup, fieldName, {
errors: []
validateAll = (cb) => {
let validator = new schema(this.validateRules);
let needValidateValues = {};
Object.keys(this.validateRules).forEach((path) => {
needValidateValues[path] = this.getter(path);
});
validator.validate(needValidateValues, (errors) => {
if (cb && typeof cb === 'function') {
cb(errors, this.state.value);
}
if (this.props.enableScrollErrorField && errors && errors.length > 0) {
// todo 默认定位到第一个,最好有报错动效
this.validateRefs[errors[0].field].scrollIntoView &&
this.validateRefs[errors[0].field].scrollIntoView();
window.scroll(
window.scrollX,
window.scrollY - this.props.scrollErrorFieldTopOffset
export const createValidator = (
descriptor: any,
values: any,
callback: (errors: any[]) => void
): any => {
// @ts-ignore
schema.warning = (): void => {}
const validator = new schema(descriptor)
validator.validate(
values,
// @ts-ignore
(errors: any[]): void => {
callback(errors)
}
)
}
it('eq', () => {
const schema = new AsyncValidator({
name: [rules.eq(10, '必须等于10')],
});
schema.validate({
}, (errors) => {
expect(handleError(errors)).toEqual([]);
});
schema.validate({
name: '',
}, (errors) => {
expect(handleError(errors)).toEqual([{ field: 'name', message: '必须等于10' }]);
});
schema.validate({
name: '1234',
}, (errors) => {
expect(handleError(errors)).toEqual([{ field: 'name', message: '必须等于10' }]);
});
//遍历规则的key值
let validateArr = Object.keys(schema)
let validateObj = {}
validateArr.forEach(function (i) {
validateObj[i] = that[i]
schema[i].error = null
})
//这里相当于要把自定义的error给去掉啦。
let descriptor = {}
validateArr.forEach(function (i) {
descriptor[i] = schema[i].rules
})
new Schema(descriptor).validate(validateObj, (errors, fields) => {
if (errors) {
console.log("此刻的errors:", errors)
errors.forEach(function (errorItem) {
//回填到每个字段的验证细节上。
schema[errorItem.field].error = appendString(schema[errorItem.field].error, errorItem.message, ";")
errorMessage = appendString(errorMessage, errorItem.message, ";")
console.log("此刻的errorItem:", errorItem)
console.log("此刻的schema[errorItem.field]:", schema[errorItem.field])
})
}
})
const validate = object => {
const { formSchema } = validReport({ translate })
const validator = new Schema(formSchema)
return validator.validate(object, { suppressWarning: true }).catch(({ errors }) => {
throw new Error(errors[0].message)
})
}
const validate = object => {
const { formSchema } = UniqueSlugForm({ translate, apollo, currentUser })
const validator = new Schema(formSchema)
return validator.validate(object, { suppressWarning: true }).catch(({ errors }) => {
throw new Error(errors[0].message)
})
}
constructor(props) {
super(props);
const { value, defaultValue, rules, name: itemName } = props;
this.state = {
value: value === undefined ? defaultValue : value,
errors: null,
};
if (rules) {
this.validator = new Validator({
[itemName]: getValidateRules(rules),
});
}
}