Value.Convert: Consider re-adding type to array coercion in v1
In v0 of typebox, types were coerced to arrays when possible. In v1, this is no longer is the case.
Would you consider re-adding this to typebox? Otherwise, I would also appreciate if you can give some advice on how to best implement this ourselves.
Context: This was very convenient for URL search params. We use @fastify/type-provider-typebox.
Previous issue and PR: https://github.com/sinclairzx81/typebox/issues/823 https://github.com/sinclairzx81/typebox/pull/836
Reproduction (runnable as is using deno)
// v1:
import { Type, type Static } from "npm:typebox";
import { Value } from "npm:typebox/value";
// v0:
// import { Type, type Static } from "npm:@sinclair/typebox";
// import { Value } from "npm:@sinclair/typebox/value";
const result = Value.Convert(
Type.Object({
a: Type.Array(Type.String()),
}),
{ a: "string" }
);
console.log("result:", result);
PS: Thank you for this awesome library and all the effort you put in. I've been using it and following the progress for years
@lovre-nc Hi!
Would you consider re-adding this to typebox?
Yes, it's possible, but probably not until the next version of typebox (1.1, next year), and probably would need to be enabled via a configuration (I'm undecided at this stage)
I removed the logic in 1.0 to try and reduce some of the complexity in 0.34.x implementations, (and because the coercion logic itself was difficult to optimize for). But I do think there is value in supporting it so will consider revisiting again. I dp want Convert to map as closely to Ajv coercion rules as possible, which means modelling for the conversion matrix at the following link.
https://ajv.js.org/coercion.html
Otherwise, I would also appreciate if you can give some advice on how to best implement this ourselves.
Unfortunately, there is no mechanism in TypeBox to perform this conversion in 1.0 (so manual conversion will be required)
The focus for 1.1 is to try and do something about TypeBox Value.* (which wasn't a focus for 1.0), but where I want to spend time narrowing the API down for future optimization (possible compiler support for Default, Clean, Convert, etc) and also dedicate some time to Codecs (which may be pulled into a new sub-module, typebox/codec)
I will drop a next label on this and pick this up next year.
Thanks for the suggestion!
S