Some more fixes, getting a balance of validity checking, not letting package output anything non-sensical.

This commit is contained in:
mike12345567 2021-02-03 12:38:06 +00:00
parent 4f2fd656c5
commit 9aaf6b4883
2 changed files with 17 additions and 18 deletions

View File

@ -83,25 +83,21 @@ module.exports.processObjectSync = (object, context) => {
* @returns {string} The enriched string, all templates should have been replaced if they can be.
*/
module.exports.processStringSync = (string, context) => {
const input = cloneDeep(string)
if (!exports.isValid(string)) {
return string
}
let clonedContext = removeNull(cloneDeep(context))
clonedContext = addConstants(clonedContext)
// remove any null/undefined properties
if (typeof string !== "string") {
throw "Cannot process non-string types."
}
try {
string = processors.preprocess(string)
// this does not throw an error when template can't be fulfilled, have to try correct beforehand
const template = hbsInstance.compile(string, {
strict: false,
})
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
}
string = processors.preprocess(string)
// this does not throw an error when template can't be fulfilled, have to try correct beforehand
const template = hbsInstance.compile(string, {
strict: false,
})
return processors.postprocess(template(clonedContext))
}
/**
@ -119,9 +115,9 @@ module.exports.makePropSafe = property => {
* @returns {boolean} Whether or not the input string is valid.
*/
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
const invalidCases = [`expecting 'id', 'string', 'number'`]
const invalidCases = [`expecting '`]
// don't really need a real context to check if its valid
const context = {}
try {

View File

@ -317,9 +317,12 @@ describe("Cover a few complex use cases", () => {
expect(validity).toBe(true)
})
it("should confirm an invalid string", () => {
const validity = isValid("{{ awdd () ")
expect(validity).toBe(false)
it("should confirm a bunch of invalid strings", () => {
const invalids = ["{{ awd )", "{{ awdd () ", "{{ awdwad ", "{{ awddawd }"]
for (let invalid of invalids) {
const validity = isValid(invalid)
expect(validity).toBe(false)
}
})
it("input a garbage string, expect it to be returned", async () => {