Skip to content

Commit

Permalink
Print warning if a validation error has a fix
Browse files Browse the repository at this point in the history
  • Loading branch information
freekh committed Aug 24, 2023
1 parent 98648c1 commit 34c50aa
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 13 deletions.
9 changes: 5 additions & 4 deletions packages/cli/src/validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,14 @@ export async function validate({
valModule.errors.validation
)) {
for (const v of validationErrors) {
if (fix) {
if (v.fixes && v.fixes.length > 0) {
const fixPatch = await createFixPatch(
{ projectRoot },
!!fix,
sourcePath as SourcePath,
v
);
if (fixPatch?.patch && fixPatch?.patch.length > 0) {
if (fix && fixPatch?.patch && fixPatch?.patch.length > 0) {
await service.patch(moduleId, fixPatch.patch);
console.log(
picocolors.green("✔"),
Expand All @@ -62,8 +63,8 @@ export async function validate({
fixPatch?.remainingErrors?.forEach((e) => {
errors += 1;
console.log(
picocolors.red("✘"),
"Found non-fixable error in",
v.fixes ? picocolors.yellow("⚠") : picocolors.red("✘"),
`Found ${v.fixes ? "fixable " : ""}error in`,
`${sourcePath}:`,
e.message
);
Expand Down
82 changes: 73 additions & 9 deletions packages/server/src/createFixPatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import crypto from "crypto";

export async function createFixPatch(
config: { projectRoot: string },
apply: boolean,
sourcePath: SourcePath,
validationError: ValidationError
): Promise<{ patch: Patch; remainingErrors: ValidationError[] } | undefined> {
Expand Down Expand Up @@ -68,15 +69,78 @@ export async function createFixPatch(

// skips if the metadata is already correct
if (!metadataIsCorrect) {
patch.push({
op: "replace",
path: sourceToPatchPath(sourcePath).concat("metadata"),
value: {
width: imageMetadata.width,
height: imageMetadata.height,
sha256: imageMetadata.sha256,
},
});
if (apply) {
patch.push({
op: "replace",
path: sourceToPatchPath(sourcePath).concat("metadata"),
value: {
width: imageMetadata.width,
height: imageMetadata.height,
sha256: imageMetadata.sha256,
},
});
} else {
if (
typeof currentValue === "object" &&
currentValue &&
"metadata" in currentValue &&
currentValue.metadata &&
typeof currentValue.metadata === "object"
) {
if (
!("sha256" in currentValue.metadata) ||
currentValue.metadata.sha256 !== imageMetadata.sha256
) {
remainingErrors.push({
message:
"Image metadata sha256 is incorrect! Found: " +
("sha256" in currentValue.metadata
? currentValue.metadata.sha256
: "<empty>") +
". Expected: " +
imageMetadata.sha256 +
".",
fixes: undefined,
});
}
if (
!("width" in currentValue.metadata) ||
currentValue.metadata.width !== imageMetadata.width
) {
remainingErrors.push({
message:
"Image metadata width is incorrect! Found: " +
("width" in currentValue.metadata
? currentValue.metadata.width
: "<empty>") +
". Expected: " +
imageMetadata.width,
fixes: undefined,
});
}
if (
!("height" in currentValue.metadata) ||
currentValue.metadata.height !== imageMetadata.height
) {
remainingErrors.push({
message:
"Image metadata height is incorrect! Found: " +
("height" in currentValue.metadata
? currentValue.metadata.height
: "<empty>") +
". Expected: " +
imageMetadata.height,
fixes: undefined,
});
}
} else {
remainingErrors.push({
...validationError,
message: "Image metadata is not an object!",
fixes: undefined,
});
}
}
}
} else if (fix === "image:add-metadata") {
patch.push({
Expand Down

0 comments on commit 34c50aa

Please sign in to comment.