Skip to content

Commit

Permalink
chore: treat empty string as null
Browse files Browse the repository at this point in the history
this make it easier to specify a form field is optional explicitly
  • Loading branch information
beenotung committed Feb 17, 2025
1 parent b01eca6 commit cead2ca
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/core.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -997,6 +997,9 @@ describe('nullable parser', () => {
it('should pass non-null value', () => {
expect(nullable(string()).parse('guest')).to.equals('guest')
})
it('should treat empty string as null', () => {
expect(nullable(string()).parse('')).to.be.null
})
it('should reject not matched value', () => {
expect(() => nullable(string()).parse(undefined)).to.throws(
'Invalid nullable string, got undefined',
Expand Down
1 change: 1 addition & 0 deletions src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -778,6 +778,7 @@ export function nullable<T>(
) {
function parse(input: unknown, context: ParserContext = {}): T | null {
if (input === null) return null
if (input === '') return null
let typePrefix = context.typePrefix
return parser.parse(input, {
...context,
Expand Down

0 comments on commit cead2ca

Please sign in to comment.