totaljs/framework

PATCH method and Schema validation

fgnm opened this issue · 2 comments

fgnm commented

I've noticed that automatic schema validation does not works if I use PATCH as HTTP method.
For example:

ROUTE('PATCH [api]/v1/user/patch            *user --> @patch', ['authorize']);
...
NEWSCHEMA('user', function (schema) {
    schema.define('region', 'Upper', true);
    schema.setPatch(function($) {
        $.success();
    });
});

Return success even if I request curl -X PATCH -H 'Content-Type: application/json' -i 'http://api.localhost:8000/v1/user/patch' --data '{ }'
In this case the body is empty and no region field is provided, but the schema does not validate. If I use the POST method instead of PATCH it works as expected and returns an error.

The framework validates received fields only with PATCH method. So your behaviour is correct. You can easily check the keys:

schema.setPatch(function($) {
	
	if (!$.keys.length) {
		$.invalid('invalid');
		return;
	}

	// do something

});

Again:

  • PATCH method allows you to violate rules in schema validation
  • in PATCH are validates fields which are really received
  • existing of required fields is optional, but if they are exist then are required
fgnm commented

Thank you as always for the clear explanation 😄