Skip to content

add ability to access other object values in the "conform" function #22

@leonidaz

Description

@leonidaz

it's very difficult to validate certain properties without being able to refer to other object properties: e.g. comparing two dates, etc.

it would be pretty easy to create this functionality via "conform" attribute -- conform function could receive one extra parameter "object" which would reference the whole object with properties being validated.

so, when calling checkType, the "conform" constraint would take this form:

constrain('conform', value, function (a, e, object) { return e(a, object) });

instead of the current:

constrain('conform', value, function (a, e) { return e(a) });

and the "constrain" function would change from this:

function constrain(name, value, assert) {
  if (schema[name] !== undefined && !assert(value, schema[name])) {
    error(name, property, value, schema, errors);
  }
}

to this ('object' would exist in this context just like schema)

function constrain(name, value, assert) {
  if (schema[name] !== undefined && !assert(value, schema[name], object)) {
    error(name, property, value, schema, errors);
  }
}

consequently, the "conform" attribute would take this form:

{ conform: function (v,object) {
        if(object['some_other_property'] > v) return true;
        return false;
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions