feathersjs-ecosystem / feathers-hooks-common

Useful hooks for use with FeathersJS services.
https://hooks-common.feathersjs.com
MIT License
193 stars 89 forks source link

Suggestion: How to validate data for `patch(data)`? #237

Closed eddyystop closed 3 years ago

eddyystop commented 7 years ago

This issue is meant to invite comments.

https://medium.com/@markherhold/validating-json-patch-requests-44ca5981a7fc

These talk about Joi (see eddyystop/feathers-hooks-validate-joi) but the issues are the same. https://github.com/eddyystop/feathers-hooks-validate-joi/issues/5 https://github.com/MarkHerhold/json-patch-joi https://tools.ietf.org/html/rfc6902

daffl commented 7 years ago

I remember discussing one version where we do a get of the old version, performa (deep) merge and then do a normal validation.

eddyystop commented 7 years ago

That's one option covered in the article. Its a possiblly costly approach.

beeplin commented 6 years ago

For mongoDB v3.6, using $jsonSchema operator will do this job. Internally mongoDB updates/patches the data first, and then validate the updated/patched result. If the result is not valid (according to the previously provided json schema), it will automatically roll back and throw errors.

This is the major reason why I transferred from mongoose to vanilla mongodb.

https://docs.mongodb.com/manual/core/schema-validation/index.html

danlupascu commented 5 years ago

@beeplin Have you tried this with feathers? Can you give me an idea on how to add this to feathers?

beeplin commented 5 years ago

@danlupascu this feature comes from mongodb and has nothing to do with feathers. You don't need to do anything in feathers. Just add json schema in your mongodb collections, and do your create/update/patch in feathers as normal. Mongodb will throw errors if the the written data breaks the schema.