Comparing version 1.0.15 to 1.0.16
@@ -65,4 +65,11 @@ export = Sjablong; | ||
*/ | ||
createObjectFromSchemaDefaults(schema: object, preview?: boolean): object; | ||
/** | ||
* Creates a JSON object with default or preview values from a JSON Schema | ||
* @param {object} schema The JSON Schema generated for this template | ||
* @param {boolean} excludeNonDefault Should properties without a default value be extracted? | ||
* @param {boolean} preview Should preview values be used if default is empty? | ||
* @returns {object} A object of all default values | ||
*/ | ||
createObjectFromSchema(schema: object, skipNonDefault?: boolean, preview?: boolean): object; | ||
/** | ||
* Validates data against the schema of the template | ||
@@ -69,0 +76,0 @@ * @param {object} JSONSchema The JSON Schema generated for this template |
@@ -327,23 +327,77 @@ /* | ||
*/ | ||
createObjectFromSchemaDefaults (schema, preview = false) { | ||
// createObjectFromSchemaDefaults (schema, preview = false) { | ||
// // Input validation | ||
// if (!schema) { throw new VTFKError('No schema was provided'); } | ||
// inspect(schema); | ||
// function recursivelyFindValues (current, currentKey, parentKey, objectToReturn, preview) { | ||
// if (!current) { return; } | ||
// if (typeof current !== 'object') { return; } | ||
// if (currentKey === '0') { return; } | ||
// // Check if the object is not of type object and has a default or preview value | ||
// if (current.type && current.type !== 'object' && current.path) { | ||
// if (current.default) { | ||
// set(objectToReturn, current.path, current.default); | ||
// } else if (preview && current.preview) { | ||
// set(objectToReturn, current.path, current.preview); | ||
// } | ||
// return; | ||
// } else { | ||
// for (const key in current) { | ||
// recursivelyFindValues(current[key], key, currentKey, objectToReturn, preview); | ||
// } | ||
// } | ||
// return objectToReturn; | ||
// } | ||
// const obj = recursivelyFindValues(schema, 'root', 'parentKey', {}, preview); | ||
// return obj; | ||
// } | ||
/** | ||
* Creates a JSON object with default or preview values from a JSON Schema | ||
* @param {object} schema The JSON Schema generated for this template | ||
* @param {boolean} excludeNonDefault Should properties without a default value be extracted? | ||
* @param {boolean} preview Should preview values be used if default is empty? | ||
* @returns {object} A object of all default values | ||
*/ | ||
createObjectFromSchema(schema, skipNonDefault = true, preview = false) { | ||
// Input validation | ||
if (!schema) { throw new VTFKError('No schema was provided'); } | ||
inspect(schema); | ||
function recursivelyFindValues (current, currentKey, parentKey, objectToReturn, preview) { | ||
// Function that recursively determines the values? | ||
function recursivelyFindValues (current, currentKey, currentPath, objectToReturn, preview) { | ||
if (!current) { return; } | ||
if (typeof current !== 'object') { return; } | ||
if (currentKey === '0') { return; } | ||
// Update the current path if applicable | ||
if(currentKey !== 'properties' && currentKey) currentPath += currentKey + '.'; | ||
// Check if the object is not of type object and has a default or preview value | ||
if (current.type && current.type !== 'object') { | ||
// Determine what path to use | ||
let path = current.path; | ||
if(currentPath) path = currentPath; | ||
if(path.endsWith('.')) path = path.substring(0, path.length - 1); | ||
// Skip if applicable | ||
if(skipNonDefault && !preview && !current.default) { return; } | ||
if((skipNonDefault && preview) && (!current.default && !current.preview)) { return; } | ||
// Check if the object is not of type object and has a default or preview value | ||
if (current.type && current.type !== 'object' && current.path) { | ||
if (current.default) { | ||
set(objectToReturn, current.path, current.default); | ||
} else if (preview && current.preview) { | ||
set(objectToReturn, current.path, current.preview); | ||
} | ||
// Determine what value to set | ||
let value = undefined; | ||
if(preview && current.preview) value = current.preview; | ||
if(current.default) value = current.default; | ||
// Set the value | ||
set(objectToReturn, path, value); | ||
// return | ||
return; | ||
} else { | ||
for (const key in current) { | ||
recursivelyFindValues(current[key], key, currentKey, objectToReturn, preview); | ||
recursivelyFindValues(current[key], key, currentPath, objectToReturn, preview); | ||
} | ||
@@ -355,3 +409,3 @@ } | ||
const obj = recursivelyFindValues(schema, 'root', 'parentKey', {}, preview); | ||
const obj = recursivelyFindValues(schema, '', '', {}, preview); | ||
@@ -358,0 +412,0 @@ return obj; |
{ | ||
"name": "sjablong", | ||
"version": "1.0.15", | ||
"version": "1.0.16", | ||
"description": "A system for replacing and/or validating replacement-fields in text", | ||
@@ -25,2 +25,3 @@ "main": "index.js", | ||
"handlebars": "^4.7.7", | ||
"json-schema-defaults": "^0.4.0", | ||
"lodash.get": "^4.4.2", | ||
@@ -27,0 +28,0 @@ "lodash.set": "^4.3.2", |
@@ -14,10 +14,40 @@ /* | ||
console.log('== Template =='); | ||
console.log(template); | ||
const schemaWithoutPaths = { | ||
type: 'object', | ||
properties: { | ||
info: { | ||
type: 'object', | ||
properties: { | ||
'our-date': { | ||
label: 'Vår dato', | ||
type: 'string' | ||
}, | ||
'our-reference': { | ||
label: 'Vår referanse', | ||
type: 'string', | ||
preview: 'test' | ||
}, | ||
paragraph: { | ||
label: 'Paragraf', | ||
type: 'string', | ||
default: 'Offl. § 13 jf. fvl. §13 (1)', | ||
required: true | ||
} | ||
} | ||
} | ||
} | ||
} | ||
console.log('== Schema =='); | ||
const schema = Sjablong.generateSchema(template); | ||
inspect(schema); | ||
let obj = Sjablong.createObjectFromSchema(schemaWithoutPaths, true, true) | ||
console.log('== Flattened schema =='); | ||
console.log(Sjablong.flattenSchema(schema)); | ||
inspect(obj); | ||
// console.log('== Template =='); | ||
// console.log(template); | ||
// console.log('== Schema =='); | ||
// const schema = Sjablong.generateSchema(template); | ||
// inspect(schema); | ||
// console.log('== Flattened schema =='); | ||
// console.log(Sjablong.flattenSchema(schema)); |
34026
769
8
+ Addedjson-schema-defaults@^0.4.0
+ Addedjson-schema-defaults@0.4.0(transitive)