Config schema validation#7335
Conversation
There was a problem hiding this comment.
@preshetin Thanks for taking a start on that.
As it's a sensitive and significant feature, I think before jumping into an actual implementation, it'll be good to agree on some basic implementation spec.
Let me outline, what I think of it:
Where validation logic should be attached?
I believe that validation should not be configured as a plugin but be a part of a core (so be incorporated into engine which is also responsible for load of config, resolving variables, intiializing plugins). It should be considered as a core feature.
When validation should happen?
Ideally validation should happen after all plugins were initialized and variables resolved but before life-cycle engine jump in.
Technically it's when current service.validate() happens:
serverless/lib/classes/Service.js
Lines 174 to 202 in 7abb23e
Which already tries to do some validation which should we back with schema.
I would replace this logic with new schema validation, and ensure upfront we cover the validation it's covered now.
What objects should validation cover?
Some problem is that current config handling is not clean. We do not have one object with which we work, but we span config parts on our service instance here:
serverless/lib/classes/Service.js
Lines 98 to 131 in 7abb23e
and then work with that. Variables resolution is made on service instance.
Ideally after Variables resolution is made. I think we should prepare a plain object that derives from service as follows:
const configToValidate = {
service: service.serviceObject,
app: service.app,
org: service.org,
custom: service.custom,
plugins: service.plugins,
resources: service.resources,
functions: service.functions.
provider: service.provider,
package: service.package,
layers: service.layers,
outputs: service.outputs
}And validate that.
@pmuens what are your thoughts on that?
For now just follow with Node v6. Hopefully we'd be able to drop support for it shortly |
|
@medikoo thank you for this guidance. Next time I'll try to start by gathering feedback first before submitting a PR. I agree with all your points. It would require slight adjustments to this PR (like moving validation logic from package plugin to core Service class) which I am ready to implement. Also, do you agree with the proposed multi-schema approach? Using many small schemas would allow plugins to easily extend any piece of the schema. Otherwise, I don't think that extending a branch in a big tree-like schema is possible in Would you like me to implement changes, or it is better to wait for @pmuens feedback? Also, I assume we keep sticking with Joi lib, although it does not support Node v6. Hopefully, Node v6 support will be dropped by the time this PR is merged. |
I think we can safely assume it's a v2 feature (in that case we may even crash on errors). We plan to rollout v2 in less than a month I think. So it shouldn't be blocked for too long.
I think we need a validation tool that allows to easily in generic way extend base schema (e.g. define a new property on some object which is a deep nested property in global schema). So we don't have to upfront define those eventual extension points, as otherwise we can easily run into maintenance issues in a future (it's likely that we will need more and more customization options on the way). I've outlined my concerns at the bottom of description here #6562 Let's continue discussion there (I've also pinned that issue, which I cannot do with PR, so hopefully we get additional feedback from community). After we agree over there on implementation aspects, let's just continue work on implementation over here |
There was a problem hiding this comment.
Great idea! I was always looking for a formal spec for serverless.yml. Ideally we'd put version: 1 or something like that in serverless.yml so that we can have multiple validation schemas (and therefore version serverless.yml).
Only question I have is whether this breaks custom plugins. The main problem is that custom plugins can extend the serverless object in whatever way they want. The "official" documentation says that only the custom property should be used by plugin authors but AFAIK this is not widely known.
|
@preshetin I've updated the implementation with new version of If you have opportunity please take a look into it, I've tried my best in documenting logic. I'd like to be sure I didn't remove any valuable functionality which was covered in your version. I've also changed slightly approach in unspecified provider validation - in all (but "off") cases it's just dedicated warning reported. I've also already created github issues to cover schemas for all config parts which we left not specified fully, and provided links to corresponding issues in code. I've tested it, and it seems to work really well, would love to publish it on monday! :) |
|
@medikoo I'm not upset at all. In your place, I'd do the same as it is often easier to redo from scratch than understanding someone's logic. I tested it and it looks pretty good. The new way of showing errors under one list feels more uniform. One small regression I've noticed. In case of When I have time for open source, I'd be happy to jump into one of those numerous issues on fine-tuning other schema parts. I don't want to be too cheerful about the release because many times before it was postponed. In case this happens, I hope it will go smoothly. |

What did you implement
This PR implements the validation of the essential parts of
serverless.ymlconfig backed by AJV.This validation architecture consists of one schema that is dynamically extended by plugins (both internal and external). After the schema is extended by plugins, it is used to perform the validation of the user's config.
This PR implements validation engine with relaxed schema. Fine-tuning schema parts is expected to be covered by other PR's.
Closes #6562
How can we verify it
This
serverless.ymlfile contains errors which this PR should log to the console.Todos
Useful Scripts
npm run test:ci--> Run all validation checks on proposed changesnpm run lint:updated--> Lint all the updated filesnpm run lint:fix--> Automatically fix lint problems (if possible)npm run prettier-check:updated--> Check if updated files adhere to Prettier confignpm run prettify:updated--> Prettify all the updated filesUpdate documentation
Implement logic for friendly error messages
Implement schema extenstion helpers
Freeze neccessary schema parts
Configuration validation backed by schema #6562 (comment) Implement all schema points from mentioned comment except
functionspartConfiguration validation backed by schema #6562 (comment) Implement schema point for
functionspartConfiguration validation backed by schema #6562 (comment) Move
httpspecific logic fromservice.validate()toapiGateway/lib/validate.jsEnable "Allow edits from maintainers" for this PR
Write and run all tests
Update the messages below
Is this ready for review?: YES
Is it a breaking change?: NO