ad-on-is / adonis-autoswagger

Auto-Generate swagger docs for AdonisJS
MIT License
116 stars 38 forks source link

Autoswagger throws an error when vine.number() is inside a vine validation schema #104

Closed kingsley-sunny closed 3 months ago

kingsley-sunny commented 3 months ago

Autoswagger throws error, for vine.number() schema

Whenever I define a number schema ( vine.number() ), autoswagger throws this error user_role validatior.ts

/**
 * Validates the assign action
 */
export const assignUserRoleValidator = vine.compile(
  vine.object({
    user_id: vine.number(),
    role_id: vine.number(),
  })
)

export type AssignUserType = Infer<typeof assignUserRoleValidator>

Error Output

[05:39:27.455] ERROR (618049): Cannot read properties of null (reading 'messages')
    request_id: "xo2my6xrhtts493z1vtsa19z"
    x-request-id: "xo2my6xrhtts493z1vtsa19z"
    err: {
      "type": "TypeError",
      "message": "Cannot read properties of null (reading 'messages')",
      "stack":
          TypeError: Cannot read properties of null (reading 'messages')
              at ValidatorParser.parsePropsAndMeta (/home/bossekc/Documents/blum/backend/node_modules/.pnpm/adonis-autoswagger@3.46.0/node_modules/adonis-autoswagger/dist/parsers.js:638:24)
              at async ValidatorParser.validatorToObject (/home/bossekc/Documents/blum/backend/node_modules/.pnpm/adonis-autoswagger@3.46.0/node_modules/adonis-autoswagger/dist/parsers.js:621:16)
              at async AutoSwagger.getValidators (/home/bossekc/Documents/blum/backend/node_modules/.pnpm/adonis-autoswagger@3.46.0/node_modules/adonis-autoswagger/dist/autoswagger.js:538:39)
              at async AutoSwagger.getSchemas (/home/bossekc/Documents/blum/backend/node_modules/.pnpm/adonis-autoswagger@3.46.0/node_modules/adonis-autoswagger/dist/autoswagger.js:515:17)
              at async AutoSwagger.generate (/home/bossekc/Documents/blum/backend/node_modules/.pnpm/adonis-autoswagger@3.46.0/node_modules/adonis-autoswagger/dist/autoswagger.js:214:24)
              at async AutoSwagger.docs (/home/bossekc/Documents/blum/backend/node_modules/.pnpm/adonis-autoswagger@3.46.0/node_modules/adonis-autoswagger/dist/autoswagger.js:181:32)
      "status": 500
    }

But if I should change it to vine.string() everything works fine.

I noticed the error coming from the parsePropsAndMeta method in the parser.js,

const { SimpleMessagesProvider } = await import("@vinejs/vine");
        const [e] = await validator.tryValidate(testObj, {
            messagesProvider: new SimpleMessagesProvider({
                required: "REQUIRED",
                string: "TYPE",
                object: "TYPE",
                number: "TYPE",
                boolean: "TYPE",
            }),
        });
        // valid = true;
        const msgs = e.messages;

It is throws an error for vine.number() schema because it passes the validation and there is no error.

@ad-on-is ,

ad-on-is commented 3 months ago

Hey thx.. I've overlooked this one, and it only happenes when the whole validator is numbers only, which are set as default, and produce no errors to parse the types...

Fixed in 3.49.0