Skip to content

Commit

Permalink
Update Valibot min version, Fix intersect schema tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Saeris committed Mar 27, 2024
1 parent f372e8b commit 60b9bed
Show file tree
Hide file tree
Showing 7 changed files with 1,293 additions and 1,158 deletions.
5 changes: 5 additions & 0 deletions .changeset/fifty-walls-buy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"valimock": patch
---

Update Valibot min version, Fix tests for intersect schema
672 changes: 336 additions & 336 deletions .yarn/releases/yarn-4.0.2.cjs → .yarn/releases/yarn-4.1.1.cjs

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion .yarnrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ enableGlobalCache: false

nodeLinker: node-modules

yarnPath: .yarn/releases/yarn-4.0.2.cjs
yarnPath: .yarn/releases/yarn-4.1.1.cjs
31 changes: 17 additions & 14 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"engines": {
"node": ">=18.x"
},
"packageManager": "yarn@4.0.2",
"packageManager": "yarn@4.1.1",
"type": "module",
"main": "./dist/Valimock.js",
"module": "./dist/Valimock.js",
Expand Down Expand Up @@ -59,25 +59,28 @@
},
"peerDependencies": {
"@faker-js/faker": ">= 8.0.0",
"valibot": ">= 0.21.0"
"valibot": ">= 0.30.0"
},
"devDependencies": {
"@changesets/changelog-github": "^0.5.0",
"@changesets/cli": "^2.27.1",
"@faker-js/faker": "^8.3.1",
"@faker-js/faker": "^8.4.1",
"@saeris/eslint-config": "^2.6.0",
"@types/eslint": "^8.56.1",
"@types/node": "20.10.6",
"@vitest/coverage-v8": "^1.1.1",
"eslint": "^8.56.0",
"@types/eslint": "^8.56.6",
"@types/node": "20.11.30",
"@typescript-eslint/eslint-plugin": "^7.4.0",
"@typescript-eslint/parser": "^7.4.0",
"@vitest/coverage-v8": "^1.4.0",
"eslint": "^8.57.0",
"eslint-import-resolver-typescript": "^3.6.1",
"eslint-plugin-vitest": "^0.3.20",
"prettier": "^3.1.1",
"tsup": "^8.0.1",
"tsx": "^4.7.0",
"typescript": "5.3.3",
"valibot": "^0.25.0",
"vitest": "^1.1.1"
"eslint-plugin-import": "^2.29.1",
"eslint-plugin-vitest": "^0.4.0",
"prettier": "^3.2.5",
"tsup": "^8.0.2",
"tsx": "^4.7.1",
"typescript": "5.4.3",
"valibot": "^0.30.0",
"vitest": "^1.4.0"
},
"eslintConfig": {
"root": true,
Expand Down
53 changes: 26 additions & 27 deletions src/Valimock.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
/* eslint-disable @typescript-eslint/class-methods-use-this */
/* eslint-disable no-undefined */
/* eslint-disable @typescript-eslint/unbound-method */
import { faker as defaultFaker, type Faker } from "@faker-js/faker";
import RandExp from "randexp";
import type * as v from "valibot";
import * as v from "valibot";

export class MockError extends Error {
constructor(public typeName?: string) {
Expand Down Expand Up @@ -76,15 +78,12 @@ export interface ValimockOptions {
export class Valimock {
options: ValimockOptions = {
faker: defaultFaker,
// eslint-disable-next-line no-undefined
seed: undefined,
throwOnUnknownType: false,
// eslint-disable-next-line no-undefined
stringMap: undefined,
recordKeysLength: 1,
mapEntriesLength: 1,
customMocks: {},
// eslint-disable-next-line no-undefined
mockeryMapper: (
keyName: string | undefined,
fakerInstance: Faker
Expand Down Expand Up @@ -245,29 +244,32 @@ export class Valimock {
}
};

mock = <T extends { type?: string } & (v.BaseSchema | v.BaseSchemaAsync)>(
mock = <T extends v.BaseSchema | v.BaseSchemaAsync>(
schema: T
): v.Output<typeof schema> => this.#mock(schema);

#mock = <T extends { type?: string } & (v.BaseSchema | v.BaseSchemaAsync)>(
#mock = <T extends v.BaseSchema | v.BaseSchemaAsync>(
schema: T,
keyName?: string
): v.Output<typeof schema> => {
try {
if (this.options.seed) this.options.faker.seed(this.options.seed);
if (`type` in schema && typeof schema.type === `string`) {
if (Object.keys(this.#schemas).includes(schema.type)) {
if (schema.type === `string`) {
return this.#mockString(
schema as v.StringSchema | v.StringSchemaAsync,
keyName
);
}
return this.#schemas[schema.type](schema);
}
if (Object.keys(this.options.customMocks).includes(schema.type)) {
return this.options.customMocks[schema.type](schema, this.options);
}
if (
v.isOfType<
`string`,
| v.BaseSchema
| v.BaseSchemaAsync
| v.StringSchema
| v.StringSchemaAsync
>(`string`, schema)
) {
return this.#mockString(schema, keyName);
}
if (Object.keys(this.#schemas).includes(schema.type)) {
return this.#schemas[schema.type](schema);
}
if (Object.keys(this.options.customMocks).includes(schema.type)) {
return this.options.customMocks[schema.type](schema, this.options);
}
if (this.options.throwOnUnknownType) {
throw new MockError(schema.type);
Expand Down Expand Up @@ -362,6 +364,7 @@ export class Valimock {
): v.Output<typeof schema> =>
schema.options.reduce(
(hash, entry) => Object.assign(hash, this.#mock(entry)),
// eslint-disable-next-line @typescript-eslint/prefer-reduce-type-parameter
{} as v.BaseSchema
) as v.Output<typeof schema>;

Expand Down Expand Up @@ -417,7 +420,6 @@ export class Valimock {
undefined
]) ??
schema.default ??
// eslint-disable-next-line no-undefined
this.options.faker.helpers.arrayElement([null, undefined]);

#mockNull = (
Expand Down Expand Up @@ -481,8 +483,8 @@ export class Valimock {

#mockRecursive = (
schema:
| v.RecursiveSchema<() => v.BaseSchema>
| v.RecursiveSchemaAsync<() => v.BaseSchema | v.BaseSchemaAsync>
| v.LazySchema<() => v.BaseSchema>
| v.LazySchemaAsync<() => v.BaseSchema | v.BaseSchemaAsync>
): v.Output<typeof schema> => this.#mock(schema.getter());

#mockSet = (
Expand Down Expand Up @@ -540,7 +542,7 @@ export class Valimock {
const regexCheck = schema.pipe?.find(
(check) =>
(`requirement` in check && check.requirement instanceof RegExp) ||
(`type` in check && check.type === `regex`)
check.type === `regex`
);
if (
regexCheck &&
Expand Down Expand Up @@ -574,7 +576,6 @@ export class Valimock {
genKey.toLowerCase() === lowerCaseKeyName ||
schema.pipe?.find(
(item) =>
`type` in item &&
typeof item.type === `string` &&
item.type.toUpperCase() === genKey.toUpperCase()
)
Expand Down Expand Up @@ -614,9 +615,7 @@ export class Valimock {

#mockUndefined = (
schema: v.UndefinedSchema | v.UndefinedSchemaAsync
): v.Output<typeof schema> =>
// eslint-disable-next-line no-undefined
undefined;
): v.Output<typeof schema> => undefined;

#schemas = {
array: this.#mockArray,
Expand Down
28 changes: 18 additions & 10 deletions src/__tests__/mockIntersect.spec.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { describe, expect, it } from "vitest";
import {
intersect,
//intersectAsync,
intersectAsync,
object,
//objectAsync,
objectAsync,
string,
email,
maxLength,
minLength,
parse
parse,
parseAsync
} from "valibot";
import { Valimock } from "../Valimock.js";

Expand All @@ -23,8 +24,13 @@ describe(`mockIntersect`, () => {
object({
email: string([email()])
})
]) //,
/* export broken on main
])
])(`should generate valid mock data (%#)`, (schema) => {
const result = mockSchema(schema);
expect(parse(schema, result)).toStrictEqual(result);
});

it.each([
intersectAsync([
object({
name: string([minLength(2), maxLength(12)])
Expand All @@ -33,9 +39,11 @@ describe(`mockIntersect`, () => {
email: string([email()])
})
])
*/
])(`should generate valid mock data (%#)`, (schema) => {
const result = mockSchema(schema);
expect(parse(schema, result)).toStrictEqual(result);
});
])(
`should generate valid mock data with async validation (%#)`,
async (schema) => {
const result = mockSchema(schema);
await expect(parseAsync(schema, result)).resolves.toStrictEqual(result);
}
);
});
Loading

0 comments on commit 60b9bed

Please sign in to comment.