From 2dcc346057a01af3e21443b07d06b28a324114c2 Mon Sep 17 00:00:00 2001 From: Agustin Ramos Date: Fri, 6 Jul 2018 15:49:37 -0300 Subject: [PATCH] add property 'default' --- README.md | 10 +++++--- index.js | 77 ++++++++++++++++++++++++++++--------------------------- 2 files changed, 46 insertions(+), 41 deletions(-) diff --git a/README.md b/README.md index 0553124..956eb46 100644 --- a/README.md +++ b/README.md @@ -24,9 +24,11 @@ var schema = { "properties": { "firstName": { "type": "string" + "default": "" }, "lastName": { - "type": "string" + "type": "string", + "default": "" }, "age": { "description": "Age in years", @@ -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 = { @@ -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 \ No newline at end of file diff --git a/index.js b/index.js index 5cc30a5..fa7c2fe 100644 --- a/index.js +++ b/index.js @@ -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 }