Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@ var schema = {
"properties": {
"firstName": {
"type": "string"
"default": ""
},
"lastName": {
"type": "string"
"type": "string",
"default": ""
},
"age": {
"description": "Age in years",
Expand Down Expand Up @@ -64,11 +66,11 @@ var schema = {
"required": ["firstName", "lastName"]
};

var document = {firstName: 'John', lastName: 'Dow!', shouldNot: 'see this!'};
var document = {lastName: 'Dow!', shouldNot: 'see this!'};

var results = filter(schema, document);

console.log(results); // # {firstName: 'John', lastName: 'Dow!'}
console.log(results); // # {firstName: '', lastName: 'Dow!'}

// Works on nested objects and arrays as well...
var document2 = {
Expand All @@ -93,3 +95,5 @@ console.log(nestedResults); // # {firstName: 'Johnny', lastName: 'Dowski', cont
If a ```"type": "object"``` with no ```"property":``` is defined (see 'general:' in above example), or empty, the entire object is removed from the results. It could be that you require it to be included but empty, but to the best of my knowledge I thought it would be cleaner to simply remove it if empty. Else it copies everything over, as in all of what is in the properties of the key.

Background info: The lack of ```property``` is legal in json-schema and means anything goes, or what I refer to as free-form.. free-style.. oh well, pick your meaning for it, it has the word 'free'!

Add property "default" if you want the output of your form to have a default value
77 changes: 39 additions & 38 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,54 +1,55 @@
module.exports = filterObjectOnSchema;
module.exports = filterObjectOnSchema

function filterObjectOnSchema(schema, doc) {
var results

var results;
// console.log('DOC: ', JSON.stringify(doc, null, 2))
// console.log('SCH: ', JSON.stringify(schema, null, 2))

//console.log("DOC: ", JSON.stringify(doc, null, 2));
//console.log("SCH: ", JSON.stringify(schema, null, 2));

if(schema.type === 'object') {
results = {}; // holds this levels items
if (schema.type === 'object') {
results = {} // holds this levels items

// process properties - recursive
Object.keys(schema.properties).forEach(function(key) {
if(typeof(doc[key]) !== 'undefined') {
if(doc[key] === null) {
results[key] = doc[key];
} else {
var sp = schema.properties[key];
if(sp.type === 'object') {

// check if property-less object (free-form)
if(sp.hasOwnProperty('properties')) {
results[key] = filterObjectOnSchema(sp, doc[key]);
} else {
if(Object.keys(doc[key]).length > 0) {
results[key] = doc[key];
}
}
} else if(sp.type === 'array') {
if(doc[key]) results[key] = filterObjectOnSchema(sp, doc[key]);
} else if(sp.type === 'boolean' || sp.type === 'number' || sp.type === 'integer' || sp.type === 'string') {
if(typeof doc[key] !== 'undefined') results[key] = doc[key];
Object.keys(schema.properties).forEach(function (key) {
if (doc[key] !== undefined) {
var sp = schema.properties[key]
if (sp.default !== undefined) {
results[key] = sp.default
}
if (sp.type === 'object') {
// check if property-less object (free-form)
if (sp.hasOwnProperty('properties')) {
results[key] = filterObjectOnSchema(sp, doc[key])
} else {
if(doc[key]) results[key] = doc[key];
if (Object.keys(doc[key]).length > 0) {
results[key] = doc[key]
}
}
} else if (sp.type === 'array') {
if (doc[key]) results[key] = filterObjectOnSchema(sp, doc[key])
} else if (
sp.type === 'boolean' ||
sp.type === 'number' ||
sp.type === 'integer'
) {
if (doc[key] !== null && typeof doc[key] !== 'undefined')
results[key] = doc[key]
} else {
if (doc[key]) results[key] = doc[key]
}
}
});

} else if(schema.type === 'array') {
})
} else if (schema.type === 'array') {
// arrays can hold objects or literals
if(schema.items.type === 'object') {
results = [];
doc.forEach(function(item) {
results.push(filterObjectOnSchema(schema.items, item));
});
if (schema.items.type === 'object') {
results = []
doc.forEach(function (item) {
results.push(filterObjectOnSchema(schema.items, item))
})
} else {
results = doc;
results = doc
}
}

return results;
return results
}