Skip to content

Commit

Permalink
Adds if, then, else rule
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcisbee committed Oct 5, 2021
1 parent 8a4cee1 commit 6a9bf54
Show file tree
Hide file tree
Showing 16 changed files with 465 additions and 9 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "esjson",
"version": "1.0.0",
"version": "1.1.0",
"description": "JSON Schema validator (cli)",
"main": "src/index.js",
"bin": {
Expand Down
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ const defaultConfig = {
* @property {Record<string, any>} config
* @property {string} filePath
* @property {boolean=} shallow
* @property {boolean=} catch
*/

function validator() {
Expand Down
2 changes: 1 addition & 1 deletion src/report.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ function report(params) {
"\n" +
blue(`ℹ ${passed} validated`) +
"\n" +
bold(green("\u2714 No knowns problems!")) +
bold(green("\u2714 No known problems!")) +
"\n\n",
);
process.exit(0);
Expand Down
4 changes: 2 additions & 2 deletions src/validate.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ function validate(filePath, fileContents, schema, config, position = []) {
) {
const warning = new Warning(message, code, pos, undefined, lineNo);

if (this.shallow) {
if (this.shallow || this.catch) {
throw warning;
}

Expand All @@ -52,7 +52,7 @@ function validate(filePath, fileContents, schema, config, position = []) {

const error = new ValidationError(message, code, pos, undefined, lineNo);

if (this.shallow) {
if (this.shallow || this.catch) {
throw error;
}

Expand Down
4 changes: 2 additions & 2 deletions src/validate/all-of.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ function validateAllOf(value, currentSchema, position) {

for (const key in currentSchema) {
try {
// Run shallow validation
// Run full validation
this.validateSchema.call(
{...this, shallow: true},
{...this, catch: true},
value,
position,
currentSchema[key],
Expand Down
19 changes: 19 additions & 0 deletions src/validate/const.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* @this {import('src').Context}
* @param {*} value
* @param {*} currentSchema
* @param {(string | number)[]} position
*/
function validateConst(value, currentSchema, position) {
if (value === currentSchema) {
return true;
}

this.error(
`Value "${value}" not accepted. Expected value: ${currentSchema}`,
"const",
position,
);
}

module.exports = validateConst;
36 changes: 36 additions & 0 deletions src/validate/if-then-else.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* @this {import('src').Context}
* @param {*} value
* @param {*} currentSchema
* @param {(string | number)[]} position
*/
function validateIfThenElse(value, currentSchema, position) {
let errorDepth = position.length + 1;
let possibleError;

try {
// Run full validation
this.validateSchema.call(
{...this, catch: true},
value,
position,
currentSchema,
);

return;
} catch (e) {
if (e.position && e.position.length > errorDepth) {
possibleError = "if";
errorDepth = e.position.length;
}
}

// Guessing the error based on error depth
if (possibleError) {
return possibleError;
}

return true;
}

module.exports = validateIfThenElse;
25 changes: 25 additions & 0 deletions src/validate/not.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* @this {import('src').Context}
* @param {*} value
* @param {*} currentSchema
* @param {(string | number)[]} position
*/
function validateNot(value, currentSchema, position) {
try {
// Run full validation
this.validateSchema.call(
{...this, catch: true},
value,
position,
currentSchema,
);

this.error("Value not accepted", "not", position);

return;
} catch (e) {
return true;
}
}

module.exports = validateNot;
45 changes: 44 additions & 1 deletion src/validate/schema.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
const GenericError = require("../diagnostics/generic-error");

const validateEnum = require("./enum");
const validateAllOf = require("./all-of");
const validateAnyOf = require("./any-of");
const validateConst = require("./const");
const validateEnum = require("./enum");
const validateIfThenElse = require("./if-then-else");
const validateNot = require("./not");
const validateString = require("./type/string");
const validateObject = require("./type/object");
const validateNumber = require("./type/number");
Expand All @@ -28,6 +31,41 @@ function validateSchema(json, position, currentSchema = this.schema) {
throw new GenericError("Invalid schema");
}

if (currentSchema.if) {
const key = validateIfThenElse.call(
this,
json,
{
type: currentSchema.type,
...currentSchema.if,
},
position,
);

if (typeof key === "undefined") {
currentSchema = {
...currentSchema,
if: undefined,
then: undefined,
else: undefined,
...(currentSchema.then || {type: "object"}),
};
} else {
currentSchema = {
...currentSchema,
if: undefined,
then: undefined,
else: undefined,
...(currentSchema.else || {type: "object"}),
};
}
}

if (currentSchema.const) {
validateConst.call(this, json, currentSchema.const, position);
return;
}

if (currentSchema.type === "array") {
validateArray.call(this, json, currentSchema, position);
return;
Expand Down Expand Up @@ -68,6 +106,11 @@ function validateSchema(json, position, currentSchema = this.schema) {
return;
}

if (currentSchema.not) {
validateNot.call(this, json, currentSchema.not, position);
return;
}

if (!this.shallow && currentSchema.allOf) {
const key = validateAllOf.call(this, json, currentSchema.allOf, position);

Expand Down
5 changes: 5 additions & 0 deletions src/validate/type/boolean.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const validateEnum = require("../enum");
const validateNot = require("../not");

/**
* @this {import('src').Context}
Expand All @@ -15,6 +16,10 @@ function validateBoolean(value, currentSchema, position) {
if (currentSchema.enum) {
validateEnum.call(this, value, currentSchema.enum, position);
}

if (currentSchema.not) {
validateNot.call(this, value, currentSchema.not, position);
}
}

module.exports = validateBoolean;
5 changes: 5 additions & 0 deletions src/validate/type/null.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const validateEnum = require("../enum");
const validateNot = require("../not");

/**
* @this {import('src').Context}
Expand All @@ -15,6 +16,10 @@ function validateNull(value, currentSchema, position) {
if (currentSchema.enum) {
validateEnum.call(this, value, currentSchema.enum, position);
}

if (currentSchema.not) {
validateNot.call(this, value, currentSchema.not, position);
}
}

module.exports = validateNull;
5 changes: 5 additions & 0 deletions src/validate/type/number.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const validateEnum = require("../enum");
const validateNot = require("../not");

/**
* @this {import('src').Context}
Expand All @@ -15,6 +16,10 @@ function validateNumber(value, currentSchema, position) {
if (currentSchema.enum) {
validateEnum.call(this, value, currentSchema.enum, position);
}

if (currentSchema.not) {
validateNot.call(this, value, currentSchema.not, position);
}
}

module.exports = validateNumber;
5 changes: 5 additions & 0 deletions src/validate/type/string.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const validateEnum = require("../enum");
const validateNot = require("../not");

/**
* @this {import('src').Context}
Expand All @@ -15,6 +16,10 @@ function validateString(value, currentSchema, position) {
if (currentSchema.enum) {
validateEnum.call(this, value, currentSchema.enum, position);
}

if (currentSchema.not) {
validateNot.call(this, value, currentSchema.not, position);
}
}

module.exports = validateString;
Loading

0 comments on commit 6a9bf54

Please sign in to comment.