Skip to content

Problem with intersecting looseObjects with optional pipes #1147

Open
@KubaJastrz

Description

@KubaJastrz

So I just found weird behavior when trying to instersect two looseObjects with optional pipes. Great combo, I know. The problem is that changing the combination sligthly (one object + one loose object, one pipe + one no pipe) makes it work.

I described all scenarios in the playground

Archive

import * as v from 'valibot';

console.log(
  'both object with optional pipe',
  v.safeParse(
    v.intersect([
      v.object({
        firstName: v.optional(v.pipe(v.string(), v.toLowerCase())),
      }),
      v.object({
        lastName: v.optional(v.pipe(v.string(), v.toUpperCase())),
      }),
    ]),
    {
      firstName: 'BOB',
    },
  ),
);

console.log(
  'both looseObject with optional pipe',
  v.safeParse(
    v.intersect([
      v.looseObject({
        firstName: v.optional(v.pipe(v.string(), v.toLowerCase())),
      }),
      v.looseObject({
        lastName: v.optional(v.pipe(v.string(), v.toUpperCase())),
      }),
    ]),
    {
      firstName: 'BOB',
    },
  ),
);

console.log(
  'one object, one looseObject',
  v.safeParse(
    v.intersect([
      v.object({
        firstName: v.optional(v.pipe(v.string(), v.toLowerCase())),
      }),
      v.looseObject({
        lastName: v.optional(v.pipe(v.string(), v.toUpperCase())),
      }),
    ]),
    {
      firstName: 'BOB',
    },
  ),
);

console.log(
  'one required pipe, one optional pipe',
  v.safeParse(
    v.intersect([
      v.looseObject({
        firstName: v.pipe(v.string(), v.toLowerCase()),
      }),
      v.looseObject({
        lastName: v.optional(v.pipe(v.string(), v.toUpperCase())),
      }),
    ]),
    {
      firstName: 'BOB',
    },
  ),
);

console.log(
  'one optional string, one optional pipe',
  v.safeParse(
    v.intersect([
      v.looseObject({
        firstName: v.optional(v.string()),
      }),
      v.looseObject({
        lastName: v.optional(v.pipe(v.string(), v.toUpperCase())),
      }),
    ]),
    {
      firstName: 'BOB',
    },
  ),
);

Metadata

Metadata

Assignees

Labels

intendedThe behavior is expected

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions