mrjono1 / joi-to-typescript

Convert Joi Schemas to TypeScript interfaces
MIT License
124 stars 38 forks source link

Adding allow(0) to number().positive() goes down to 0 instead of number #446

Open Cheprer opened 2 months ago

Cheprer commented 2 months ago

Hi I'm having similar problem as described here: https://github.com/mrjono1/joi-to-typescript/issues/244 and that is according to this change https://github.com/mrjono1/joi-to-typescript/issues/206 should be working differently.

As title says: Adding allow(0) to number().positive() goes down to 0 instead of number.

In general based on the issue: https://github.com/mrjono1/joi-to-typescript/issues/206 this behaviour should only happen when I would specify valid(0) or only(0).

To Reproduce Steps to reproduce the behavior:

import Joi from "joi";

export const JobSchema = Joi.object({
  uptime: Joi.number().positive().allow(0),
}).meta({ className: 'Job' });

Expected behavior Generated type should be:

export interface Job {
  uptime?: number | 0; // or it can be just number
}

Actual behavior

export interface Job {
  uptime?: 0;
}
Cheprer commented 2 months ago

For now workaround I figured could be to add .meta e.g.:

export const JobSchema = Joi.object({
  uptime: Joi.number().positive().allow(0).meta({ className: 'number' }),
}).meta({ className: 'Job' });

// results to
export interface Job {
  uptime?: number | 0;
}
mrjono1 commented 1 month ago

Allow is working almost the same as valid this should be fixed