How to use the validatorjs function in validatorjs

To help you get started, we’ve selected a few validatorjs 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 LinusBorg / composition-api-demos / src / composables / use-validation.js View on Github external
const validateAll = () => {
    // can we move this outside of the fn?
    const validator = new Validator(modelRef.value, rules)
    console.log('validating')
    valid.value = !dirty.value || validator.passes()
    errors.value = dirty.value && validator.errors.errors
    // console.log(validator.errors)
  }
github nuitcoder / neubbs / src / main / webapp / src / utils / validate.js View on Github external
export const validateLogin = (values, props) => {
  const { intl: { formatMessage } } = props

  const rules = {
    username: 'required|between:3,15|alpha_num',
    password: 'required|between:6,16',
  }
  const messages = {
    'required.username': formatMessage({ id: 'validate.username.required' }),
    'between.username': formatMessage({ id: 'validate.username.between' }),
    'alpha_num.username': formatMessage({ id: 'validate.username.alpha_num' }),
    'required.password': formatMessage({ id: 'validate.password.required' }),
    'between.password': formatMessage({ id: 'validate.password.between' }),
  }
  const validation = new Validator(values, rules, messages)

  const errors = {}
  if (validation.fails()) {
    errors.username = validation.errors.first('username')
    errors.password = validation.errors.first('password')
  }

  return errors
}
github the-AjK / btb / dashboard / src / components / Profile.js View on Github external
this.password2 = "";
                if (field === "password" && event.target.value !== "" && event.target.value.length < 8) {
                    this.errors.password = "Password too short [min 8 chars]";
                } else {
                    this.errors.password = false;
                }
                if (field === "email") {
                    let validation = new Validator(
                        { email: event.target.value },
                        { email: "required|email" }
                    );
                    validation.passes();
                    this.errors[field] = validation.errors.first(field)
                }
                if (field === "username") {
                    let validation = new Validator(
                        { username: event.target.value },
                        { username: "required" }
                    );
                    validation.passes();
                    this.errors[field] = validation.errors.first(field)
                }
                this[field] = event.target.value
            });
github kentik / mobx-form / src / state / FieldState.js View on Github external
if (!this.rules) {
      this.errors = [];
      return;
    }

    const rules = {
      [this.name]: toJS(this.rules)
    };

    const attributeNames = {
      [this.name]: this.label || this.name
    };

    const { model } = this.form;

    const validator = new Validator({ model, ...values }, rules, this.messages);
    validator.setAttributeNames(attributeNames);
    validator.check();

    this.errors = validator.errors.get(this.name);
  }
github mkozhukharenko / mobx-form-validation / src / common / generic-form.store.js View on Github external
onFieldChange = (field, value) => {
    this.form.fields[field].value = value;
    var validation = new Validator(
      this.getFlattenedValues('value'),
      this.getFlattenedValues('rule'));
    this.form.meta.isValid = validation.passes();
    this.form.fields[field].error = validation.errors.first(field)
  };
github JosephusPaye / Keen-UI / src / mixins / ValidatesInput.js View on Github external
validate() {
            if (!this.validationRules || !this.dirty) {
                return;
            }

            let data = {
                value: this.value
            };

            let rules = {
                value: this.validationRules
            };

            let validation = new Validator(data, rules, this.validationMessages);
            validation.setAttributeNames({ value: this.name.replace(/_/g, ' ') });

            this.valid = validation.passes();

            if (!this.valid) {
                this.validationError = validation.errors.first('value');
            }
        }
    }
github the-AjK / btb / dashboard / src / components / Login.js View on Github external
handleChange = field => event => {
        this.setFieldValue(field, event.target.value);
        let { email, password } = this.form.fields;
        var validation = new Validator(
          { email: email.value, password: password.value },
          { email: email.rule, password: password.rule }
        );
        this.setValid(validation.passes());
        this.setFieldError(field, validation.errors.first(field));
      };
github HSLdevcom / jore-map-ui / src / validation / FormValidator.ts View on Github external
public static validate = (value: any, rule: string): IValidationResult => {
        const validator = new Validator(
            {
                value
            },
            {
                value: rule
            },
            ruleTranslations
        );

        const isValid = Boolean(validator.passes());
        const firstErrorMessage = validator.errors.first('value');

        return {
            isValid,
            errorMessage: typeof firstErrorMessage === 'string' ? firstErrorMessage : ''
        };
github Eazybee / useFormBee / src / hooks / useFormBee.js View on Github external
const validate = (name, value) => {
        const validation = new Validator(
          { [name]: value },
          { [name]: rules[name] },
        );

        const errorMessage = validation.fails() && validation.errors.first(name);

        if (errorMessage) {
          newErrors[name] = errorMessage;
          hasError = false;
        } else {
          delete newErrors[name];
        }
      };