Skip to content

Commit

Permalink
moar tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcisbee committed Nov 13, 2020
1 parent aebe53e commit b4be6d9
Show file tree
Hide file tree
Showing 7 changed files with 491 additions and 4 deletions.
40 changes: 36 additions & 4 deletions src/validate/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@ const validateAnyOf = require("./anyOf");
*/
// @ts-ignore
function validateSchema(json, position, currentSchema = this.schema) {
if (
typeof currentSchema === "number" ||
typeof currentSchema === "string" ||
!currentSchema
) {
throw new Error("Invalid schema");
}

if (currentSchema.type === "array") {
if (!Array.isArray(json)) {
this.error('Incorrect type. Expected "array".', "type", position);
Expand Down Expand Up @@ -83,21 +91,41 @@ function validateSchema(json, position, currentSchema = this.schema) {
return;
}

if (currentSchema.type === "null") {
if (json !== null) {
this.error(`"${json}" should be null`, "type", position);
return;
}

return;
}

if (Array.isArray(currentSchema.type)) {
for (const typeSchema of currentSchema.type) {
let errorCount = 0;
for (const type of currentSchema.type) {
try {
validateSchema.call(
{...this, shallow: true},
json,
position,
typeSchema,
{
type,
},
);
return;
} catch {
// Ignore this error
} catch (e) {
errorCount++;
}
}

if (errorCount >= currentSchema.type.length) {
this.error(
`"${json}" should be ${currentSchema.type.join(" or ")}`,
"type",
position,
);
}

return;
}

Expand All @@ -119,6 +147,10 @@ function validateSchema(json, position, currentSchema = this.schema) {
return;
}

if (Object.keys(currentSchema).length === 0) {
return;
}

throw new Error(
`Unhandled schema "${JSON.stringify(currentSchema)}" with value "${json}"`,
);
Expand Down
65 changes: 65 additions & 0 deletions tests/empty.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
const {test} = require("uvu");
const assert = require("uvu/assert");

const output = require("../src/output");

const schema = {};
const userConfig = {};

test("passes with \"asd\"", () => {
const errors = output("\"asd\"", schema, userConfig);

assert.equal(errors, []);
});

test("passes with null", () => {
const errors = output("null", schema, userConfig);

assert.equal(errors, []);
});

test("passes with 0", () => {
const errors = output("0", schema, userConfig);

assert.equal(errors, []);
});

test("passes with 123", () => {
const errors = output("123", schema, userConfig);

assert.equal(errors, []);
});

test("passes with false", () => {
const errors = output("false", schema, userConfig);

assert.equal(errors, []);
});

test("passes with true", () => {
const errors = output("true", schema, userConfig);

assert.equal(errors, []);
});

test("passes with []", () => {
const errors = output("[]", schema, userConfig);

assert.equal(errors, []);
});

test("passes with {}", () => {
const errors = output("{}", schema, userConfig);

assert.equal(errors, []);
});

test("passes with larger object", () => {
const input = { "an": ["arbitrarily", "nested"], "data": "structure" };

const errors = output(JSON.stringify(input), schema, userConfig);

assert.equal(errors, []);
});

test.run();
83 changes: 83 additions & 0 deletions tests/false.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
const {test} = require("uvu");
const assert = require("uvu/assert");

const output = require("../src/output");

const schema = false;
const userConfig = {};

test("throws error with \"asd\"", () => {
const errors = output("\"asd\"", schema, userConfig);

assert.equal(errors, [
new Error('Invalid schema')
]);
});

test("throws error with null", () => {
const errors = output("null", schema, userConfig);

assert.equal(errors, [
new Error('Invalid schema')
]);
});

test("throws error with 0", () => {
const errors = output("0", schema, userConfig);

assert.equal(errors, [
new Error('Invalid schema')
]);
});

test("throws error with 123", () => {
const errors = output("123", schema, userConfig);

assert.equal(errors, [
new Error('Invalid schema')
]);
});

test("throws error with false", () => {
const errors = output("false", schema, userConfig);

assert.equal(errors, [
new Error('Invalid schema')
]);
});

test("throws error with true", () => {
const errors = output("true", schema, userConfig);

assert.equal(errors, [
new Error('Invalid schema')
]);
});

test("throws error with []", () => {
const errors = output("[]", schema, userConfig);

assert.equal(errors, [
new Error('Invalid schema')
]);
});

test("throws error with {}", () => {
const errors = output("{}", schema, userConfig);

assert.equal(errors, [
new Error('Invalid schema')
]);
});

test("throws error with larger object", () => {
const input = { "an": ["arbitrarily", "nested"], "data": "structure" };

const errors = output(JSON.stringify(input), schema, userConfig);

assert.equal(errors, [
new Error('Invalid schema')
]);
});

test.run();
69 changes: 69 additions & 0 deletions tests/multiple-types.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
const {test} = require("uvu");
const assert = require("uvu/assert");

const ValidationError = require("../src/diagnostics/error");
const output = require("../src/output");

const schema = {type: ["number", "string"]};
const userConfig = {};

test("passes with 0", () => {
const errors = output("0", schema, userConfig);
const expectation = [];

assert.equal(errors, expectation);
});

test("passes with \"asd\"", () => {
const errors = output("\"asd\"", schema, userConfig);
const expectation = [];

assert.equal(errors, expectation);
});

test("throws error with null", () => {
const errors = output("null", schema, userConfig);
const expectation = [
new ValidationError('"null" should be number or string', "type")
];

assert.equal(errors, expectation);
});

test("throws error with false", () => {
const errors = output("false", schema, userConfig);
const expectation = [
new ValidationError('"false" should be number or string', "type")
];

assert.equal(errors, expectation);
});

test("throws error with true", () => {
const errors = output("true", schema, userConfig);
const expectation = [
new ValidationError('"true" should be number or string', "type")
];

assert.equal(errors, expectation);
});

test("throws error with []", () => {
const errors = output("[]", schema, userConfig);
const expectation = [
new ValidationError('"" should be number or string', "type")
];

assert.equal(errors, expectation);
});

test("throws error with {}", () => {
const errors = output("{}", schema, userConfig);
const expectation = [
new ValidationError('"[object Object]" should be number or string', "type")
];

assert.equal(errors, expectation);
});

test.run();
80 changes: 80 additions & 0 deletions tests/null.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
const {test} = require("uvu");
const assert = require("uvu/assert");

const ValidationError = require("../src/diagnostics/error");
const output = require("../src/output");

const schema = {type: "null"};
const userConfig = {};

test("passes with null", () => {
const errors = output("null", schema, userConfig);
const expectation = [];

assert.equal(errors, expectation);
});

test("throws error with 0", () => {
const errors = output("0", schema, userConfig);
const expectation = [
new ValidationError('"0" should be null', "type")
];

assert.equal(errors, expectation);
});

test("throws error with 123", () => {
const errors = output("123", schema, userConfig);
const expectation = [
new ValidationError('"123" should be null', "type")
];

assert.equal(errors, expectation);
});

test("throws error with \"asd\"", () => {
const errors = output("\"asd\"", schema, userConfig);
const expectation = [
new ValidationError('"asd" should be null', "type")
];

assert.equal(errors, expectation);
});

test("throws error with false", () => {
const errors = output("false", schema, userConfig);
const expectation = [
new ValidationError('"false" should be null', "type")
];

assert.equal(errors, expectation);
});

test("throws error with true", () => {
const errors = output("true", schema, userConfig);
const expectation = [
new ValidationError('"true" should be null', "type")
];

assert.equal(errors, expectation);
});

test("throws error with []", () => {
const errors = output("[]", schema, userConfig);
const expectation = [
new ValidationError('"" should be null', "type")
];

assert.equal(errors, expectation);
});

test("throws error with {}", () => {
const errors = output("{}", schema, userConfig);
const expectation = [
new ValidationError('"[object Object]" should be null', "type")
];

assert.equal(errors, expectation);
});

test.run();
Loading

0 comments on commit b4be6d9

Please sign in to comment.