fix: nullable number not passing the nullable checks

This commit is contained in:
Florian Bouillon 2025-04-02 14:18:43 +02:00
parent abfaedda2d
commit 1ef7c4932d
Signed by: Florian Bouillon
GPG Key ID: 7676FF78F3BC40EC
2 changed files with 9 additions and 1 deletions

View File

@ -19,7 +19,7 @@ export default class SchemaNullable<Type extends SchemaItem> extends SchemaItem<
public override parse(input: unknown, options?: { fast?: boolean }): ValidationResult<SchemaInfer<Type> | undefined> {
const res = super.parse(input, options)
if (!res.valid) {
return res
return this.child.parse(input, options)
}
if (this.isNull(res.object)) {

View File

@ -63,6 +63,14 @@ test('number', () => {
expect(c.parse(1).valid).toBe(true)
expect(c.parse('2').valid).toBe(true)
expect(c.parse(true).valid).toBe(false)
// handle parsing a nullable number
// @ts-ignore
c = s.number().parseString().nullable()
expect(c.parse(1).valid).toBe(true)
expect(c.parse('2').valid).toBe(true)
expect(c.parse(null).valid).toBe(true)
expect(c.parse(true).valid).toBe(false)
})
test('object', () => {