loopbackio / loopback-next

LoopBack makes it easy to build modern API applications that require complex integrations.
https://loopback.io
Other
4.93k stars 1.06k forks source link

bug: ajv example validation #9308

Open mosoriob opened 1 year ago

mosoriob commented 1 year ago

Describe the bug

Steps to reproduce

    git clone https://github.com/mosoriob/loopback-next.git
   cd loopback-next/
   cd sandbox/validation-ajv
    npm install
    npm test

Current Behavior

The test fail with error:

Request POST /pets failed with status code 500. Error: strict mode: unknown keyword: "example"

Link to reproduction sandbox

https://github.com/mosoriob/loopback-next.git

More details

The API was generated using lb4 openapi

$ lb4 openapi pet.yaml
Loading pet.yaml...
? Select controllers and/or service proxies to be generated: OpenApiController
   create src/models/pet.model.ts
   create src/models/new-pet.model.ts
   create src/models/error.model.ts
   create src/controllers/open-api.controller.ts
npm WARN deprecated querystring@0.2.0: The querystring API is considered Legacy. new code should use the URLSearchParams API instead.

added 38 packages, and audited 609 packages in 10s

80 packages are looking for funding
  run `npm fund` for details

found 0 vulnerabilities

Logs

No response

Additional information

Similar to: https://github.com/loopbackio/loopback-next/issues/4939

Reproduction

https://github.com/mosoriob/loopback-next.git

nflaig commented 1 year ago

this seems to be an issue with the current ajv version, a workaround to fix this is to manually add example as a keyword to your loopback 4 application

src/application.ts

this.bind<AjvKeyword>("ajv.keywords.example").to({ keyword: "example" }).tag(RestTags.AJV_KEYWORD);
mosoriob commented 1 year ago

Your workaround works! Thanks! :)

jashanbaatth commented 1 year ago

@nflaig what to write if I want to remove any defined keyword like minimum or maximum. So that I can redefine that

nflaig commented 1 year ago

No idea if that works, naively would assume just overriding it but that might not be possible if it already exists. There might be other ways but that's rather ajv specific.

Let's wait and see if there is a response in the issue you opened