mrjono1 / joi-to-typescript

Convert Joi Schemas to TypeScript interfaces
MIT License
125 stars 39 forks source link

Joi.object().pattern method cant convert to TypeScript! #352

Closed qraxiss closed 1 year ago

qraxiss commented 1 year ago

Describe the bug The bug is related to the usage of the .pattern method in the Joi schema. The bug prevents the expected behavior from occurring when using the .pattern method in the schema definition.

To Reproduce Steps to reproduce the behavior:

Define a param schema using the .object method and set up several properties using various .string and .boolean methods from Joi. Define a request schema using the .object method and set up properties like method, url, headers, query, and params. Use the .pattern method with regular expressions to specify the pattern for properties headers, query, and params.

import Joi from 'joi'

export const param = Joi.object({
    description: Joi.string().required(),
    type: Joi.string().required(),
    default: Joi.string().required(),
    required: Joi.boolean().required()
})

export const request = Joi.object({
    method: Joi.string().required(),
    url: Joi.string().required(),
    headers: Joi.object().pattern(/.*/, param),
    query: Joi.object().pattern(/.*/, param),
    params: Joi.object().pattern(/.*/, param)
})

Expected behavior The expected behavior is that the param schema and the request schema are defined correctly using Joi. The .pattern method should work as intended and should not cause any errors during the schema definition.

Actual behavior The actual behavior is that there is an error occurring during the usage of the .pattern method. Specifically, the error message is:

TypeError: Cannot read properties of undefined (reading 'type')

This error seems to stem from the code snippet where the .pattern method is being used.

const isMap = ((_a = details.patterns) === null || _a === void 0 ? void 0 : _a.length) === 1 && details.patterns[0].schema.type === 'string';
                                                                                                                               ^
//TypeError: Cannot read properties of undefined (reading 'type')

Additional context The issue might be related to the internal implementation of Joi or the way the .pattern method is being used in the schema definitions. Further investigation and debugging are needed to identify the root cause of the error and find a solution to the issue.

mrjono1 commented 1 year ago

yes this is a bug and it should not throw an exception here

Currently we have not much support for .pattern()

Below is the only PR to support pattern, but reading it has given me and idea on a workaround for you. https://github.com/mrjono1/joi-to-typescript/pull/165

I haven't used .pattern()

but can you use it something like .pattern(joi.string().pattern('/.*/'), param) ?

qraxiss commented 1 year ago
export const request = Joi.object({
    method: Joi.string().required(),
    url: Joi.string()
        .regex(/^https?:\/\/.+/)
        .required(),
    query: Joi.object().pattern(Joi.string(), param),
    params: Joi.object().pattern(Joi.string(), param)
})
Joi.object().pattern(Joi.string(), param)

this works for me, thanks for the help