From d3fd4efb29f32fee5a11825b3584b30781ba4a55 Mon Sep 17 00:00:00 2001 From: Mikhail Yarmaliuk Date: Mon, 6 Nov 2023 10:05:43 +0100 Subject: [PATCH] feat: add yup number strip empty string method --- src/helpers/extend-yup.ts | 5 ++++- typings/yup.d.ts | 6 +++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/src/helpers/extend-yup.ts b/src/helpers/extend-yup.ts index 7c936ec..5a64c89 100644 --- a/src/helpers/extend-yup.ts +++ b/src/helpers/extend-yup.ts @@ -1,4 +1,4 @@ -import { string, addMethod } from 'yup'; +import { string, number, addMethod } from 'yup'; /** * ExtendYupValidation @@ -8,6 +8,9 @@ function ExtendYupValidation(): void { addMethod(string, 'stripEmptyString', function () { return this.transform((value: string) => (value === '' ? null : value)); }); + addMethod(number, 'stripEmptyString', function () { + return this.transform((__, value: string) => (value === '' ? null : value)); + }); } export default ExtendYupValidation; diff --git a/typings/yup.d.ts b/typings/yup.d.ts index b9e4b84..d7a27a8 100644 --- a/typings/yup.d.ts +++ b/typings/yup.d.ts @@ -1,8 +1,12 @@ /* eslint-disable @typescript-eslint/naming-convention */ -import type { StringSchema as BaseStringSchema } from 'yup'; +import type { StringSchema as BaseStringSchema, NumberSchema as BaseNumberSchema } from 'yup'; declare module 'yup' { interface StringSchema { stripEmptyString(): BaseStringSchema; } + + interface NumberSchema { + stripEmptyString(): BaseNumberSchema; + } }