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

Add a Validation that checks all operationIds in Link objects are valid #236

Open
mattpolzin opened this issue Dec 19, 2021 · 2 comments
Open
Labels
enhancement New feature or request good first issue Good for newcomers v3.x

Comments

@mattpolzin
Copy link
Owner

mattpolzin commented Dec 19, 2021

Create a validation that operationIds in Link objects refer to Operation objects in the document that have the given ids. This should probably be an optional validation rather than a default one.

Note that you should make changes to both the OpenAPIKit30 and OpenAPIKit modules (the changes will be mostly if not entirely identical).

@mattpolzin mattpolzin added enhancement New feature or request v3.x good first issue Good for newcomers labels Dec 19, 2021
@nexon
Copy link
Contributor

nexon commented Sep 12, 2023

Hey @mattpolzin This is still necessary? I was thinking on giving a shot doing this one. If so, can you point me out how you were thinking on implement this?

@mattpolzin
Copy link
Owner Author

This isn't necessary to release v3.0.0, but I do still want to have this validation.

This will get implemented similarly to other optional validations (it won't get added to the default-on validations here: https://github.com/mattpolzin/OpenAPIKit/blob/release/3_0/Sources/OpenAPIKit/Validator/Validator.swift#L182..L195). It will still be exposed as a property on the Validation type, though, similar to e.g. https://github.com/mattpolzin/OpenAPIKit/blob/release/3_0/Sources/OpenAPIKit/Validator/Validation+Builtins.swift#L13..L27.

This particular validation may or may not be a bit tricky to implement now that I am thinking about it a bit more, simply because it must start with the context of a Link and then from there go and locate all operations to see if the Link's operationId` is found anywhere.

If you still want to take a crack at it, you would start with something like:

    public static var linkOperationsExist: Validation<OpenAPI.Link> {
        .init(
            description: "Links with operationIds have corresponding Operations",
            check: { context in
                guard case let .b(operationId) = context.subject.operation else {
                        // don't make assertions about Links that don't have operationIds
                        return true
                }
                // TODO: look at all Operations in the document and see if any have the
                //              given operationId
                let operationIdFound: Bool
                return operationIdFound
            }
        )
    }

The part left "TODO" above is the "interesting" part because PathItems have Operations and PathItems are found both within the Document and also in the Components (for OpenAPIKit, not for OpenAPIKit30). The context variable in the above code is a ValidationContext so it has a document property that you can use to access the Document and go from there.

Let me know if any of that needs to be clarified a bit more!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers v3.x
Development

No branches or pull requests

2 participants