Skip to content

Commit

Permalink
feat: add math validators
Browse files Browse the repository at this point in the history
  • Loading branch information
bondarenkosa authored Apr 20, 2023
1 parent b0667eb commit f47f2ca
Show file tree
Hide file tree
Showing 13 changed files with 697 additions and 0 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ yarn add @byndyusoft/class-validator-extended class-validator class-transformer

- [AtLeastOneDefined](./src/decorators/validators/atLeastOneDefined.ts)
- [IsNullable](./src/decorators/validators/isNullable.ts)
- [LessThan](./src/decorators/validators/math/lessThan.ts)
- [LessThanOrEqualTo](./src/decorators/validators/math/lessThanOrEqualTo.ts)
- [GreaterThan](./src/decorators/validators/math/greaterThan.ts)
- [GreaterThanOrEqualTo](./src/decorators/validators/math/greaterThanOrEqualTo.ts)

#### class-transformer

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
}
},
"dependencies": {
"decimal.js": "^10.4.3",
"lodash": "^4.17.21",
"tslib": "^2.3.1",
"yn": "^4.0.0"
Expand Down
112 changes: 112 additions & 0 deletions src/decorators/validators/__tests__/math/greaterThan.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/*
* Copyright 2023 Byndyusoft
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { validate, ValidationError } from "class-validator";

import { GreaterThan } from "~/src";

interface IBoxProperties {
readonly height: string | number;
readonly width: string | number;
}

class Box {
@GreaterThan("100")
public readonly height!: string | number;

@GreaterThan(100)
public readonly width!: string | number;

public constructor({ height, width }: IBoxProperties) {
this.height = height;
this.width = width;
}
}

describe("decorators/validators/GreaterThan", () => {
describe("when values are valid", () => {
const cases: Array<{
description: string;
height: string | number;
width: string | number;
}> = [
{
description: "for number",
height: 100.1,
width: 100.1,
},
{
description: "for string number",
height: "100.1",
width: "100.1",
},
];

it.each(cases)(
"does not have validation errors $description",
async ({ height, width }) => {
const box = new Box({ height, width });

const validationErrors = await validate(box);

expect(validationErrors).toHaveLength(0);
},
);
});

describe("when values are invalid", () => {
const cases: Array<{
description: string;
height: string | number;
width: string | number;
}> = [
{
description: "for number",
height: 100,
width: -100.1,
},
{
description: "for string number",
height: "100",
width: "-100.1",
},
{
description: "for wrong types",
height: "abc",
width: "00ff",
},
];

it.each(cases)(
"returns validation errors $description",
async ({ height, width }) => {
const box = new Box({ height, width });

const validationErrors = await validate(box);

expect(validationErrors).toBeArrayOfSize(2);

expect(validationErrors).toSatisfyAll((error: ValidationError) => {
if (!error.constraints) {
return false;
}

return "greaterThan" in error.constraints;
});
},
);
});
});
112 changes: 112 additions & 0 deletions src/decorators/validators/__tests__/math/greaterThanOrEqualTo.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/*
* Copyright 2023 Byndyusoft
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { validate, ValidationError } from "class-validator";

import { GreaterThanOrEqualTo } from "~/src";

interface IBoxProperties {
readonly height: string | number;
readonly width: string | number;
}

class Box {
@GreaterThanOrEqualTo("100")
public readonly height!: string | number;

@GreaterThanOrEqualTo(100)
public readonly width!: string | number;

public constructor({ height, width }: IBoxProperties) {
this.height = height;
this.width = width;
}
}

describe("decorators/validators/GreaterThanOrEqualTo", () => {
describe("when values are valid", () => {
const cases: Array<{
description: string;
height: string | number;
width: string | number;
}> = [
{
description: "for number",
height: 100.1,
width: 100.1,
},
{
description: "for string number",
height: "100.1",
width: "100.1",
},
];

it.each(cases)(
"does not have validation errors $description",
async ({ height, width }) => {
const box = new Box({ height, width });

const validationErrors = await validate(box);

expect(validationErrors).toHaveLength(0);
},
);
});

describe("when values are invalid", () => {
const cases: Array<{
description: string;
height: string | number;
width: string | number;
}> = [
{
description: "for number",
height: 99.9,
width: -99.9,
},
{
description: "for string number",
height: "99.9",
width: "-99.9",
},
{
description: "for wrong types",
height: "abc",
width: "00ff",
},
];

it.each(cases)(
"returns validation errors $description",
async ({ height, width }) => {
const box = new Box({ height, width });

const validationErrors = await validate(box);

expect(validationErrors).toBeArrayOfSize(2);

expect(validationErrors).toSatisfyAll((error: ValidationError) => {
if (!error.constraints) {
return false;
}

return "greaterThanOrEqualTo" in error.constraints;
});
},
);
});
});
112 changes: 112 additions & 0 deletions src/decorators/validators/__tests__/math/lessThan.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/*
* Copyright 2023 Byndyusoft
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { validate, ValidationError } from "class-validator";

import { LessThan } from "~/src";

interface IBoxProperties {
readonly height: string | number;
readonly width: string | number;
}

class Box {
@LessThan("100")
public readonly height!: string | number;

@LessThan(100)
public readonly width!: string | number;

public constructor({ height, width }: IBoxProperties) {
this.height = height;
this.width = width;
}
}

describe("decorators/validators/LessThan", () => {
describe("when values are valid", () => {
const cases: Array<{
description: string;
height: string | number;
width: string | number;
}> = [
{
description: "for number",
height: -99.9,
width: 99.9,
},
{
description: "for string number",
height: "99.9",
width: "-99.9",
},
];

it.each(cases)(
"does not have validation errors $description",
async ({ height, width }) => {
const box = new Box({ height, width });

const validationErrors = await validate(box);

expect(validationErrors).toHaveLength(0);
},
);
});

describe("when values are invalid", () => {
const cases: Array<{
description: string;
height: string | number;
width: string | number;
}> = [
{
description: "for number",
height: 100,
width: 100.1,
},
{
description: "for string number",
height: "100",
width: "100.1",
},
{
description: "for wrong types",
height: "abc",
width: "00ff",
},
];

it.each(cases)(
"returns validation errors $description",
async ({ height, width }) => {
const box = new Box({ height, width });

const validationErrors = await validate(box);

expect(validationErrors).toBeArrayOfSize(2);

expect(validationErrors).toSatisfyAll((error: ValidationError) => {
if (!error.constraints) {
return false;
}

return "lessThan" in error.constraints;
});
},
);
});
});
Loading

0 comments on commit f47f2ca

Please sign in to comment.