-
-
Notifications
You must be signed in to change notification settings - Fork 189
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #103 from FlorianDevPhynix/main
Fallback method
- Loading branch information
Showing
11 changed files
with
185 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { describe, expect, test } from 'vitest'; | ||
import { object, string } from '../../schemas/index.ts'; | ||
import { parse } from '../parse/index.ts'; | ||
import { fallback } from './fallback.ts'; | ||
|
||
describe('fallback', () => { | ||
const schema1 = fallback(string(), 'test'); | ||
const schema2 = fallback(string(), () => 'test'); | ||
const schema3 = object({ key1: schema1, key2: schema2 }); | ||
|
||
test('should use default value', () => { | ||
const output1 = parse(schema1, 123); | ||
expect(output1).toBe('test'); | ||
const output2 = parse(schema2, 123); | ||
expect(output2).toBe('test'); | ||
const output3 = parse(schema3, {}); | ||
expect(output3).toEqual({ key1: 'test', key2: 'test' }); | ||
}); | ||
|
||
test('should not use default value', () => { | ||
const input1 = 'hello'; | ||
const output1 = parse(schema1, input1); | ||
expect(output1).toBe(input1); | ||
const output2 = parse(schema2, input1); | ||
expect(output2).toBe(input1); | ||
const input2 = { key1: 'hello', key2: 'hello' }; | ||
const output3 = parse(schema3, input2); | ||
expect(output3).toEqual(input2); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import type { BaseSchema, Output } from '../../types.ts'; | ||
import type { FallbackInfo } from './types.ts'; | ||
|
||
/** | ||
* Returns a fallback value when validating the passed schema failed. | ||
* | ||
* @param schema The schema to catch. | ||
* @param value The fallback value. | ||
* | ||
* @returns The passed schema. | ||
*/ | ||
export function fallback<TSchema extends BaseSchema>( | ||
schema: TSchema, | ||
value: Output<TSchema> | ((info: FallbackInfo) => Output<TSchema>) | ||
): TSchema { | ||
return { | ||
...schema, | ||
|
||
/** | ||
* Parses unknown input based on its schema. | ||
* | ||
* @param input The input to be parsed. | ||
* @param info The parse info. | ||
* | ||
* @returns The parsed output. | ||
*/ | ||
_parse(input, info) { | ||
const result = schema._parse(input, info); | ||
return { | ||
output: result.issues | ||
? typeof value === 'function' | ||
? (value as (info: FallbackInfo) => Output<TSchema>)({ | ||
input, | ||
issues: result.issues, | ||
}) | ||
: value | ||
: result.output, | ||
}; | ||
}, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { describe, expect, test } from 'vitest'; | ||
import { objectAsync, string, stringAsync } from '../../schemas/index.ts'; | ||
import { parseAsync } from '../parse/index.ts'; | ||
import { fallbackAsync } from './fallbackAsync.ts'; | ||
|
||
describe('fallbackAsync', () => { | ||
const schema1 = fallbackAsync(stringAsync(), 'test'); | ||
const schema2 = fallbackAsync(string(), () => 'test'); | ||
const schema3 = objectAsync({ key1: schema1, key2: schema2 }); | ||
|
||
test('should use default value', async () => { | ||
const output1 = await parseAsync(schema1, 123); | ||
expect(output1).toBe('test'); | ||
const output2 = await parseAsync(schema2, 123); | ||
expect(output2).toBe('test'); | ||
const output3 = await parseAsync(schema3, {}); | ||
expect(output3).toEqual({ key1: 'test', key2: 'test' }); | ||
}); | ||
|
||
test('should not use default value', async () => { | ||
const input1 = 'hello'; | ||
const output1 = await parseAsync(schema1, input1); | ||
expect(output1).toBe(input1); | ||
const output2 = await parseAsync(schema2, input1); | ||
expect(output2).toBe(input1); | ||
const input2 = { key1: 'hello', key2: 'hello' }; | ||
const output3 = await parseAsync(schema3, input2); | ||
expect(output3).toEqual(input2); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import type { BaseSchema, BaseSchemaAsync, Output } from '../../types.ts'; | ||
import type { FallbackInfo } from './types.ts'; | ||
|
||
/** | ||
* Returns a fallback value when validating the passed schema failed. | ||
* | ||
* @param schema The schema to catch. | ||
* @param value The fallback value. | ||
* | ||
* @returns The passed schema. | ||
*/ | ||
export function fallbackAsync<TSchema extends BaseSchema | BaseSchemaAsync>( | ||
schema: TSchema, | ||
value: Output<TSchema> | ((info: FallbackInfo) => Output<TSchema>) | ||
): TSchema { | ||
return { | ||
...schema, | ||
|
||
/** | ||
* Parses unknown input based on its schema. | ||
* | ||
* @param input The input to be parsed. | ||
* @param info The parse info. | ||
* | ||
* @returns The parsed output. | ||
*/ | ||
async _parse(input, info) { | ||
const result = await schema._parse(input, info); | ||
return { | ||
output: result.issues | ||
? typeof value === 'function' | ||
? (value as (info: FallbackInfo) => Output<TSchema>)({ | ||
input, | ||
issues: result.issues, | ||
}) | ||
: value | ||
: result.output, | ||
}; | ||
}, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export { fallback } from './fallback.ts'; | ||
export { fallbackAsync } from './fallbackAsync.ts'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import type { Issues } from '../../error/index.ts'; | ||
|
||
/** | ||
* Fallback info type. | ||
*/ | ||
export type FallbackInfo = { | ||
input: unknown; | ||
issues: Issues; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
title: fallback | ||
--- | ||
|
||
# fallback | ||
|
||
> The content of this page is not yet ready. Until then just use the [source code](https://github.com/fabian-hiller/valibot/blob/main/library/src/methods/fallback/fallback.ts). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters