Skip to content

Commit

Permalink
3831 validateStringTemplates
Browse files Browse the repository at this point in the history
  • Loading branch information
BALEHOK committed Jul 1, 2022
1 parent 0ed9f75 commit 6148991
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 20 deletions.
5 changes: 0 additions & 5 deletions src/pageEditor/validation/validateRenderers.ts
Expand Up @@ -41,11 +41,6 @@ function validateRenderers(
traversePipeline({
pipeline: extensionPipeline,
visitPipeline({ pipeline, pipelinePath, parentNode }) {
console.log("validate renderer", {
parentNode,
pipelinePath,
});

const isRootPipeline = parentNode === null;
// Only run validation for root pipeline and document Brick sub pipeline
if (
Expand Down
37 changes: 22 additions & 15 deletions src/pageEditor/validation/validateStringTemplates.ts
Expand Up @@ -17,11 +17,13 @@

import { FormikErrorTree } from "@/pageEditor/tabs/editTab/editTabTypes";
import { BlockPipeline } from "@/blocks/types";
import { isEmpty, set } from "lodash";
import { isEmpty } from "lodash";
import { isExpression, isTemplateExpression } from "@/runtime/mapArgs";
import { isMustacheOnly } from "@/components/fields/fieldUtils";
import { UnknownObject } from "@/types";
import { joinName } from "@/utils";
import { joinName, joinPathParts } from "@/utils";
import { traversePipeline } from "@/pageEditor/utils";
import { setPipelineBlockError } from "./setPipelineBlockError";

const MUSTACHE_ERROR_MESSAGE =
"Invalid string template. Read more about string templates: https://docs.pixiebrix.com/nunjucks-templates";
Expand All @@ -31,18 +33,20 @@ function validateObject(
namePath: string,
errors: FormikErrorTree
) {
for (const prop of Object.keys(config)) {
const propNamePath = joinName(namePath, prop);
// eslint-disable-next-line security/detect-object-injection -- iterating through props
const value = config[prop];
for (const [prop, value] of Object.entries(config)) {
if (
isTemplateExpression(value) &&
value.__type__ !== "mustache" &&
isMustacheOnly(value.__value__)
) {
set(errors, propNamePath, MUSTACHE_ERROR_MESSAGE);
// We should use 'joinName' here b/c the form fields can have special chars
setPipelineBlockError(
errors,
MUSTACHE_ERROR_MESSAGE,
joinName(namePath, prop)
);
} else if (typeof value === "object" && !isExpression(value)) {
validateObject(value as UnknownObject, propNamePath, errors);
validateObject(value as UnknownObject, joinName(namePath, prop), errors);
}
}
}
Expand All @@ -55,13 +59,16 @@ function validateStringTemplates(
return;
}

for (const [index, block] of pipeline.entries()) {
validateObject(
block.config,
joinName(index.toString(), "config"),
pipelineErrors
);
}
traversePipeline({
pipeline,
visitBlock({ blockConfig, path }) {
validateObject(
blockConfig.config,
joinPathParts(path, "config"),
pipelineErrors
);
},
});
}

export default validateStringTemplates;

0 comments on commit 6148991

Please sign in to comment.