Skip to content

v.decimal() requires a leading zero to be considered a valid decimal #1072

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
asg86260 opened this issue Mar 7, 2025 · 5 comments · Fixed by #1086
Closed

v.decimal() requires a leading zero to be considered a valid decimal #1072

asg86260 opened this issue Mar 7, 2025 · 5 comments · Fixed by #1086
Assignees
Labels
fix A smaller enhancement or bug fix workaround Workaround fixes problem

Comments

@asg86260
Copy link

asg86260 commented Mar 7, 2025

I threw together an example using the valibot playground.

.1 throws an error
0.1 is fine.

import * as v from 'valibot';

const Schema = v.object({
  noLeadingZero: v.pipe(v.string(), v.decimal()),
  leadingZero: v.pipe(v.string(), v.decimal()),
});

const result = v.safeParse(Schema, {
  noLeadingZero: '.1',
  leadingZero: '0.1',
});

console.log(result);

and the output here

{
  typed: true,
  success: false,
  output: {
    noLeadingZero: ".1",
    leadingZero: "0.1"
  },
  issues: [
    {
      kind: "validation",
      type: "decimal",
      input: ".1",
      expected: null,
      received: "\".1\"",
      message: "Invalid decimal: Received \".1\"",
      requirement: RegExp,
      path: [
        {
          type: "object",
          origin: "value",
          input: {
            noLeadingZero: ".1",
            leadingZero: "0.1"
          },
          key: "noLeadingZero",
          value: ".1"
        }
      ],
      issues: undefined,
      lang: undefined,
      abortEarly: undefined,
      abortPipeEarly: undefined
    }
  ]
}
@fabian-hiller
Copy link
Owner

Thank you for creating this issue. I am not sure if we should change this. The .1 thing seems to be more or less a programming thing. What do you think?

In the meantime regex with a modified version of the DECIMAL_REGEX can be used.

@fabian-hiller fabian-hiller self-assigned this Mar 8, 2025
@fabian-hiller fabian-hiller added question Further information is requested workaround Workaround fixes problem labels Mar 8, 2025
@asg86260
Copy link
Author

asg86260 commented Mar 8, 2025

Thanks for replying so quickly!

Yea, I honestly wasn't sure if this was intended behavior or not. In our case a user types in .1 into some form. which seems like a valid decimal input, but the validation for v.decimal is saying its incorrect. I can see arguments for both sides, and ill defer to yall.

we can totally work with a regex modification if that behavior is intended.

@fabian-hiller
Copy link
Owner

fabian-hiller commented Mar 11, 2025

Hey, I thought about it and think that we should probably make the number before the . somehow optional. Would you be interested in creating a PR that updates the regex and tests?

@fabian-hiller fabian-hiller added fix A smaller enhancement or bug fix and removed question Further information is requested labels Mar 11, 2025
@fabian-hiller
Copy link
Owner

I will create a PR

@asg86260
Copy link
Author

nice, sorry I couldn't find time for this last week. i appreciate the update!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
fix A smaller enhancement or bug fix workaround Workaround fixes problem
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants