Skip to content

Commit

Permalink
Merge pull request #2 from binoyPeries/main
Browse files Browse the repository at this point in the history
Improve validation error msg
  • Loading branch information
binoyPeries authored Aug 16, 2024
2 parents bec42f7 + 6ef2fa1 commit b592154
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 38 deletions.
57 changes: 38 additions & 19 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36096,7 +36096,7 @@ yup.addMethod(yup.array, "checkEndpointNameUniqueness", function () {
return true;
});
return (
isUnique || new yup.ValidationError("Endpoint names must be unique")
isUnique || new yup.ValidationError("Endpoint names must be unique.")
);
},
});
Expand All @@ -36109,7 +36109,7 @@ yup.addMethod(yup.object, "basePathRequired", function () {
const { type } = testCtx.parent;
if (BASE_PATH_REQUIRED_TYPES.includes(type) && !value.basePath) {
return new yup.ValidationError(
"Base path is required for REST, GraphQL, and WS endpoints"
"Base path is required for REST, GraphQL, and WS endpoints."
);
}
return true;
Expand All @@ -36127,7 +36127,7 @@ yup.addMethod(yup.string, "schemaFileExists", function (srcDir) {
return (
hasFile ||
new yup.ValidationError(
`Schema file does not exist at the given path ${value}`
`Schema file does not exist at the given path ${value}.`
)
);
} catch (error) {
Expand All @@ -36145,11 +36145,11 @@ const serviceSchema = yup
basePath: yup.string().matches(/^\/[a-zA-Z0-9\/-]*$/, "Invalid base path"),
port: yup
.number()
.required("Missing port number")
.moreThan(1000, "Port number must be greater than 1000")
.lessThan(65535, "Port number must be less than 65535"),
.required("Missing port number.")
.moreThan(1000, "Port number must be greater than 1000.")
.lessThan(65535, "Port number must be less than 65535."),
})
.required("service definition is required")
.required("service definition is required.")
.basePathRequired();
// endpointSchema - Schema for endpoint definition
const endpointSchema = (srcDir) =>
Expand All @@ -36159,20 +36159,20 @@ const endpointSchema = (srcDir) =>
yup.object().shape({
name: yup
.string()
.required("Endpoint name is required")
.max(50, "Endpoint name must be less than 50 characters")
.required("Endpoint name is required.")
.max(50, "Endpoint name must be less than 50 characters.")
.matches(
/^[a-z0-9-]+$/,
"Endpoint name must only contain lowercase letters, digits, and hyphens"
"Endpoint name must only contain lowercase letters, digits, and hyphens."
),
displayName: yup
.string()
.required()
.max(50, "Display name must be less than 50 characters"),
.max(50, "Display name must be less than 50 characters."),
service: serviceSchema,
type: yup
.string()
.required("Missing endpoint type")
.required("Missing endpoint type.")
.oneOf(ALLOWED_TYPES),
networkVisibilities: yup
.array()
Expand Down Expand Up @@ -38094,14 +38094,22 @@ const fs = __nccwpck_require__(7147);
const yaml = __nccwpck_require__(3966);
const { componentYamlSchema } = __nccwpck_require__(1677);

// sanitizeSrcRootPath - remove trailing slash from the path
function sanitizeSrcRootPath(path) {
if (path.endsWith("/")) {
return path.slice(0, -1);
}
return path;
}

function readComponentYaml(filePath) {
try {
fullPath = `${filePath}/.choreo/component.yaml`;
let fileContent = fs.readFileSync(fullPath, "utf8");
return fileContent;
} catch (error) {
console.log(
"No component.yaml found, hence skipping the validation:",
"No component.yaml found, hence skipping the validation: ",
error.message
);
core.setOutput(
Expand All @@ -38113,32 +38121,43 @@ function readComponentYaml(filePath) {
}
}

function constructValidationErrorMessage(errors) {
let errorMessage = "Failed to validate component.yaml";
if (errors.length == 0) {
return errorMessage;
}
const errorList =
errors.length === 1 ? errors[0] : errors.map((e) => `\n- ${e}`).join("");
return `${errorMessage}: ${errorList}`;
}

// Main code
let sourceRootDir = "";
try {
sourceRootDir = core.getInput("source-root-dir-path");
} catch (error) {
console.log("Failed to read input:", error.message);
console.log("Failed to read input: ", error.message);
core.setFailed("Failed to read input", error.message);
}

try {
fileContent = readComponentYaml(sourceRootDir);
const sanitizedPath = sanitizeSrcRootPath(sourceRootDir);
fileContent = readComponentYaml(sanitizedPath);
if (fileContent !== null) {
// Parse the yaml content
componentYamlFile = yaml.load(fileContent);
componentYamlSchema(sourceRootDir)
.validate(componentYamlFile)
componentYamlSchema(sanitizedPath)
.validate(componentYamlFile, { abortEarly: false })
.then(() => {
core.setOutput("Component.yaml validation", "Successful");
})
.catch((err) => {
console.log("Component.yaml validation failed:", err.errors);
console.log(constructValidationErrorMessage(err.errors));
core.setFailed("Component.yaml validation failed", err.errors);
});
}
} catch (error) {
console.log("Failed to validate component.yaml:", error.message);
console.log("Failed to validate component.yaml: ", error.message);
core.setFailed("Failed to validate component.yaml", error.message);
}

Expand Down
33 changes: 26 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,22 @@ const fs = require("fs");
const yaml = require("js-yaml");
const { componentYamlSchema } = require("./schemas");

// sanitizeSrcRootPath - remove trailing slash from the path
function sanitizeSrcRootPath(path) {
if (path.endsWith("/")) {
return path.slice(0, -1);
}
return path;
}

function readComponentYaml(filePath) {
try {
fullPath = `${filePath}/.choreo/component.yaml`;
let fileContent = fs.readFileSync(fullPath, "utf8");
return fileContent;
} catch (error) {
console.log(
"No component.yaml found, hence skipping the validation:",
"No component.yaml found, hence skipping the validation: ",
error.message
);
core.setOutput(
Expand All @@ -22,31 +30,42 @@ function readComponentYaml(filePath) {
}
}

function constructValidationErrorMessage(errors) {
let errorMessage = "Failed to validate component.yaml";
if (errors.length == 0) {
return errorMessage;
}
const errorList =
errors.length === 1 ? errors[0] : errors.map((e) => `\n- ${e}`).join("");
return `${errorMessage}: ${errorList}`;
}

// Main code
let sourceRootDir = "";
try {
sourceRootDir = core.getInput("source-root-dir-path");
} catch (error) {
console.log("Failed to read input:", error.message);
console.log("Failed to read input: ", error.message);
core.setFailed("Failed to read input", error.message);
}

try {
fileContent = readComponentYaml(sourceRootDir);
const sanitizedPath = sanitizeSrcRootPath(sourceRootDir);
fileContent = readComponentYaml(sanitizedPath);
if (fileContent !== null) {
// Parse the yaml content
componentYamlFile = yaml.load(fileContent);
componentYamlSchema(sourceRootDir)
.validate(componentYamlFile)
componentYamlSchema(sanitizedPath)
.validate(componentYamlFile, { abortEarly: false })
.then(() => {
core.setOutput("Component.yaml validation", "Successful");
})
.catch((err) => {
console.log("Component.yaml validation failed:", err.errors);
console.log(constructValidationErrorMessage(err.errors));
core.setFailed("Component.yaml validation failed", err.errors);
});
}
} catch (error) {
console.log("Failed to validate component.yaml:", error.message);
console.log("Failed to validate component.yaml: ", error.message);
core.setFailed("Failed to validate component.yaml", error.message);
}
24 changes: 12 additions & 12 deletions schemas.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ yup.addMethod(yup.array, "checkEndpointNameUniqueness", function () {
return true;
});
return (
isUnique || new yup.ValidationError("Endpoint names must be unique")
isUnique || new yup.ValidationError("Endpoint names must be unique.")
);
},
});
Expand All @@ -49,7 +49,7 @@ yup.addMethod(yup.object, "basePathRequired", function () {
const { type } = testCtx.parent;
if (BASE_PATH_REQUIRED_TYPES.includes(type) && !value.basePath) {
return new yup.ValidationError(
"Base path is required for REST, GraphQL, and WS endpoints"
"Base path is required for REST, GraphQL, and WS endpoints."
);
}
return true;
Expand All @@ -67,7 +67,7 @@ yup.addMethod(yup.string, "schemaFileExists", function (srcDir) {
return (
hasFile ||
new yup.ValidationError(
`Schema file does not exist at the given path ${value}`
`Schema file does not exist at the given path ${value}.`
)
);
} catch (error) {
Expand All @@ -85,11 +85,11 @@ const serviceSchema = yup
basePath: yup.string().matches(/^\/[a-zA-Z0-9\/-]*$/, "Invalid base path"),
port: yup
.number()
.required("Missing port number")
.moreThan(1000, "Port number must be greater than 1000")
.lessThan(65535, "Port number must be less than 65535"),
.required("Missing port number.")
.moreThan(1000, "Port number must be greater than 1000.")
.lessThan(65535, "Port number must be less than 65535."),
})
.required("service definition is required")
.required("service definition is required.")
.basePathRequired();
// endpointSchema - Schema for endpoint definition
const endpointSchema = (srcDir) =>
Expand All @@ -99,20 +99,20 @@ const endpointSchema = (srcDir) =>
yup.object().shape({
name: yup
.string()
.required("Endpoint name is required")
.max(50, "Endpoint name must be less than 50 characters")
.required("Endpoint name is required.")
.max(50, "Endpoint name must be less than 50 characters.")
.matches(
/^[a-z0-9-]+$/,
"Endpoint name must only contain lowercase letters, digits, and hyphens"
"Endpoint name must only contain lowercase letters, digits, and hyphens."
),
displayName: yup
.string()
.required()
.max(50, "Display name must be less than 50 characters"),
.max(50, "Display name must be less than 50 characters."),
service: serviceSchema,
type: yup
.string()
.required("Missing endpoint type")
.required("Missing endpoint type.")
.oneOf(ALLOWED_TYPES),
networkVisibilities: yup
.array()
Expand Down

0 comments on commit b592154

Please sign in to comment.