Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to filter JSON objects according to "schema"

I build a RESTful api with node.js and express / koa.

I want to filter the JSON data input - for security reasons as well to have only the needed business specific properties. After filtering the business-specific validation takes place.

How can I throw away unwanted JSON / JS object properties - i.e. properties not in my database schema as well as empty properties?

  • Define Schema & filter according to this? Using e.g. https://github.com/alank64/json-schema-filter
  • Any configurable filtering available?
like image 387
Michael Schmidt Avatar asked Jan 02 '15 15:01

Michael Schmidt


2 Answers

I think joi is a good library for validation and normalization. You can also sometimes get away with something as simple as _.pick from lodash/underscore.

like image 87
Peter Lyons Avatar answered Sep 18 '22 10:09

Peter Lyons


You may consider using JSON-schema validators.

http://json-schema.org/implementations.html

Validators benchmark: https://github.com/ebdrup/json-schema-benchmark

Disclaimer: I created ajv

like image 29
esp Avatar answered Sep 18 '22 10:09

esp