Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect formatters being used for Ajv in Validation #280

Open
awcchungster opened this issue Dec 16, 2021 · 7 comments
Open

Incorrect formatters being used for Ajv in Validation #280

awcchungster opened this issue Dec 16, 2021 · 7 comments

Comments

@awcchungster
Copy link

I am getting errors on run from ajv that is being loaded in openapi-backend.

unknown format "date-time" ignored in schema at path "#/properties/timestamp"
unknown format "date-time" ignored in schema at path "#/properties/timestamp" 

The fix is this snippet that should go into the validation file.

const AJV = require('ajv').default;
const addFormats = require('ajv-formats').default;
const ajv = new AJV();
addFormats(ajv);
@ak683517
Copy link

If you just want to add date-time support you can use:

  const api = new OpenAPIBackend({
      definition: '...........',
      customizeAjv: (ajv, ajvOpts, validationContext) => {
          let dtFormat = {
              type: 'string',
              validate: /^\d\d\d\d-[0-1]\d-[0-3]\dt(?:[0-2]\d:[0-5]\d:[0-5]\d|23:59:60)(?:\.\d+)?(?:z|[+-]\d\d(?::?\d\d)?)$/i,
          };
          ajv.addFormat("date-time", dtFormat);
          return ajv;
      }
  });

The regex above was copied from the "ajv-formats" module, here:
https://github.com/ajv-validator/ajv-formats/blob/8b424f1e11d23f556cc12f1b9fd16a37286cf326/src/formats.ts

@awcchungster
Copy link
Author

Hi @ak683517,

That snippet returned an error with

index.ts:45:32 - error TS2345: Argument of type '{ type: string; validate: RegExp; }' is not assignable to parameter of type 'Format'.
  Property 'async' is missing in type '{ type: string; validate: RegExp; }' but required in type 'AsyncFormatDefinition<number>'.

45     ajv.addFormat("date-time", dtFormat);
                                  ~~~~~~~~

  node_modules/ajv/dist/types/index.d.ts:165:5
    165     async: true;
            ~~~~~
    'async' is declared here.


Found 1 error.

@sam3k
Copy link

sam3k commented Feb 3, 2022

I'd cast it as any:

ajv.addFormat("date-time", dtFormat as any);

@drodil
Copy link
Contributor

drodil commented Sep 29, 2022

Any updates when this is going to be fixed?

@BenCGI
Copy link

BenCGI commented May 23, 2023

Would also like to get an update -> so we should add the formatter ourselves if needed?

@wolfgangbecker
Copy link

wolfgangbecker commented Aug 22, 2023

The following worked for me:

import { fullFormats } from 'ajv-formats/dist/formats'

const api = new OpenAPIBackend({
  definition: '...........',
  ajvOpts: {
    formats: fullFormats,
  },
})

@ydayagi
Copy link

ydayagi commented Dec 5, 2023

@anttiviljami
Any updates when this is going to be fixed?

openshift-merge-bot bot pushed a commit to janus-idp/backstage-plugins that referenced this issue Feb 26, 2024
#1270)

Fix warn "unknown format X ignored in schema at path Y"

During backstage startup there are warnings like
`unknown format "date-time" ignored in schema at path "#/oneOf/0/properties/ended"`
due to a bug in OpenAPIBackend[1].

Resolve https://issues.redhat.com/browse/FLPATH-1045

Based on Yaron Dayagi <ydayagi@redhat.com> work in notification
plugin[2]

[1] openapistack/openapi-backend#280
[2] https://github.com/janus-idp/backstage-plugins/blob/903c7f37a1cf138ac96ef3f631f951866c2014fa/plugins/notifications-backend/src/service/router.ts#L45-L52

Signed-off-by: Gloria Ciavarrini <gciavarrini@redhat.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants