How to use async-validator - 10 common examples

To help you get started, we’ve selected a few async-validator examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github react-component / form-validation / src / Validation.jsx View on Github external
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();
        }
      }
    });
github yiminghe / async-validator / examples / validator.js View on Github external
/* 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',
  },
github NSFI / ppfish-components / es / components / Form / RcForm / createBaseForm.js View on Github external
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: []
github alibaba / ice / react-materials / components / form-binder / src / FormBinderWrapper.jsx View on Github external
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
github fluent-org / fluent-ui / packages / fluent-ui / src / Form / Form.validator.ts View on Github external
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)
    }
  )
}
github alibaba / nopage / test / validate / index.spec.jsx View on Github external
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' }]);
        });
github eyebluecn / blog-front / src / common / model / base / BaseEntity.js View on Github external
//遍历规则的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])
        })
      }
    })
github Human-Connection / Human-Connection / webapp / components / utils / ReportModal.spec.js View on Github external
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)
    })
  }
github Human-Connection / Human-Connection / webapp / components / utils / UniqueSlugForm.spec.js View on Github external
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)
    })
  }
github zhangkun-Jser / auto-form-create / src / FormField.jsx View on Github external
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),
            });
        }
    }

async-validator

validate form asynchronous

MIT
Latest version published 2 years ago

Package Health Score

79 / 100
Full package analysis