Skip to content

Commit

Permalink
feat: add validator for string integer
Browse files Browse the repository at this point in the history
  • Loading branch information
bondarenkosa authored Apr 24, 2023
1 parent f47f2ca commit b345a61
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ yarn add @byndyusoft/class-validator-extended class-validator class-transformer

- [AtLeastOneDefined](./src/decorators/validators/atLeastOneDefined.ts)
- [IsNullable](./src/decorators/validators/isNullable.ts)
- [IsIntString](./src/decorators/validators/math/isIntString.ts)
- [LessThan](./src/decorators/validators/math/lessThan.ts)
- [LessThanOrEqualTo](./src/decorators/validators/math/lessThanOrEqualTo.ts)
- [GreaterThan](./src/decorators/validators/math/greaterThan.ts)
Expand Down
81 changes: 81 additions & 0 deletions src/decorators/validators/__tests__/math/isIntString.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* 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 { IsIntString } from "~/src";

class Box {
@IsIntString()
public readonly height!: string;

public constructor(height: string) {
this.height = height;
}
}

describe("decorators/validators/IsIntString", () => {
describe("when values are valid", () => {
const height = "99";
const box = new Box(height);

it("does not have validation errors", async () => {
const validationErrors = await validate(box);

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

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

it.each(cases)(
"returns validation errors $description",
async ({ height }) => {
// @ts-expect-error for validation tests
const box = new Box(height);

const validationErrors = await validate(box);

expect(validationErrors).toBeArrayOfSize(1);

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

return "isIntString" in error.constraints;
});
},
);
});
});
1 change: 1 addition & 0 deletions src/decorators/validators/math/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@

export * from "./greaterThan";
export * from "./greaterThanOrEqualTo";
export * from "./isIntString";
export * from "./lessThan";
export * from "./lessThanOrEqualTo";
49 changes: 49 additions & 0 deletions src/decorators/validators/math/isIntString.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* 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 {
buildMessage,
isString,
ValidateBy,
ValidationOptions,
} from "class-validator";
import { Decimal } from "decimal.js";

const isIntString = (value: unknown): boolean => {
try {
return isString(value) && new Decimal(value).isInt();
} catch {
return false;
}
};

export function IsIntString(
validationOptions?: ValidationOptions,
): PropertyDecorator {
return ValidateBy(
{
name: "isIntString",
validator: {
validate: (value): boolean => isIntString(value),
defaultMessage: buildMessage(
(eachPrefix) => `${eachPrefix}$property must be a string integer`,
validationOptions,
),
},
},
validationOptions,
);
}

0 comments on commit b345a61

Please sign in to comment.