fabian-hiller / valibot

The modular and type safe schema library for validating structural data 🤖
https://valibot.dev
MIT License
6.33k stars 204 forks source link

Decimal doesn't support non-integer numbers #822

Closed andrew-3kb closed 2 months ago

andrew-3kb commented 2 months ago

The decimal validation action will only pass on integers, and not on decimals that aren't integers.

import * as v from 'valibot';
const Schema = v.pipe(v.string(), v.decimal());
const result = v.safeParse(Schema, "2.2");
console.log(result); // success: false (I think it should be true)

I've seen issue #665 on here where someone raised the same thing, which had a explanation that valibot actually means "base ten integer" instead of decimal. I want to make a case that we should consider changing this for the following reasons:

If we want to keep the behaviour of decimal as it is (and backwards compatibility is a great reason) I think the documentation should be updated to be clear it only will validate base ten integers and remove the link to the wikipedia article. It would be nice to add a baseTenNumberIncludingNonIntegers action in this case, hopefully someone can think of a better name though.

Otherwise I propose that decimal is updated to pass on non-integers, and we add a new validation action for the old behaviour called something like baseTenInteger.

I'm happy to do the work and make a pull request for either if you agree with one of the above.

fabian-hiller commented 2 months ago

I agree that the current implementation of decimal can be confusing. Maybe we should rename decimal to digits and add a new decimal action that accepts any base ten number (even non-integer).

andrew-3kb commented 2 months ago

Was fixed by https://github.com/fabian-hiller/valibot/pull/823