Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

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

Open
Leonidaz opened this Issue Aug 2, 2012 · 3 comments

Comments

Projects
None yet
4 participants

Leonidaz commented Aug 2, 2012

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 added a commit to redbadger/revalidator that referenced this issue Oct 15, 2012

@pksunkara pksunkara closed this in c963b26 Oct 15, 2012

Contributor

jfhbrook commented Oct 17, 2012

@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 Oct 17, 2012

@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 commented Nov 13, 2014

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