Skip to content
This repository was archived by the owner on Dec 12, 2021. It is now read-only.

Add skipPresence option.#63

Closed
danhper wants to merge 1 commit intoansman:masterfrom
danhper:master
Closed

Add skipPresence option.#63
danhper wants to merge 1 commit intoansman:masterfrom
danhper:master

Conversation

@danhper
Copy link
Copy Markdown

@danhper danhper commented Jul 9, 2015

Hi,

This is a PR to add a skipPresence options to validate.
This is useful to validate data on update.

For example, this would allow to write something like this.

var options = {};
if (request.method === 'PUT') {
  options.skipPresence = true;
}
validate(request.body, constraints, options);

Thanks.

@ansman
Copy link
Copy Markdown
Owner

ansman commented Jul 9, 2015

This could be solved today like this:

var constraints = {
  foo: {
    presence: function(value, attributes, attribute, options, constraints) {
      return options.method !== 'PUT' ? {} : null
    }
  }
};

validate({}, constraints, {method: 'POST'});
// {"foo": ["Foo can't be blank"]}

validate({}, constraints, {method: 'PUT'});
// undefined

I understand this is not as nice as having a dedicated option for it but I don't want to add validator specific options unless there is a very good reason.

I'm in the processing of implementing a change that will pass the global options to a custom validator which means you could do something like this:

var presence = validate.validators.presence;
validate.validators.presence = function(value, options, key, attributes, globalOptions) {
  return globalOptions.method === 'PUT' ? null : presence.call(presence, arguments);
};

It should be ready within the hour.

ansman added a commit that referenced this pull request Jul 9, 2015
@danhper
Copy link
Copy Markdown
Author

danhper commented Jul 10, 2015

This seems to be a more flexible alternative, thanks!
I am closing this PR, as 7f5355d solves the issue.

@danhper danhper closed this Jul 10, 2015
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants