Skip to content

Parser crash for nonOptional / nonNullish wrapped around transformers emit undefined #909

@NiklasPor

Description

@NiklasPor

While building a base type which maps empty strings to undefined, I stumbled about the minLength & maxLength functions crashing, when passed an undefined value which was wrapped in a nonOptional or nonNullish:

import * as v from 'valibot';

const String = v.nonOptional(
  v.pipe(
    v.string(),
    v.transform((val) => val?.trim() || undefined),
  ),
);

const Password = v.pipe(String, v.minLength(6))

const Schema = v.object({
  password: Password
});

const result = v.safeParse(Schema, {
  password: "",
});

console.log(result);

This actually crashes the execution, even though safe Parse is used:

[error]: TypeError: Cannot read properties of undefined (reading 'length')
    at ~validate (https://valibot.dev/assets/Cb45s_t9-index.min.js:1:18263)
    at ~validate (https://valibot.dev/assets/Cb45s_t9-index.min.js:1:64883)
    at ~validate (https://valibot.dev/assets/Cb45s_t9-index.min.js:1:45222)
    at Module.Ae (https://valibot.dev/assets/Cb45s_t9-index.min.js:1:65578)
    at about:srcdoc:152:18

Playground example: https://valibot.dev/playground/?code=JYWwDg9gTgLgBAKjgQwM5wG5wGZQiOAcg2QBtgAjCGQgbgCh6BjCAO1XgGUYphWBzOAF5MAOlZsA8mBjA2ZABT04YsMDABTJSpUZRHXgIUBKADTKdensnbZoIBQpKljwgHyYyAflE9QJuAAfQLgAV1YAEw1sPg0IswsE4wZmNg44AAU0VAB3aAjhVXUtbkN+UzEQPgAZDQEYAAsFADZjY0YWdi4mBo0QZEK9CAoAKw0mGAUAbwswbLyoCIAuTPn8+gBfZI60+CgNVFDSeBE9VGRsDSyoVBKevuQKmZU51Fz8lYAiT-MtlM7UBBSBpRKQIPwFPtDsdtkA

Metadata

Metadata

Assignees

Labels

bugSomething isn't workingintendedThe behavior is expectedpriorityThis has priority

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions