Skip to content

Commit

Permalink
feat: add yup number strip empty string method
Browse files Browse the repository at this point in the history
  • Loading branch information
MatthewPattell committed Nov 6, 2023
1 parent 76bbcb2 commit d3fd4ef
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
5 changes: 4 additions & 1 deletion src/helpers/extend-yup.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { string, addMethod } from 'yup';
import { string, number, addMethod } from 'yup';

/**
* ExtendYupValidation
Expand All @@ -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;
6 changes: 5 additions & 1 deletion typings/yup.d.ts
Original file line number Diff line number Diff line change
@@ -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;
}
}

0 comments on commit d3fd4ef

Please sign in to comment.