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

WIP: Implement schema validation in Collector #232

Closed
wants to merge 1 commit into from

Conversation

Bespaliy
Copy link
Member

  • tests and linter show no problems (npm t)
  • tests are added/updated for bug fixes and new features
  • code is properly formatted (npm run fmt)
  • description of changes is added in CHANGELOG.md
  • update .d.ts typings

@Bespaliy Bespaliy changed the title Implement schema validation in Collector https://github.com/metarhia/metautil/issues/230 Implement schema validation in Collector Dec 12, 2023
Copy link
Member

@tshemsedinov tshemsedinov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Max line length: 80

this.keys = keys;
if (exact === false) this.exact = false;
if (typeof timeout === 'number') this.#timeout(timeout);
if (schema) this.schema = schema;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (schema) this.schema = schema;
this.schema = schema;

Comment on lines +35 to +37
if (!this.schema) return { valid: true, errors: [] };

return this.schema.check(data);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (!this.schema) return { valid: true, errors: [] };
return this.schema.check(data);
if (!this.schema) return { valid: true, errors: [] };
return this.schema.check(data);

@@ -48,6 +48,7 @@
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-import": "^2.29.0",
"eslint-plugin-prettier": "^5.0.0",
"metaschema": "^2.1.5",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"metaschema": "^2.1.5",

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we include a type schema in the 'd.ts' file, something like this:

interface ValidationResult {
  valid: boolean;
  errors: string[];
}

export class Schema {
  check(value: any): ValidationResult;
}

or is there another approach?

Copy link
Member Author

@Bespaliy Bespaliy Dec 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For testing purposes, we can create a mock Schema class that imitates the functionality of the 'check' method based on the test requirements.

profile2.set('age', 'string');

const expectedResult2 = new Error(
'Invalid keys type: Field "id" not of expected type: number; Field "profile.firstname" not of expected type: string; Field "profile.age" not of expected type: number',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Max line: 80

);

try {
await ac1;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In all tests where you should catch an error, you should also throw an extra error if that test passed without the expected error. Otherwise, you simply won't notice that the test went wrong(after npm run test you will see simply one test less, that is, all test will go successfully without errors). It was recently added in all our tests.

So after all await in try you should add this line:
test.error(new Error('Should not be executed'));

);

try {
await ac1;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
await ac1;
await ac1;
test.error(new Error('Should not be executed'));

'Invalid keys type: Field "fullName" is not expected',
);
try {
await ac2;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
await ac2;
await ac2;
test.error(new Error('Should not be executed'));

);

try {
await ac2;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
await ac2;
await ac2;
test.error(new Error('Should not be executed'));

@tshemsedinov
Copy link
Member

I believe that Collector should not know about schemas, it may depend on abstract interface (function validate). @Bespaliy

@tshemsedinov tshemsedinov changed the title Implement schema validation in Collector WIP: Implement schema validation in Collector Feb 10, 2024
@tshemsedinov
Copy link
Member

Closed in favour of #248

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants