Skip to content

Commit dacf3bc

Browse files
Add design doc for dab validate (#1822)
## Why make this change? design doc of implementing `dab validate`. --------- Co-authored-by: Sean Leonard <[email protected]>
1 parent bc146f3 commit dacf3bc

File tree

3 files changed

+54
-0
lines changed

3 files changed

+54
-0
lines changed

docs/design/dab-validate.md

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# Design Specification: Validating a Config File
2+
3+
## Overview
4+
5+
This design specification outlines the process for validating the Data API builder config file used by a runtime engine. The validation process ensures that the config file is compliant with the schema and contains all the required information for the runtime engine to function correctly.
6+
7+
## Types of Validations run on the config file
8+
9+
The following types of validations are run on the config file (in the order specified):
10+
1. **Schema Validation**: Validates that config satisfies the schema requirements. For example, additionalProperties not defined in schema is not present in the config.
11+
2. **Config Properties Validation**: Validates that config properties such as datasource, runtime-section are correctly defined in the config. For example, both Rest and GraphQL should not be disabled.
12+
3. **Config permission Validation**: Validates semantic correctness of the permission defined for each entity. For example, `execute` permission is used only for stored-procedures.
13+
4. **Database connection Validation**: Validates that a proper connection can be estabilished with the database.
14+
5. **Entities metadata Validation**: Validates entites configuration against database. this check won't run if database connection fails. For example, checks if the specified entity is present in the DB or not.
15+
16+
## Design and Data Flow
17+
18+
1. The validation can be initiated using the CLI command `dab validate`.
19+
2. This command will have an optional flag `-c` or `--config` to provide the config file. If not used it will pick the default config file `dab-config.{DAB_ENVIRONMENT}.json`, or `dab-config.json` if `DAB_ENVIRONMENT` is not set.
20+
3. CLI calls the method `IsConfigValid` which returns true if config is valid, else returns false.
21+
4. `IsConfigValid` method calls another method in the engine `TryValidateConfig()` which contains all the different methods we have for validating different parts of the config file (highlighted in [validations-types](#types-of-validations-run-on-the-config-file)).
22+
5. We have introduced class variable called `IsValidateOnly` and `ConfigValidationExceptions` in `RuntimeConfigValidator` class.
23+
6. Whenever the validations are trigerred by `dab validate`, It will set `IsValidateOnly=true` and it will collect Exceptions thrown in `ConfigValidationException` instead of throwing it directly.
24+
> [NOTE]</br>
25+
> Exceptions thrown in method where we cannot proceed further will be handled in a way that execution doesn't continue in the method.
26+
> For Example: If an entity defined in the config is not present in the Database, we would not validate it's relationship information.
27+
28+
5. Validation Steps performed:
29+
- Schema Validation:
30+
- A new `JsonConfigSchemaValidator` class is added which uses `NJsonSchema` package to do schema validation.
31+
- It contains a method `Task<JsonSchemaValidationResult> ValidateJsonConfigWithSchemaAsync(string jsonSchema, string jsonData)` which runs schema validation and returns the result.
32+
- `jsonSchema` is obtained from the method `GetJsonSchema(RuntimeConfig runtimeConfig)`, which fetches the schema from `$schema` property if present in the config
33+
else fetches from the DAB package itself.
34+
- Config Properties Validation:
35+
- `ValidateConfigProperties()` method is called to run validations for Datasource, authentication options, runtime section.
36+
- It also validates entities do not generate duplicate queries or mutations.
37+
- ValidatePermissionsInConfig:
38+
- Validates the semantic correctness of the permissions defined for each entity within runtime configuration.
39+
- Validate Entity Metadata:
40+
- To proceed with this validation, we ensure that connection-string provided is valid.
41+
- Then we call the `ValidateEntitiesMetadata` method which first validates our connection to database using `ValidateDatabaseConnection` method.
42+
- If we are able to connect to Database, then we fetch the metadata and validate the entity and store the exceptions in the `SqlMetadataExceptions` present in SqlMetadataProvider class.
43+
- Once all the validation is done, we return all the metadata exception to the configValidationException which contains all the exceptions occured so far.
44+
6. Finally, when all the validation errors are collected, we log it in the console.
45+
7. If there are any kinds of validation errors `TryValidateConfig()` would return false, else true.
46+
47+
48+
## Example
49+
![Invalid DAB config](../media/invalid-config.png)
50+
![Validate output](../media/validate-output.png)
51+
52+
## Limitations
53+
1. Currently the `validate` command support is limited to single datasource config file.
54+
2. `NJsonSchema.Net` package currently has an open issue that overlooks/ ignores "if then else" conditions in json schema for attribute checks. (refer [here](https://github.com/RicoSuter/NJsonSchema/issues/1240))

docs/media/invalid-config.png

92.7 KB
Loading

docs/media/validate-output.png

44.7 KB
Loading

0 commit comments

Comments
 (0)