Skip to content

Commit

Permalink
Fix bug in fallback method for specific schemas #752
Browse files Browse the repository at this point in the history
  • Loading branch information
fabian-hiller committed Jul 29, 2024
1 parent c969064 commit d8e43fc
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 12 deletions.
1 change: 1 addition & 0 deletions library/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ All notable changes to the library will be documented in this file.
- Change `unknown[]` in `LengthInput` type to `ArrayLike<unknown>`
- Change `ArrayInput` and `ContentInput` type to use `MaybeReadonly`
- Change `EMOJI_REGEX` to be more accurate and strict (pull request #666)
- Fix bug in `fallback` and `fallbackAsync` method for specific schemas (pull request #752)
- Fix bug in `fallbackAsync` method for async schemas (pull request #732)

## v0.36.0 (July 05, 2024)
Expand Down
7 changes: 5 additions & 2 deletions library/src/methods/fallback/fallback.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { describe, expect, test } from 'vitest';
import { transform } from '../../actions/index.ts';
import { number } from '../../schemas/index.ts';
import { boolean, number, union } from '../../schemas/index.ts';
import { pipe } from '../pipe/index.ts';
import { fallback, type SchemaWithFallback } from './fallback.ts';

Expand Down Expand Up @@ -29,7 +29,10 @@ describe('fallback', () => {
});
});

const schema = fallback(pipe(number(), transform(String)), '123');
const schema = fallback(
pipe(union([number(), boolean()]), transform(String)),
'123'
);

describe('should return default dataset', () => {
test('for valid input', () => {
Expand Down
8 changes: 4 additions & 4 deletions library/src/methods/fallback/fallback.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ export function fallback<
...schema,
fallback,
_run(dataset, config) {
schema._run(dataset, config);
return dataset.issues
? { typed: true, value: getFallback(this, dataset, config) }
: dataset;
const outputDataset = schema._run(dataset, config);
return outputDataset.issues
? { typed: true, value: getFallback(this, outputDataset, config) }
: outputDataset;
},
};
}
4 changes: 2 additions & 2 deletions library/src/methods/fallback/fallbackAsync.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { describe, expect, test } from 'vitest';
import { transformAsync } from '../../actions/index.ts';
import { number } from '../../schemas/index.ts';
import { boolean, number, union } from '../../schemas/index.ts';
import { pipeAsync } from '../pipe/index.ts';
import {
fallbackAsync,
Expand Down Expand Up @@ -49,7 +49,7 @@ describe('fallbackAsync', () => {

const schema = fallbackAsync(
pipeAsync(
number(),
union([number(), boolean()]),
transformAsync(async (input) => String(input))
),
async () => '123'
Expand Down
8 changes: 4 additions & 4 deletions library/src/methods/fallback/fallbackAsync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,11 @@ export function fallbackAsync<
fallback,
async: true,
async _run(dataset, config) {
await schema._run(dataset, config);
return dataset.issues
const outputDataset = await schema._run(dataset, config);
return outputDataset.issues
? // @ts-expect-error
{ typed: true, value: await getFallback(this, dataset, config) }
: dataset;
{ typed: true, value: await getFallback(this, outputDataset, config) }
: outputDataset;
},
};
}

0 comments on commit d8e43fc

Please sign in to comment.