Skip to content

Multi-schema nullable validators not working as expected. #303

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
ccampanale opened this issue Aug 22, 2022 · 3 comments
Closed

Multi-schema nullable validators not working as expected. #303

ccampanale opened this issue Aug 22, 2022 · 3 comments

Comments

@ccampanale
Copy link

I'm using [email protected] and attempting a multi-validator schema to represent the union type string | boolean | null while disallowing undefined but this is not working as I expect.

According to the docs:

If you want disallow undefined value but allow null value, use nullable instead of optional.

const schema = {
    dateString: [
        { type: "string", nullable: true },
        { type: "boolean", nullable: true }
    ]
}

check = v.compile(schema);

check({ dateString: true }); // Valid
check({ dateString: new Date().toISOString() }); // Valid
check({ dateString: null }); // Fail

The third test should pass since both schemas are nullable. Instead I see the follow results from the validator:

[
  {
    type: 'required',
    message: "The 'dateString' field is required.",
    field: 'dateString',
    actual: null
  }
]

Let me know if I a missing something and thanks for taking a look!

@icebob icebob added the bug label Aug 27, 2022
@icebob
Copy link
Owner

icebob commented Aug 27, 2022

The nullable is not working with multi rule shorthand format, try this form:

const schema = {
	dateString: {
		type: "multi", rules: [
			{ type: "string" },
			{ type: "boolean" }
		], nullable: true
	}
};

image

@icebob icebob removed the bug label Aug 27, 2022
@icebob icebob closed this as completed in b580a12 Aug 27, 2022
@ccampanale
Copy link
Author

@icebob Thanks for addressing this. The long form multi - which I honestly must have glossed over in the docs - works great.

@icebob
Copy link
Owner

icebob commented Aug 29, 2022

Nop, by the way, the issue fixed in the latest version, so your original code can work as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants