Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add validation service settings by provided schema ability. #1276

Closed
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
2 changes: 2 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { EventEmitter2 } from "eventemitter2";
import type { BinaryLike, CipherCCMTypes, CipherGCMTypes, CipherKey, CipherOCBTypes } from 'crypto'
import type { Worker } from "cluster";
import { ValidationSchema } from "fastest-validator";

declare namespace Moleculer {
/**
Expand Down Expand Up @@ -628,6 +629,7 @@ declare namespace Moleculer {
$dependencyTimeout?: number;
$shutdownTimeout?: number;
$secureSettings?: string[];
$validationSchema?: ValidationSchema | GenericObject;
[name: string]: any;
}

Expand Down
19 changes: 19 additions & 0 deletions src/service.js
Original file line number Diff line number Diff line change
Expand Up @@ -252,13 +252,32 @@ class Service {
return settings;
}

/**
* Validate service settings by provided validation schema.
*
* @param {Object} settings
* @memberof Service
*/
_validateSettings(settings) {
return this.broker.validator.validate(settings, settings.$validationSchema);
}

/**
* Initialize service. It called `created` handler in schema
*
* @private
* @memberof Service
*/
_init() {
if (this.settings && this.settings.$validationSchema) {
this.logger.debug("Service settings validation schema found. Try to use it...");
if (this.broker.validator) {
this._validateSettings(this.settings);
} else {
this.logger.warn("Validator not enabled. Skip service settings validation.");
}
}

this.logger.debug(`Service '${this.fullName}' is creating...`);
if (isFunction(this.schema.created)) {
this.schema.created.call(this);
Expand Down
69 changes: 69 additions & 0 deletions test/unit/service.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
const Service = require("../../src/service");
const Context = require("../../src/context");
const ServiceBroker = require("../../src/service-broker");
const { ValidationError } = require("../../src/errors");

describe("Test Service class", () => {
describe("Test constructor", () => {
Expand Down Expand Up @@ -489,6 +490,74 @@ describe("Test Service class", () => {
});
});

describe("Test service settings validation", () => {
const broker = new ServiceBroker({ logger: false });

const svc = new Service(broker);

jest.spyOn(broker.validator, "validate");

const $validationSchema = {
$$strict: false,
someSetting: {
props: { someString: { type: "string" } },
strict: true,
type: "object"
}
};

const validSettings = {
$validationSchema,
someSetting: {
someString: "someString"
}
};

const invalidSettings = {
$validationSchema,
someSetting: {
someAnotherString: "someString"
}
};

it("should validate service settings correctly", () => {
const validationResult = svc._validateSettings(validSettings);

expect(validationResult).toEqual(true);
});

it("should validate service settings correctly", () => {
const merged = svc.mergeSchemaSettings;
svc.parseServiceSchema({ settings: validSettings, merged, name: "posts" });

broker.validator.validate.mockClear();

svc._init();

expect(broker.validator.validate).toBeCalledTimes(1);
});

it("should throw validation error", () => {
const merged = svc.mergeSchemaSettings;

try {
svc.parseServiceSchema({ settings: invalidSettings, merged, name: "posts" });
} catch (error) {
expect(error).toBeInstanceOf(ValidationError);
}
});

it("should not validate service settings", () => {
broker.validator = null;

const merged = svc.mergeSchemaSettings;

expect(() => {
svc.parseServiceSchema({ settings: validSettings, merged, name: "posts" });
}).not.toThrow();
});
});

describe("Test _init", () => {
const broker = new ServiceBroker({ logger: false });

Expand Down
Loading