Skip to content

Commit

Permalink
fix: Alias mock() method, Add more usage details to README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
Saeris committed Oct 17, 2023
1 parent 982d523 commit 3826a88
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 18 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ yarn add -D valimock @faker-js/faker

## 馃敡 Usage

Import and optionally configure a new instance of the `Valimock` class, then pass along your `valibot` schema to `mock()`, that's it!

```ts
import { parse, array, union, string, url, number, maxValue } from "valibot";
import { Valimock } from "valimock";
Expand All @@ -36,6 +38,18 @@ describe(`example test`, () => {
});
```

> [!NOTE]
>
> For async schemas, you will need to use `parseAsync()`. Be aware that async schemas generate a `Promise` and may need to be `await`'ed depending on usage.
>
> Please see the [`__tests__`](./src/__tests__/) folder for more usage examples of different schema types.
> [!WARNING]
>
> At present, not all of `valibot`'s API is fully covered by `valimock`, however, any unimplemented schema type can be handled by a user-supplied map via the `customMocks` configuration option. The schema's `kind` proerty is used as the property key for this map.
>
> In the future a table will be provided with API coverage data.
## 馃摚 Acknowledgements

Valimock's implementation is based on [`@anatine/zod-mock`](https://github.com/anatine/zod-plugins/tree/main/packages/zod-mock)
Expand Down
44 changes: 26 additions & 18 deletions src/Valimock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,11 @@ export interface ValimockOptions {
* This is a mapping of field name to mock generator function.
* This mapping can be used to provide backup mock
* functions for Schema types not yet implemented.
*
* The functions in this map will only be used if this library
* is unable to find an appropriate mocking function to use.
*/
backupMocks: Record<
customMocks: Record<
string,
(
schema: v.BaseSchema | v.BaseSchemaAsync,
Expand Down Expand Up @@ -82,7 +83,7 @@ export class Valimock {
stringMap: undefined,
recordKeysLength: 1,
mapEntriesLength: 1,
backupMocks: {},
customMocks: {},
// eslint-disable-next-line no-undefined
mockeryMapper: (
keyName: string | undefined,
Expand Down Expand Up @@ -244,6 +245,10 @@ export class Valimock {
};

mock = <T extends v.BaseSchema | v.BaseSchemaAsync>(
schema: T
): v.Output<typeof schema> => this.#mock(schema);

#mock = <T extends v.BaseSchema | v.BaseSchemaAsync>(
schema: T,
keyName?: string
): v.Output<typeof schema> => {
Expand All @@ -258,8 +263,8 @@ export class Valimock {
}
return this.#schemas[schema.kind](schema);

Check failure on line 264 in src/Valimock.ts

View workflow job for this annotation

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

Property 'kind' does not exist on type 'BaseSchema | BaseSchemaAsync'.

Check failure on line 264 in src/Valimock.ts

View workflow job for this annotation

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

Property 'kind' does not exist on type 'BaseSchema | BaseSchemaAsync'.

Check failure on line 264 in src/Valimock.ts

View workflow job for this annotation

GitHub Actions / Release

Property 'kind' does not exist on type 'BaseSchema | BaseSchemaAsync'.
}
if (Object.keys(this.options.backupMocks).includes(schema.kind)) {
return this.options.backupMocks[schema.kind](schema);
if (Object.keys(this.options.customMocks).includes(schema.kind)) {

Check failure on line 266 in src/Valimock.ts

View workflow job for this annotation

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

Property 'kind' does not exist on type 'BaseSchema | BaseSchemaAsync'.

Check failure on line 266 in src/Valimock.ts

View workflow job for this annotation

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

Property 'kind' does not exist on type 'BaseSchema | BaseSchemaAsync'.

Check failure on line 266 in src/Valimock.ts

View workflow job for this annotation

GitHub Actions / Release

Property 'kind' does not exist on type 'BaseSchema | BaseSchemaAsync'.
return this.options.customMocks[schema.kind](schema, this.options);

Check failure on line 267 in src/Valimock.ts

View workflow job for this annotation

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

Property 'kind' does not exist on type 'BaseSchema | BaseSchemaAsync'.

Check failure on line 267 in src/Valimock.ts

View workflow job for this annotation

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

Property 'kind' does not exist on type 'BaseSchema | BaseSchemaAsync'.

Check failure on line 267 in src/Valimock.ts

View workflow job for this annotation

GitHub Actions / Release

Property 'kind' does not exist on type 'BaseSchema | BaseSchemaAsync'.
}
if (this.options.throwOnUnknownType) {
throw new MockError(schema.kind);

Check failure on line 270 in src/Valimock.ts

View workflow job for this annotation

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

Property 'kind' does not exist on type 'BaseSchema | BaseSchemaAsync'.

Check failure on line 270 in src/Valimock.ts

View workflow job for this annotation

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

Property 'kind' does not exist on type 'BaseSchema | BaseSchemaAsync'.

Check failure on line 270 in src/Valimock.ts

View workflow job for this annotation

GitHub Actions / Release

Property 'kind' does not exist on type 'BaseSchema | BaseSchemaAsync'.
Expand Down Expand Up @@ -293,7 +298,7 @@ export class Valimock {
? checks.length
: this.options.faker.number.int({ min, max })
},
() => this.mock(schema.array.item)
() => this.#mock(schema.array.item)
)
);
};
Expand Down Expand Up @@ -367,7 +372,7 @@ export class Valimock {
this.#wrapResult(
schema,
schema.intersection.reduce(
(hash, entry) => Object.assign(hash, this.mock(entry)),
(hash, entry) => Object.assign(hash, this.#mock(entry)),
{} as v.BaseSchema
)
) as v.Output<typeof schema>;
Expand All @@ -383,7 +388,7 @@ export class Valimock {
): v.Output<typeof schema> => {
const result = new Map<v.BaseSchema, v.BaseSchema>();
while (result.size < this.options.mapEntriesLength) {
result.set(this.mock(schema.map.key), this.mock(schema.map.value));
result.set(this.#mock(schema.map.key), this.#mock(schema.map.value));
}
return this.#wrapResult(schema, result);
};
Expand Down Expand Up @@ -413,14 +418,17 @@ export class Valimock {
| v.NonOptionalSchema<v.BaseSchema>
| v.NonOptionalSchemaAsync<v.BaseSchema>
): v.Output<typeof schema> =>
this.#wrapResult(schema, this.mock(schema.wrapped));
this.#wrapResult(schema, this.#mock(schema.wrapped));

#mockNullable = (
schema: v.NullableSchema<v.BaseSchema> | v.NullableSchemaAsync<v.BaseSchema>
): v.Output<typeof schema> =>
this.#wrapResult(
schema,
this.options.faker.helpers.arrayElement([this.mock(schema.wrapped), null])
this.options.faker.helpers.arrayElement([
this.#mock(schema.wrapped),
null
])
);

#mockNullish = (
Expand All @@ -429,7 +437,7 @@ export class Valimock {
this.#wrapResult(
schema,
this.options.faker.helpers.arrayElement([
this.mock(schema.wrapped),
this.#mock(schema.wrapped),
null,
// eslint-disable-next-line no-undefined
undefined
Expand Down Expand Up @@ -472,7 +480,7 @@ export class Valimock {
Object.entries(schema.object).reduce<Record<string, v.BaseSchema>>(
(hash, [key, value]) => ({
...hash,
[key]: this.mock<v.BaseSchema | v.BaseSchemaAsync>(value, key)
[key]: this.#mock<v.BaseSchema | v.BaseSchemaAsync>(value, key)
}),
{}
)
Expand All @@ -484,7 +492,7 @@ export class Valimock {
this.#wrapResult(
schema,
this.options.faker.helpers.arrayElement([
this.mock<v.BaseSchema>(schema.wrapped),
this.#mock<v.BaseSchema>(schema.wrapped),
// eslint-disable-next-line no-undefined
undefined
]) ?? schema.default
Expand All @@ -500,8 +508,8 @@ export class Valimock {
schema,
Object.fromEntries(
Array.from({ length: this.options.recordKeysLength }, () => [
this.mock(schema.record.key),
this.mock(schema.record.value)
this.#mock(schema.record.key),
this.#mock(schema.record.value)
])
) as v.Output<typeof schema>
);
Expand All @@ -511,7 +519,7 @@ export class Valimock {
| v.RecursiveSchema<() => v.BaseSchema>
| v.RecursiveSchemaAsync<() => v.BaseSchema | v.BaseSchemaAsync>
): v.Output<typeof schema> =>
this.#wrapResult(schema, this.mock(schema.getter()));
this.#wrapResult(schema, this.#mock(schema.getter()));

#mockSet = (
schema: v.SetSchema<v.BaseSchema> | v.SetSchemaAsync<v.BaseSchemaAsync>
Expand All @@ -526,7 +534,7 @@ export class Valimock {
const targetLength = fixed ?? this.options.faker.number.int({ min, max });
const result = new Set<v.BaseSchema | v.BaseSchemaAsync>();
while (result.size < targetLength) {
result.add(this.mock(schema.set.value));
result.add(this.#mock(schema.set.value));
}
return this.#wrapResult(schema, result);
};
Expand Down Expand Up @@ -633,7 +641,7 @@ export class Valimock {
): v.Output<typeof schema> =>
this.#wrapResult(
schema,
schema.tuple.items.map((item) => this.mock(item)) as v.Output<
schema.tuple.items.map((item) => this.#mock(item)) as v.Output<
typeof schema
>
);
Expand All @@ -645,7 +653,7 @@ export class Valimock {
): v.Output<typeof schema> =>
this.#wrapResult(
schema,
this.mock(this.options.faker.helpers.arrayElement([...schema.union]))
this.#mock(this.options.faker.helpers.arrayElement([...schema.union]))
);

#schemas = {
Expand Down

0 comments on commit 3826a88

Please sign in to comment.