From 87e4a2a77ac80fb6d04fad6b27ca4204a4fa92f0 Mon Sep 17 00:00:00 2001 From: Elton Lobo Date: Sun, 21 Jul 2024 01:49:04 +0530 Subject: [PATCH 1/3] fix `fallbackAsync` for async arg & add relevant tests --- .../methods/fallback/fallbackAsync.test.ts | 74 ++++++++++++++----- library/src/methods/fallback/fallbackAsync.ts | 2 +- 2 files changed, 55 insertions(+), 21 deletions(-) diff --git a/library/src/methods/fallback/fallbackAsync.test.ts b/library/src/methods/fallback/fallbackAsync.test.ts index 3bd6ad340..42d4f6cab 100644 --- a/library/src/methods/fallback/fallbackAsync.test.ts +++ b/library/src/methods/fallback/fallbackAsync.test.ts @@ -1,7 +1,7 @@ import { describe, expect, test } from 'vitest'; -import { transform } from '../../actions/index.ts'; -import { number } from '../../schemas/index.ts'; -import { pipe } from '../pipe/index.ts'; +import { checkAsync, transform } from '../../actions/index.ts'; +import { number, string } from '../../schemas/index.ts'; +import { pipe, pipeAsync } from '../pipe/index.ts'; import { fallbackAsync, type SchemaWithFallbackAsync, @@ -44,29 +44,63 @@ describe('fallbackAsync', () => { }); }); - const schema = fallbackAsync( - pipe(number(), transform(String)), - async () => '123' - ); + describe('when sync schema passed as the argument', () => { + const schema = fallbackAsync( + pipe(number(), transform(String)), + async () => '123' + ); - describe('should return default dataset', () => { - test('for valid input', async () => { - expect(await schema._run({ typed: false, value: 789 }, {})).toStrictEqual( - { + describe('should return default dataset', () => { + test('for valid input', async () => { + expect( + await schema._run({ typed: false, value: 789 }, {}) + ).toStrictEqual({ typed: true, value: '789', - } - ); + }); + }); + }); + + describe('should return dataset with fallback', () => { + test('for invalid input', async () => { + expect( + await schema._run({ typed: false, value: 'foo' }, {}) + ).toStrictEqual({ + typed: true, + value: '123', + }); + }); }); }); - describe('should return dataset with fallback', () => { - test('for invalid input', async () => { - expect( - await schema._run({ typed: false, value: 'foo' }, {}) - ).toStrictEqual({ - typed: true, - value: '123', + describe('when async schema passed as the argument', () => { + const schema = fallbackAsync( + pipeAsync( + string(), + checkAsync(async (value) => value === 'abcde') + ), + async () => '12345' + ); + + describe('should return default dataset', () => { + test('for valid input', async () => { + expect( + await schema._run({ typed: false, value: 'abcde' }, {}) + ).toStrictEqual({ + typed: true, + value: 'abcde', + }); + }); + }); + + describe('should return dataset with fallback', () => { + test('for invalid input', async () => { + expect( + await schema._run({ typed: false, value: 'abc' }, {}) + ).toStrictEqual({ + typed: true, + value: '12345', + }); }); }); }); diff --git a/library/src/methods/fallback/fallbackAsync.ts b/library/src/methods/fallback/fallbackAsync.ts index 43c6ea693..dacb2222a 100644 --- a/library/src/methods/fallback/fallbackAsync.ts +++ b/library/src/methods/fallback/fallbackAsync.ts @@ -79,7 +79,7 @@ export function fallbackAsync< fallback, async: true, async _run(dataset, config) { - schema._run(dataset, config); + await schema._run(dataset, config); return dataset.issues ? // @ts-expect-error { typed: true, value: await getFallback(this, dataset, config) } From 4bf68c27ecf7d9e7f2298296cc6a415b15514b3d Mon Sep 17 00:00:00 2001 From: Fabian Hiller Date: Sun, 21 Jul 2024 20:22:34 +0200 Subject: [PATCH 2/3] Change unit tests of fallbackAsync to match other tests --- .../methods/fallback/fallbackAsync.test.ts | 82 ++++++------------- 1 file changed, 27 insertions(+), 55 deletions(-) diff --git a/library/src/methods/fallback/fallbackAsync.test.ts b/library/src/methods/fallback/fallbackAsync.test.ts index 42d4f6cab..eeb9017d6 100644 --- a/library/src/methods/fallback/fallbackAsync.test.ts +++ b/library/src/methods/fallback/fallbackAsync.test.ts @@ -1,7 +1,7 @@ import { describe, expect, test } from 'vitest'; -import { checkAsync, transform } from '../../actions/index.ts'; -import { number, string } from '../../schemas/index.ts'; -import { pipe, pipeAsync } from '../pipe/index.ts'; +import { transformAsync } from '../../actions/index.ts'; +import { number } from '../../schemas/index.ts'; +import { pipeAsync } from '../pipe/index.ts'; import { fallbackAsync, type SchemaWithFallbackAsync, @@ -9,7 +9,10 @@ import { describe('fallbackAsync', () => { describe('should return schema object', () => { - const schema = pipe(number(), transform(String)); + const schema = pipeAsync( + number(), + transformAsync(async (input) => String(input)) + ); type Schema = typeof schema; const baseSchema: Omit< SchemaWithFallbackAsync, @@ -44,63 +47,32 @@ describe('fallbackAsync', () => { }); }); - describe('when sync schema passed as the argument', () => { - const schema = fallbackAsync( - pipe(number(), transform(String)), - async () => '123' - ); + const schema = fallbackAsync( + pipeAsync( + number(), + transformAsync(async (input) => String(input)) + ), + async () => '123' + ); - describe('should return default dataset', () => { - test('for valid input', async () => { - expect( - await schema._run({ typed: false, value: 789 }, {}) - ).toStrictEqual({ + describe('should return default dataset', () => { + test('for valid input', async () => { + expect(await schema._run({ typed: false, value: 789 }, {})).toStrictEqual( + { typed: true, value: '789', - }); - }); - }); - - describe('should return dataset with fallback', () => { - test('for invalid input', async () => { - expect( - await schema._run({ typed: false, value: 'foo' }, {}) - ).toStrictEqual({ - typed: true, - value: '123', - }); - }); + } + ); }); }); - describe('when async schema passed as the argument', () => { - const schema = fallbackAsync( - pipeAsync( - string(), - checkAsync(async (value) => value === 'abcde') - ), - async () => '12345' - ); - - describe('should return default dataset', () => { - test('for valid input', async () => { - expect( - await schema._run({ typed: false, value: 'abcde' }, {}) - ).toStrictEqual({ - typed: true, - value: 'abcde', - }); - }); - }); - - describe('should return dataset with fallback', () => { - test('for invalid input', async () => { - expect( - await schema._run({ typed: false, value: 'abc' }, {}) - ).toStrictEqual({ - typed: true, - value: '12345', - }); + describe('should return dataset with fallback', () => { + test('for invalid input', async () => { + expect( + await schema._run({ typed: false, value: 'foo' }, {}) + ).toStrictEqual({ + typed: true, + value: '123', }); }); }); From 659f6873f2a00697d0b645162badf5b2dcfa87c6 Mon Sep 17 00:00:00 2001 From: Fabian Hiller Date: Sun, 21 Jul 2024 20:23:57 +0200 Subject: [PATCH 3/3] Add fix in fallbackAsync method to changelog --- library/CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/library/CHANGELOG.md b/library/CHANGELOG.md index c45804dd2..80491a737 100644 --- a/library/CHANGELOG.md +++ b/library/CHANGELOG.md @@ -7,6 +7,7 @@ All notable changes to the library will be documented in this file. - Add `base64` action to validate Base64 strings (pull request #644) - Refactor `HEXADECIMAL_REGEX` (pull request #666) - Change `EMOJI_REGEX` to be more accurate and strict (pull request #666) +- Fix bug in `fallbackAsync` method for async schemas (pull request #732) ## v0.36.0 (July 05, 2024)