totaljs / framework

Node.js framework
http://www.totaljs.com
Other
4.36k stars 450 forks source link

PATCH method and Schema validation #749

Closed fgnm closed 4 years ago

fgnm commented 4 years ago

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.

petersirka commented 4 years ago

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:

fgnm commented 4 years ago

Thank you as always for the clear explanation :smile: