hapijs / joi

The most powerful data validation library for JS
Other
20.95k stars 1.51k forks source link

feat: Improve object type #2979

Open felixmosh opened 1 year ago

felixmosh commented 1 year ago

This PR improves object's types.

Before

  1. regular object - output schema is any

    const testSchema = Joi.object({ FOO: 'bar' });

    image

  2. with keys - output schema is any image

  3. with append - output schema not changed image

  4. with rename - no key completion, output schema not changed image

  5. with with - no key's completion image

  6. with without - no key's completion image

After

  1. regular object usage incorporates the object type that was passed image which means that calling to attempt returns the object type properly image

  2. using keys without provided object image extending provided object image

  3. with append append with null or undefined not changing the original obj image appending new object keys image

  4. with rename - key completion, output schema changed image image

  5. with with - key completion image completes the rest of the keys (since .with('foo', 'foo') doesn't make sense) image

  6. with without - same as with image image

closes #2978

Marsup commented 1 year ago

Hi,

Thanks for all those PRs, I'm a bit torn on this one though. isStrict has been so far used by many people to ensure the schema contains what the type mandates, while inference is a goal, the other way around is also kind of nice, do you think it would be possible to preserve this?

felixmosh commented 1 year ago

This is how zod does that... It is weird (in my opinion) the need of writing a type manually, and then repeat it within the object it self....

I guess i can add an overload or something.

Neiz-Kap commented 2 months ago

Is it going to be updated/pushed?