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

[BUG] Cannot Include Json Schema in serverless.yml #87

Closed
brian-weasner opened this issue Jul 8, 2021 · 6 comments
Closed

[BUG] Cannot Include Json Schema in serverless.yml #87

brian-weasner opened this issue Jul 8, 2021 · 6 comments

Comments

@brian-weasner
Copy link
Contributor

brian-weasner commented Jul 8, 2021

Describe the bug
Serverless fails to add a Json Schema to my Http Post Request due to "File not Found".

Added Json Schema to serverless.yml the same way as described here: Serverless Request Schema Validators

To Reproduce

  1. Create a valid json schema json object (Verified that my schema is valid)
  2. Add it to the project
  3. Add the schema to the serverless http handler event with the proper file location in serverless.yml
  4. Serve in offline mode (not sure if this happens when built for production)

Expected behavior
I should not have a warning about the schema not being found, and it should be used to validate my request

Screenshots
Serverless yml file

functions:
  location:
    handler: src/controllers/location.saveLocation
    events:
      - http:
          path: location
          method: post
          request:
            schemas:
              application/json: ${file(src/schemas/location.json)}

Cmd Output

running process
Serverless: Load command interactiveCli
Serverless: Load command config
Serverless: Load command config:credentials
Serverless: Load command config:tabcompletion
Serverless: Load command config:tabcompletion:install
Serverless: Load command config:tabcompletion:uninstall
Serverless: Load command create
Serverless: Load command install
Serverless: Load command package
Serverless: Load command deploy
Serverless: Load command deploy:function
Serverless: Load command deploy:list
Serverless: Load command deploy:list:functions
Serverless: Load command invoke
Serverless: Load command invoke:local
Serverless: Load command info
Serverless: Load command logs
Serverless: Load command metrics
Serverless: Load command print
Serverless: Load command remove
Serverless: Load command rollback
Serverless: Load command rollback:function
Serverless: Load command slstats
Serverless: Load command plugin
Serverless: Load command plugin
Serverless: Load command plugin:install
Serverless: Load command plugin
Serverless: Load command plugin:uninstall
Serverless: Load command plugin
Serverless: Load command plugin:list
Serverless: Load command plugin
Serverless: Load command plugin:search
Serverless: Load command config
Serverless: Load command config:credentials
Serverless: Load command upgrade
Serverless: Load command uninstall
Serverless: Load command offline
Serverless: Load command offline:start
Serverless: Load command prune
Serverless: Load command login
Serverless: Load command logout
Serverless: Load command generate-event
Serverless: Load command test
Serverless: Load command dashboard
Serverless: Load command output
Serverless: Load command output:get
Serverless: Load command output:list
Serverless: Load command param
Serverless: Load command param:get
Serverless: Load command param:list
Serverless: Load command studio

 Serverless Warning --------------------------------------

  A valid file to satisfy the declaration 'file(src/schemas/location.json)' could not be found.

Serverless: Invoke offline

Schema File Location:
image

Check which provider is affected:
[x] AWS
[ ] Azure
[ ] Google Cloud Platform

Check which framework is affected:
[ ] Angular
[ ] Nodejs
[x] Serverless
[ ] Lambda
[ ] Infrastructure as a code

Additional context

  • Things I have tried:
    • Moving schema to root of app (next to serverless.yml and updating file path) (Does not work)
    • Console logging the file when using require() from the location handler (WORKS)
    • Adding "resolveJsonModule": true to tsconfig.json (Helps with import/require within ts, didn't help with this issue)
    • Adding Assets to the build options to copy the schema files to the dist folder (Did not work)
      • Tried multiple paths
  • Looks like its related to: [BUG] Can't include a yml function file. #23 although that issue seemed to be a serverless issue and has since been fixed.
@brian-weasner
Copy link
Contributor Author

Note: Not the same as dherault/serverless-offline#894, as I am expecting serverless to find the schema, and at least use it when deployed.

@tstackhouse
Copy link
Collaborator

I've ran into this, I don't have the code in front of me, but the workaround is to do something like this in your serverless.yml

functions:
  location:
    handler: src/controllers/location.saveLocation
    events:
      - http:
          path: location
          method: post
          request:
            schemas:
              application/json:
                - ${file(src/schemas/location.json)}
                - ${file(apps/my-app/src/schemas/location.json)}

The syntax might be different, I'd have to look at my config at $WORK, but that's the gist, you need to define the path twice, once for offline and once for APIGW, because of path resolution differrences. You might also need to make sure that the packaging step is including your schema in the generated zip file.

@brian-weasner
Copy link
Contributor Author

I decided against this approach as I couldn't get it to work, and we want validation working locally with serverless-offline, so we decided to do request body validation in our lambda functions.

Thank you for the pointers though in case anyone else runs across this. Gonna keep this issue open, as it is an issue, just no longer an issue for me as I am not using this functionality.

@tstackhouse
Copy link
Collaborator

Checked my config, this is actually the way to make offline and apigw work simultaneously with a schema in serverless.yml:

    events:
      - http:
          path: my/endpoint
          method: post
          request:
            schema:
              application/json: ${
                file(src/my-endpoint/post.request.schema.json),
                file(apps/my-app/src/my-endpoint/post.request.schema.json)}

using 2 file() declarations inside the ${} substitution separated by a comma provides failover should one file fail to be found.

I've actually taken to doing both validations you mention though, doing a basic validation at the APIGW level and then doing more complex schema validations deeper in my code, just before my business logic at the API layer. Partly due to the fact that the implementation of schemas on APIGW leave something to be desired in comparison with e.g. AJV. things like oneOf rules don't work as expected and lots of little gotchas in complex schemas that I don't recall off the top of my head.

@wickstargazer
Copy link
Member

can we close this?

@brian-weasner
Copy link
Contributor Author

Yeah, I think so. there is a workaround in place and honestly I decided to just use the latest version of AJS within my code so I could handle the response body to send back to the calling client.

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