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

On using validatePaths option #118

Open
WHKL-9 opened this issue Jan 27, 2022 · 6 comments
Open

On using validatePaths option #118

WHKL-9 opened this issue Jan 27, 2022 · 6 comments

Comments

@WHKL-9
Copy link

WHKL-9 commented Jan 27, 2022

Hi, I am trying to use this validatePaths option. Looked into your source code and see you guys are using it with the .startWith() method. Can you provide some concrete examples of how to use that?

An example I was trying=> customer/:id/setup-accounts
I tried the validatePath option but it didn't get validated because it might fail with the startWith method in the validatePath option.
The best-case scenario is that I could want to validate the path inside my yaml/json file and skip the validation for requests that are not in the spec.

Looking forward to your reply and have a nice day!

@rafael-fecha
Copy link

+1

@supertong
Copy link
Collaborator

Hey @WHKL-9, one exmaple of validatePaths would be

const oasMw = await oas({
  file: `${__dirname}/../openapi.yaml`,
  endpoint: '/openapi.json',
  uiEndpoint: '/',
  validatePaths: ['/api']
})

This will make sure that all paths like /api/* will be validated. If you want to validate all pathes, simply put it as /.
I hope it makes sense otherwise, it would be great if you could provider a detailed exmple on how you plan to use it.

@WHKL-9
Copy link
Author

WHKL-9 commented Jan 31, 2022

Hey @supertong , thanks for your reply !

Sorry, I think I was not clear enough.

I have requests that they have dynamic id's in the middle of the request path like: customer/:id/setup-accounts, so I would like to specify in the validate paths. I tried it out something like: validatePaths: [customer/:id/setup-accounts'] in order to validate any kind of requests like:
customer/cea2ffb8-828d-11ec-a8a3-0242ac120002/setup-accounts
customer/dea3022e-828d-11ec-a8a3-0242ac120002/setup-accounts
but didn't work.

The best case scenario for me would be actually that I want only to validate paths of requests that are specified in the openapi.yaml provided in the file oas method property, and I want to skip the validation at all for paths not present in the open api spec . This means, if I have in my open api spec only the setup accounts request, only this setup accounts would be validated and any other requests the validation would be skipped.

The reason is that I have a backend with many routes setup and I want to use the oas middleware to start validating the routes from the open api spec, but I don't want to add already all the requests to the spec, I want to do it gradually. So if I add the route path spec to the open api schema I want that the validation is triggered, otherwise if there's any other request o my routes that are not in the open api schema I want to skip the validation so oas3 do not throw an error like could not find path.

@supertong
Copy link
Collaborator

Hey @WHKL-9, that makes sense. Unfortunately, the validatePaths property simply check the prefix of the path like you already learnt. One proper use case of it is to separate paths that you want to apply validation with different prefix. For example, for all the path that you want to validate, simply give them a prefix like
/api/customer/:id/setup-accounts and the validatePaths: ['/api'] would make sure it's validated and other pathes don't.

I want to skip the validation at all for paths not present in the open api spec
Another way would be providing a generic json schema type for the routes that you want to skip validation. For example, { "type": ["object", "string"] }.

@supertong
Copy link
Collaborator

Hey @WHKL-9, would this PR helps you? #119

@WHKL-9
Copy link
Author

WHKL-9 commented Feb 10, 2022

yes I think I would. Thank you so much for getting in touch again! @supertong

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

3 participants