Some more fixes, getting a balance of validity checking, not letting package output anything non-sensical.
This commit is contained in:
parent
96acfc6563
commit
c10cd53eb6
|
@ -83,25 +83,21 @@ module.exports.processObjectSync = (object, context) => {
|
||||||
* @returns {string} The enriched string, all templates should have been replaced if they can be.
|
* @returns {string} The enriched string, all templates should have been replaced if they can be.
|
||||||
*/
|
*/
|
||||||
module.exports.processStringSync = (string, context) => {
|
module.exports.processStringSync = (string, context) => {
|
||||||
const input = cloneDeep(string)
|
if (!exports.isValid(string)) {
|
||||||
|
return string
|
||||||
|
}
|
||||||
let clonedContext = removeNull(cloneDeep(context))
|
let clonedContext = removeNull(cloneDeep(context))
|
||||||
clonedContext = addConstants(clonedContext)
|
clonedContext = addConstants(clonedContext)
|
||||||
// remove any null/undefined properties
|
// remove any null/undefined properties
|
||||||
if (typeof string !== "string") {
|
if (typeof string !== "string") {
|
||||||
throw "Cannot process non-string types."
|
throw "Cannot process non-string types."
|
||||||
}
|
}
|
||||||
try {
|
string = processors.preprocess(string)
|
||||||
string = processors.preprocess(string)
|
// this does not throw an error when template can't be fulfilled, have to try correct beforehand
|
||||||
// this does not throw an error when template can't be fulfilled, have to try correct beforehand
|
const template = hbsInstance.compile(string, {
|
||||||
const template = hbsInstance.compile(string, {
|
strict: false,
|
||||||
strict: false,
|
})
|
||||||
})
|
return processors.postprocess(template(clonedContext))
|
||||||
return processors.postprocess(template(clonedContext))
|
|
||||||
} catch (err) {
|
|
||||||
// suggested that we should always return input if an error occurs, incase string wasn't supposed to
|
|
||||||
// contain any handlebars statements
|
|
||||||
return input
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -119,9 +115,9 @@ module.exports.makePropSafe = property => {
|
||||||
* @returns {boolean} Whether or not the input string is valid.
|
* @returns {boolean} Whether or not the input string is valid.
|
||||||
*/
|
*/
|
||||||
module.exports.isValid = string => {
|
module.exports.isValid = string => {
|
||||||
const validCases = ["string", "number", "object", "array"]
|
const validCases = ["string", "number", "object", "array", "cannot read property"]
|
||||||
// this is a portion of a specific string always output by handlebars in the case of a syntax error
|
// this is a portion of a specific string always output by handlebars in the case of a syntax error
|
||||||
const invalidCases = [`expecting 'id', 'string', 'number'`]
|
const invalidCases = [`expecting '`]
|
||||||
// don't really need a real context to check if its valid
|
// don't really need a real context to check if its valid
|
||||||
const context = {}
|
const context = {}
|
||||||
try {
|
try {
|
||||||
|
|
|
@ -317,9 +317,12 @@ describe("Cover a few complex use cases", () => {
|
||||||
expect(validity).toBe(true)
|
expect(validity).toBe(true)
|
||||||
})
|
})
|
||||||
|
|
||||||
it("should confirm an invalid string", () => {
|
it("should confirm a bunch of invalid strings", () => {
|
||||||
const validity = isValid("{{ awdd () ")
|
const invalids = ["{{ awd )", "{{ awdd () ", "{{ awdwad ", "{{ awddawd }"]
|
||||||
expect(validity).toBe(false)
|
for (let invalid of invalids) {
|
||||||
|
const validity = isValid(invalid)
|
||||||
|
expect(validity).toBe(false)
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
it("input a garbage string, expect it to be returned", async () => {
|
it("input a garbage string, expect it to be returned", async () => {
|
||||||
|
|
Loading…
Reference in New Issue