question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unable to use validators with PatchJSONParameters

See original GitHub issue

I am not able to use the validates decorator using PatchJSONParameters.

For example, using the following code raise a ValueError (field does not exist) while there are fields in PATH_CHOICES.

@validates('name')
def validate_name(self, data):
    if len(data)<3:
        raise ValidationError('Too short!')

I am not even sure that this is an issue as I don’t know how I am supposed to do it. I can see there is already some code to map validators with patched fields in PatchJSONParameters.__init__:

self.fields['op'].validators = \
    self.fields['op'].validators + [validate.OneOf(self.OPERATION_CHOICES)]
self.fields['path'].validators = \
    self.fields['path'].validators + [validate.OneOf(self.PATH_CHOICES)]

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
DurandAcommented, Sep 15, 2016

Thank you @frol for your support and detailed answers. 😃 You should get paid for this! The article from William Durand is indeed excellent reading. I think that the workaround is good enough.

I wish that your patches (especially marshmallow integration) will be merged into Flask-RESTPlus one day.

1reaction
frolcommented, Sep 14, 2016

@DurandA Please, read this short article on the PATCH matter: http://williamdurand.fr/2014/02/14/please-do-not-patch-like-an-idiot/. After you read it, you should understand that the classes inherited from PatchJSONParameters have only 3 (three) constant fields: op, path, and value. Thus, there won’t be any name field. You may try to use @validates_schema (it should work in theory, but I haven’t tried it myself):

@validates_schema
def validate_name(self, data):
   if data['op'] == 'replace' and data['path'] == '/name' and len(data['value']) < 3:
        raise ValidationError('Too short!', 'name')

NOTE: This is just a workaround since there must be a nicer way to implement this.

Read more comments on GitHub >

github_iconTop Results From Across the Web

PATCH with ValidateModelState enabled and [Required ...
If I provide ValidateModelState =true, then every PATCH failed on validation on properties with [Required] attribute.
Read more >
Unable to patch data to FormArray - Stack Overflow
Take care when patching array. Because patchValue of FormArray patches values by the index: patchValue(value: any[], options: {onlySelf?:
Read more >
Resolve the "Parameter validation failed" error in AWS ...
How do I resolve the "Parameter validation failed: parameter value 'abc' for parameter name 'ABC' does not exist" error in CloudFormation?
Read more >
Validation with Hibernate Validator - Quarkus
This guide covers how to use Hibernate Validator/Bean Validation for: ... The method parameter ( book ) is created from the JSON payload...
Read more >
JSON Patch | jsonpatch.com
JSON Patch is a format for describing changes to a JSON document. It can be used to avoid sending a whole document when...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found