Skip to content

Commit

Permalink
Fix issue with linting and fix app.js
Browse files Browse the repository at this point in the history
  • Loading branch information
VirajP1002 committed Dec 6, 2023
1 parent 01b62d1 commit 899c750
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 33 deletions.
2 changes: 1 addition & 1 deletion .pylintrc
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ ignore=CVS
# ignore-list. The regex matches against paths and can be in Posix or Windows
# format. Because '\' represents the directory delimiter on Windows systems, it
# can't be used as an escape character.
ignore-paths=
ignore-paths=node_modules

# Files or directories matching the regular expression patterns are skipped.
# The regex matches against base names, not paths. The default value ignores
Expand Down
55 changes: 23 additions & 32 deletions ajv/app.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
import Ajv2020 from "ajv/dist/2020.js";
import fs from "fs";
import glob from "glob";
import { globSync } from "glob";
import express from "express";
import Debug from "debug";

const debug = Debug("ajv-schema-validator");

const app = express();

const ajValidator = new Ajv2020({
allErrors: false,
strict: true,
Expand All @@ -16,45 +13,39 @@ const ajValidator = new Ajv2020({
strictSchema: false, // this has been included to avoid unknown keyword errors ad strict mode is true
strictTuples: false, // this has been included due to https://github.com/ajv-validator/ajv/issues/1417
});

app.use(
express.json({
limit: "2Mb",
})
);

app.listen(5002, () => {
debug("Server running on port 5002");
});

// Export our app for testing purposes
export default app;

app.get("/status", (req, res, next) => {
return res.sendStatus(200);
});

glob("schemas/**/*.json", (er, schemas) => {
schemas.forEach((currentSchema) => {
if (!(currentSchema.valueOf() === "schemas/questionnaire_v1.json")) {
const data = fs.readFileSync(currentSchema); // eslint-disable-line security/detect-non-literal-fs-filename
ajValidator.addSchema(JSON.parse(data)).compile(true);
}
});
const baseSchema = fs.readFileSync("schemas/questionnaire_v1.json");
const validate = ajValidator.compile(JSON.parse(baseSchema));

app.post("/validate", (req, res, next) => {
debug("Validating questionnaire: " + req.body.title);
const valid = validate(req.body);
if (!valid) {
return res.json({
success: false,
errors: validate.errors.sort((errorA, errorB) => {
return errorA.instancePath.length - errorB.instancePath.length;
}),
});
}
return res.json({});
});
const schemas = globSync("schemas/**/*.json");
schemas.forEach((currentSchema) => {
if (!(currentSchema.valueOf() === "schemas/questionnaire_v1.json")) {
const data = fs.readFileSync(currentSchema); // eslint-disable-line security/detect-non-literal-fs-filename
debug("Adding schema: " + currentSchema);
ajValidator.addSchema(JSON.parse(data)).compile(true);
}
});
const baseSchema = fs.readFileSync("schemas/questionnaire_v1.json");
const validate = ajValidator.compile(JSON.parse(baseSchema));
app.post("/validate", (req, res, next) => {
debug("Validating questionnaire: " + req.body.title);
const valid = validate(req.body);
if (!valid) {
return res.json({
success: false,
errors: validate.errors.sort((errorA, errorB) => {
return errorA.instancePath.length - errorB.instancePath.length;
}),
});
}
return res.json({});
});

0 comments on commit 899c750

Please sign in to comment.