generated from Byndyusoft/node-typescript-template
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add validator for string integer
- Loading branch information
1 parent
f47f2ca
commit b345a61
Showing
4 changed files
with
132 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
src/decorators/validators/__tests__/math/isIntString.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}); | ||
}, | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
); | ||
} |