alank64 / json-schema-filter Goto Github PK
View Code? Open in Web Editor NEWFilters (removes) objects from document based on passed json-schema
License: MIT License
Filters (removes) objects from document based on passed json-schema
License: MIT License
Hey
I am using this module to whitelist keys that can be exposed by API. IMHO the filter should not omit key-value pairs that have null
as value. As the current behavior is in place it cannot be changed. But maybe we could make a third parameter for the filter method where you could switch on/off null values omitting?
My suggestion would be to have a general property for allowing all null values. And possibility to switch separately for every type
.
var result = filter(schema, document, {
allowNulls: true
// or
allowNulls: ['boolean', 'number', 'integer', 'object', 'array', 'string', 'other']
})
Would it be considerable? PR?
BR
Any chance this will get additional properties support? I'm trying to use this to filter map/dictionary style responses but I'm a JS noob and have been having a hard time getting it to work with them.
I think there is a clash because the GeoJSON object has a object called properties
.
What do you think of adding code to this library to check that the input is an object? Right now, the code below would throw an error:
var schema = {
"title": "Example Schema",
"type": "object",
"properties": {
"firstName": {
"type": "string"
}
}
};
filter(schema, null);
filter(schema, 'mystring');
For my use case, I'm going to add code like the following before calling filter
:
if (typeof input !== 'object') {
return {}; // or maybe return null?
}
Want it in this library? Any extra checks will decrease performance, so maybe it belongs outside this library.
Right now empty strings are automatically removed. Would it be possible to keep them, as it is often useful to reset data in forms?
Thanks
Perhaps I'm missing something, but the example shows validation keys that I don't see anywhere in the source code: 'required', or blocks like this:
"age": { "description": "Age in years", "type": "integer", "minimum": 0 },
This Is a bit confusing to me.
Is that supposed to actually work or were there significant changes?
JSON-SCHEMA states that if the "properties" key is missing, then any object is valid for a type: "object". A check to see if the key "properties:" is missing should be handled.
Line 31, 32 and 33 of index.js are:
}else{
if (doc[key]) results[key] = doc[key];
}
since you are checking doc[key] which will result in false, it only currently works with booleans that are set to true. I have modified it to check if(doc[key] != null).
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.