Skip to content

Commit

Permalink
Merge pull request #9 from Saeris/fix/update-getValidEnumValues
Browse files Browse the repository at this point in the history
Fix broken `#getValidEnumValues()` method implementation
  • Loading branch information
Saeris committed Nov 28, 2023
2 parents 5b303db + ff60f6c commit 5be15c5
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 11 deletions.
5 changes: 5 additions & 0 deletions .changeset/fifty-spies-retire.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"valimock": patch
---

Fix broken `#getValidEnumValues()` method
15 changes: 9 additions & 6 deletions src/Valimock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,12 +182,15 @@ export class Valimock {
};

#getValidEnumValues = <T extends v.Enum>(obj: T): Array<number | string> =>

Check warning on line 184 in src/Valimock.ts

View workflow job for this annotation

GitHub Actions / Lint, Test, and Typecheck (18)

Expected 'this' to be used by class private method #getValidEnumValues

Check warning on line 184 in src/Valimock.ts

View workflow job for this annotation

GitHub Actions / Lint, Test, and Typecheck (20)

Expected 'this' to be used by class private method #getValidEnumValues
Object.entries(obj).reduce<Array<number | string>>((arr, [key, value]) => {
if (typeof obj[obj[key]] === `number`) {
arr.push(value);
}
return arr;
}, []);
Object.values(
Object.entries(obj).reduce(
(hash, [key]) =>
typeof obj[obj[key]] === `number`
? hash
: Object.assign(hash, { [key]: obj[key] }),
{}
)
);

#findMatchingFaker = (keyName?: string): FakerFunction | undefined => {
if (typeof keyName === `undefined`) return;
Expand Down
39 changes: 34 additions & 5 deletions src/__tests__/mockEnum.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
/* eslint-disable @typescript-eslint/prefer-enum-initializers */
/* eslint-disable @typescript-eslint/prefer-literal-enum-member */
/* eslint-disable no-bitwise */
import { describe, expect, it } from "vitest";
import { parse, parseAsync, enum_, enumAsync } from "valibot";
import { Valimock } from "../Valimock.js";
Expand All @@ -12,12 +15,38 @@ describe(`mockEnum`, () => {
REJECTED = 3
}

it.each([enum_(States)])(`should generate valid mock data (%#)`, (schema) => {
const result = mockSchema(schema);
expect(parse(schema, result)).toStrictEqual(result);
});
enum Flags {
JOIN = 1 << 0,
SPECTATE = 1 << 1,
PLAY = 1 << 2
}

enum Features {
FOO = `FOO`,
BAR = `BAR`,
BAZ = `BAZ`
}

enum Status {
RED,
YELLOW,
GREEN
}

it.each([enum_(States), enum_(Flags), enum_(Features), enum_(Status)])(
`should generate valid mock data (%#)`,
(schema) => {
const result = mockSchema(schema);
expect(parse(schema, result)).toStrictEqual(result);
}
);

it.each([enumAsync(States)])(
it.each([
enumAsync(States),
enumAsync(Flags),
enumAsync(Features),
enumAsync(Status)
])(
`should generate valid mock data with async validation (%#)`,
async (schema) => {
const result = mockSchema(schema);
Expand Down

0 comments on commit 5be15c5

Please sign in to comment.