Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for input as value in v.recursive() getter #441

Merged
Merged
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
4 changes: 4 additions & 0 deletions library/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

All notable changes to the library will be documented in this file.

## vX.X.X (Month DD, YYYY)

- Add `input` of schema to `getter` function of `recursive` and `recursiveAsync` schema (pull request #441)

## v0.28.1 (February 06, 2024)

- Fix bug in `union` and `unionAsync` schema for transformed inputs (issue #420)
Expand Down
10 changes: 9 additions & 1 deletion library/src/schemas/recursive/recursive.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { describe, expect, test } from 'vitest';
import { describe, expect, test, vi } from 'vitest';
import { parse } from '../../methods/index.ts';
import { minLength } from '../../validations/index.ts';
import { string } from '../string/index.ts';
Expand All @@ -15,4 +15,12 @@ describe('recursive', () => {
expect(() => parse(schema, null)).toThrowError();
expect(() => parse(schema, {})).toThrowError();
});

test('should pass the input to the getter function as a parameter', () => {
const getter = vi.fn().mockReturnValue({ _parse: string()._parse });
const schema = recursive(getter);
const input = 'hello';
parse(schema, input);
expect(getter).toHaveBeenCalledWith(input);
});
});
6 changes: 3 additions & 3 deletions library/src/schemas/recursive/recursive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type { BaseSchema, Input, Output } from '../../types/index.ts';
* Recursive schema type.
*/
export type RecursiveSchema<
TGetter extends () => BaseSchema,
TGetter extends (input: unknown) => BaseSchema,
TOutput = Output<ReturnType<TGetter>>
> = BaseSchema<Input<ReturnType<TGetter>>, TOutput> & {
/**
Expand All @@ -24,7 +24,7 @@ export type RecursiveSchema<
*
* @returns A recursive schema.
*/
export function recursive<TGetter extends () => BaseSchema>(
export function recursive<TGetter extends (input: unknown) => BaseSchema>(
getter: TGetter
): RecursiveSchema<TGetter> {
return {
Expand All @@ -33,7 +33,7 @@ export function recursive<TGetter extends () => BaseSchema>(
async: false,
getter,
_parse(input, config) {
return this.getter()._parse(input, config);
return this.getter(input)._parse(input, config);
},
};
}
10 changes: 9 additions & 1 deletion library/src/schemas/recursive/recursiveAsync.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { describe, expect, test } from 'vitest';
import { describe, expect, test, vi } from 'vitest';
import { parseAsync } from '../../methods/index.ts';
import { minLength } from '../../validations/index.ts';
import { stringAsync } from '../string/index.ts';
Expand All @@ -15,4 +15,12 @@ describe('recursiveAsync', () => {
await expect(parseAsync(schema, null)).rejects.toThrowError();
await expect(parseAsync(schema, {})).rejects.toThrowError();
});

test('should pass input to getter', async () => {
const getter = vi.fn().mockReturnValue({ _parse: stringAsync()._parse });
const schema = recursiveAsync(getter);
const input = 'hello';
await parseAsync(schema, input);
expect(getter).toHaveBeenCalledWith(input);
});
});
13 changes: 8 additions & 5 deletions library/src/schemas/recursive/recursiveAsync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,19 @@ import type {
BaseSchema,
BaseSchemaAsync,
Input,
MaybePromise,
Output,
} from '../../types/index.ts';

/**
* Recursive schema async type.
*/
export type RecursiveSchemaAsync<
TGetter extends () => BaseSchema | BaseSchemaAsync,
TOutput = Output<ReturnType<TGetter>>
> = BaseSchemaAsync<Input<ReturnType<TGetter>>, TOutput> & {
TGetter extends (
input: unknown
) => MaybePromise<BaseSchema | BaseSchemaAsync>,
TOutput = Output<Awaited<ReturnType<TGetter>>>
> = BaseSchemaAsync<Input<Awaited<ReturnType<TGetter>>>, TOutput> & {
/**
* The schema type.
*/
Expand All @@ -30,15 +33,15 @@ export type RecursiveSchemaAsync<
* @returns An async recursive schema.
*/
export function recursiveAsync<
TGetter extends () => BaseSchema | BaseSchemaAsync
TGetter extends (input: unknown) => MaybePromise<BaseSchema | BaseSchemaAsync>
>(getter: TGetter): RecursiveSchemaAsync<TGetter> {
return {
type: 'recursive',
expects: 'unknown',
async: true,
getter,
async _parse(input, config) {
return this.getter()._parse(input, config);
return (await this.getter(input))._parse(input, config);
},
};
}
9 changes: 8 additions & 1 deletion library/src/types/other.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,14 @@ export type ObjectKeys<
/**
* Maybe readonly type.
*/
export type MaybeReadonly<T> = Readonly<T> | T;
export type MaybeReadonly<T> = T | Readonly<T>;

/**
* Maybe promise type.
*
* TODO: Refactor library with this type.
*/
export type MaybePromise<T> = T | Promise<T>;

/**
* Resolve type.
Expand Down
2 changes: 1 addition & 1 deletion website/src/routes/api/(schemas)/recursive/properties.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export const properties: Record<string, PropertyProps> = {
modifier: 'extends',
type: {
type: 'function',
params: [],
params: [{ name: 'input', type: 'unknown' }],
return: {
type: 'custom',
name: 'BaseSchema',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export const properties: Record<string, PropertyProps> = {
modifier: 'extends',
type: {
type: 'function',
params: [],
params: [{ name: 'input', type: 'unknown' }],
return: {
type: 'custom',
name: 'BaseSchema',
Expand Down
Loading