Skip to content
Merged

Dev #32

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
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
## Changelog

### [v5.19.3](https://github.com/panates/valgen/compare/v5.19.2...v5.19.3) -
### [v5.19.4](https://github.com/panates/valgen/compare/v5.19.3...v5.19.4) -

#### 🚀 New Features

- feat: Handle TypeScript enums in isEnum validator @Eray Hanoğlu

### [v5.19.3](https://github.com/panates/valgen/compare/v5.19.2...v5.19.3) - 19 January 2026

#### 🪲 Fixes

Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "valgen",
"description": "Fast runtime type validator, converter and io (encoding/decoding) library",
"version": "5.19.3",
"version": "5.19.4",
"author": "Panates",
"license": "MIT",
"private": true,
Expand Down
14 changes: 11 additions & 3 deletions src/rules/type-rules/is-enum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,24 @@ import {
* Validates if given value is one of enum values.
* @validator isEnum
*/
export function isEnum<T1, I1>(
values: I1 | I1[],
export function isEnum<T1>(
values: any,
options?: ValidationOptions & {
caseInSensitive?: boolean;
enumName?: string;
},
): Validator<T1, I1 | I1[]> {
): Validator<T1, any> {
const caseInSensitive = !!options?.caseInSensitive;
const enumName = options?.enumName;
// Prepare an object for fast lookup
if (values && typeof values === 'object' && !Array.isArray(values)) {
const keys = Object.keys(values).filter(k => !/^\d+$/.test(k));
values = keys.reduce((a, k) => {
if (values[k] != null) a.push(values[k]);
return a;
// v => !(typeof v === 'number' && !keys.includes(String(v))),
}, [] as any[]);
}
const valObj = (Array.isArray(values) ? values : [values]).reduce<any>(
(a, v) => {
if (typeof v === 'string' && caseInSensitive) a[v.toUpperCase()] = true;
Expand Down
13 changes: 13 additions & 0 deletions test/type-rules/is-enum.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,17 @@ describe('isEnum', () => {
);
expect(() => vg.isEnum(['a', 'b'])(null as any)).toThrow('must be one of');
});

it('should validate value using TypeScript enums', () => {
enum Enum1 {
a = 0,
b = 1,
}
enum Enum2 {
a = 'a',
b = 'b',
}
expect(vg.isEnum(Enum1)(0)).toStrictEqual(0);
expect(vg.isEnum(Enum2)('a')).toStrictEqual('a');
});
});