Skip to content

Commit

Permalink
fix: incorrect validation of enum keys instead of values (fixes #4)
Browse files Browse the repository at this point in the history
Fixes: #4
  • Loading branch information
mhweiner committed Jul 9, 2024
1 parent 9dbce16 commit 4fa8dc0
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 8 deletions.
2 changes: 0 additions & 2 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ jobs:
path: node_modules
key: ${{runner.os}}-node-${{hashFiles('package-lock.json')}}
- run: npm ci
env:
NODE_AUTH_TOKEN: ${{secrets.GITHUB_TOKEN}}
- run: npm run lint
- run: npm test
- run: npx autorel@^2
Expand Down
6 changes: 3 additions & 3 deletions src/predicates/enumValue.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import {ValidationError} from '..';
import {enumValue} from './enumValue';

enum TestEnum {
foo = 'foo',
bar = 'bar',
baz = 'baz'
Foo = 'foo',
Bar = 'bar',
Baz = 'baz'
}
const expectedErr = new ValidationError({
root: 'must be a valid enum value',
Expand Down
6 changes: 3 additions & 3 deletions src/predicates/enumValue.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import {Pred, ValidationError} from '..';

export function enumValue<T extends Record<string, string>>(enumObj: T): Pred<keyof T> {
export function enumValue<T extends Record<string, string>>(enumObj: T): Pred<T[keyof T]> {

return (value: unknown): value is keyof T => {
return (value: unknown): value is T[keyof T] => {

if (typeof value !== 'string' || !(value in enumObj)) {
if (typeof value !== 'string' || !Object.values(enumObj).includes(value as T[keyof T])) {

throw new ValidationError({root: 'must be a valid enum value'});

Expand Down

0 comments on commit 4fa8dc0

Please sign in to comment.