Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions lib/validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,15 +97,15 @@ Validator.prototype.compile = function(schema) {

const rules = this.compileSchemaType(schema);
this.cache.clear();
return function(value) {
return self.checkSchemaType(value, rules, undefined, null);
return function(value, path, parent) {
return self.checkSchemaType(value, rules, path, parent || null);
};
}

const rule = this.compileSchemaObject(schema);
this.cache.clear();
return function(value) {
return self.checkSchemaObject(value, rule, undefined, null);
return function(value, path, parent) {
return self.checkSchemaObject(value, rule, path, parent || null);
};
};

Expand Down
57 changes: 57 additions & 0 deletions test/validator.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,63 @@ describe("Test compile (integration test)", () => {

});

describe("Test check generator with custom path & parent", () => {

it("when schema is defined as an array, and custom path & parent are specified, they should be forwarded to validators", () => {
const v = new Validator();
const customValidator = jest.fn().mockReturnValue(true); // Will be called with (value, schema, path, parent)
v.add("customValidator", customValidator);

const validate = v.compile([{ type: "customValidator" }]);
const parent = {};
const res = validate({ customValue: 4711 }, "customPath", parent);

expect(res).toBe(true);
expect(customValidator.mock.calls[0][2]).toBe("customPath");
expect(customValidator.mock.calls[0][3]).toBe(parent);
});

it("when schema is defined as an array, path & parent should be set to default values in validators", () => {
const v = new Validator();
const customValidator = jest.fn().mockReturnValue(true); // Will be called with (value, schema, path, parent)
v.add("customValidator", customValidator);

const validate = v.compile([{ type: "customValidator" }]);
const res = validate({ customValue: 4711 });

expect(res).toBe(true);
expect(customValidator.mock.calls[0][2]).toBeUndefined();
expect(customValidator.mock.calls[0][3]).toBeNull();
});

it("when schema is defined as an object, and custom path is specified, it should be forwarded to validators", () => {
// Note: as the item we validate always must be an object, there is no use
// of specifying a custom parent, like for the schema-as-array above.
// The parent is currently used in the validator code (only forwarded to the generated
// function that validates all properties) and there is no way to test it.
const v = new Validator();
const customValidator = jest.fn().mockReturnValue(true); // Will be called with (value, schema, path, parent)
v.add("customValidator", customValidator);

const validate = v.compile({ customValue: { type: "customValidator" } });
const res = validate({ customValue: 4711 }, "customPath");

expect(res).toBe(true);
expect(customValidator.mock.calls[0][2]).toBe("customPath.customValue");
});

it("when schema is defined as an object, path should be set to default value in validators", () => {
const v = new Validator();
const customValidator = jest.fn().mockReturnValue(true); // Will be called with (value, schema, path, parent)
v.add("customValidator", customValidator);

const validate = v.compile({ customValue: { type: "customValidator" } });
const res = validate({ customValue: 4711 });

expect(res).toBe(true);
expect(customValidator.mock.calls[0][2]).toBe("customValue");
});
});
});

describe("Test nested schema", () => {
Expand Down