Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

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

Open
Leonidaz opened this Issue · 3 comments

4 participants

@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;
    }
}
@JoeStanton JoeStanton referenced this issue from a commit in redbadger/revalidator
@JoeStanton JoeStanton Used the patch from revalidator issue #22. With a slight fix for pass…
…ing the object
c48ef20
@pksunkara pksunkara closed this in c963b26
@jfhbrook

@stefansedich, @Leonidaz,

I had to revert this pull request because it broke a number of tests. As such I'm reopening this issue. Feel free to try again with a PR.

I do have a question though: Why can't you just have the conform function defined in a sub-scope of a closure that includes the object? I'm not sure this change quite pays the rent.

@jfhbrook jfhbrook reopened this
@Leonidaz

@jesusabdullah

it may be doable with closures but it would be cumbersome because then one would need to create instances of the schema. it may not be the best implementation but still it would be nice to have access to the object being validated. i'll see if i can check out the tests.

@elad

I understand the need for this functionality (I also need it for roughly the same purposes) but I don't think the implementation proposed here is ideal. What I had in mind instead is allowing conform on the entire object. As in:

var schema = {
    properties: {
        from: {
            type: 'string',
            format: 'date-time',
            required: true
        },

        to: {
            type: 'string',
            format: 'date-time',
            required: true
        }
    },

    conform: function (o) {
        // check from/to
    }
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.