Skip to content
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

transform does not work with fallback if union is also used... #752

Closed
thecotne opened this issue Jul 29, 2024 · 2 comments
Closed

transform does not work with fallback if union is also used... #752

thecotne opened this issue Jul 29, 2024 · 2 comments
Assignees
Labels
bug Something isn't working priority This has priority

Comments

@thecotne
Copy link

import * as v from 'valibot';

const BoolWithUnion = v.pipe(
  v.union([v.literal('true'), v.literal('false')]),
  v.transform((x) => x === 'true'),
)

const BoolWithFallbackUnion = v.fallback(BoolWithUnion, true);

const BoolWithCheck = v.pipe(
  v.string(),
  v.check(x => ['true', 'false'].includes(x)),
  v.transform((x) => x === 'true'),
)

const BoolWithFallbackCheck = v.fallback(BoolWithCheck, true);

const NumWithUnion = v.pipe(
  v.union([v.string(), v.number()]),
  v.transform((x) => typeof x === 'string' ? parseInt(x) : x),
)

const NumWithFallbackUnion = v.fallback(NumWithUnion, 42);

const NumWithCheck = v.pipe(
  v.any(),
  v.check(x => typeof x === 'string' || typeof x === 'number'),
  v.transform((x) => typeof x === 'string' ? parseInt(x) : x),
)

const NumWithFallbackCheck = v.fallback(NumWithCheck, 42);

console.log(v.parse(BoolWithUnion, 'false'));// false
console.log(v.parse(BoolWithCheck, 'false'));// false
console.log(v.parse(BoolWithFallbackUnion, 'false'));// "false" should be false
console.log(v.parse(BoolWithFallbackCheck, 'false'));// false
console.log(v.parse(NumWithUnion, '42'));// 42
console.log(v.parse(NumWithCheck, '42'));// 42
console.log(v.parse(NumWithFallbackUnion, '42'));// "42" should be 42
console.log(v.parse(NumWithFallbackCheck, '42'));// 42
@fabian-hiller
Copy link
Owner

Thank you! You found a bug! I will investigate this problem and check our unit tests.

@fabian-hiller fabian-hiller self-assigned this Jul 29, 2024
@fabian-hiller fabian-hiller added bug Something isn't working priority This has priority labels Jul 29, 2024
@fabian-hiller
Copy link
Owner

v0.37.0 is available

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working priority This has priority
Projects
None yet
Development

No branches or pull requests

2 participants