Beispiel #1
0
function validate(attributeName, context, options={}) {
  options = merge({}, defaultOptions, options);
  const value = get(context, attributeName);
  const { min, max, equal } = getProperties(options, "min", "max", "equal");

  Config.LOG_VALIDATION && Logger.log(`Validation : <<validator>> : '${VALIDATOR_NAME}' called on %s with options %o`, attributeName, options);

  if (Ember.isBlank(value)) { return resolve(); }

  if (Ember.typeOf(value) !== "string" || Ember.typeOf(value.toString) !== "function") {
    return reject( createError(get(options, "messages.default"), value, VALIDATOR_NAME) );
  }

  if (!Ember.isNone(min) && !Ember.isNone(max) && (value.length < min || value.length > max)) {
    return reject( createError(get(options, "messages.out_of_range"), value, VALIDATOR_NAME) );
  }

  if (!Ember.isNone(min) && value.length < min) {
    return reject( createError(get(options, "messages.less_then"), value, VALIDATOR_NAME) );
  }

  if (!Ember.isNone(max) && value.length > max) {
    return reject( createError(get(options, "messages.greater_then"), value, VALIDATOR_NAME) );
  }

  if (!Ember.isNone(equal) && value.length !== parseInt(equal)) {
    return reject( createError(get(options, "messages.not_equal"), value, VALIDATOR_NAME) );
  }

  return resolve();
}
 "validate": function() {
   return Ember.RSVP.reject(createError("error-code", "value", "validator-name"));
 }
Beispiel #3
0
    const attribute = get(this, "attribute"),
          options = this.optionsToJSON(),
          context = this.getSnapshot("context"),
          validator = this.get("validator");

    const attributeValue = context.get(attribute);
    if (Ember.isNone(attributeValue)) {
      return Ember.RSVP.resolve();
    }

    const isValidatable = attributeValue.get("isValidatable");

    if (options.required) {
      if (!attributeValue.get("content")) {
        let error = createError("required", attributeValue, "required");
        return RSVP.reject(error.set("options", options));
      }
      return RSVP.resolve();
    }

    Ember.assert("The proxy mediator working with validatable objects only", isValidatable);

    Config.LOG_VALIDATION && Ember.Logger.log("Validation : <<mediator>> : Validate : _validate '%s'", this.get('attribute'));

    const promise = attributeValue.validate();
    return promise.catch(errors => {

      errors.forEach(errors => {
        errors.forEach(error => {
          error.set("options", options);