Skip to content

Commit

Permalink
Add example YAML and update schema JSON (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
laurensWe authored Jul 24, 2024
2 parents 9ff46fd + 5572b87 commit 87b0323
Show file tree
Hide file tree
Showing 4 changed files with 222 additions and 0 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,10 @@ Er is een klein stukje infra code geschreven voor kubernetes zodat de applicatie
```sh
kubectl apply -k infra/
```


## Validate schema of YAML

```sh
./scripts/validate --schema_file schemas/base.schema.json --yaml_file tests/sample-decision-tree-complex.yaml
```
128 changes: 128 additions & 0 deletions schemas/base.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,69 @@
"type": "string",
"uniqueItems": true
},
"Source": {
"id": "#/$defs/Source",
"type": "string"
},
"SourceURL": {
"id": "#/$defs/SourceURL",
"type": "string"
},
"Description": {
"id": "#/$defs/Description",
"type": "string"
},
"AnswerComment": {
"id": "#/$defs/AnswerComment",
"type": "string"
},
"Label": {
"id": "#/$defs/Label",
"type": "string"
},
"If": {
"id": "#/$defs/If",
"type": "string"
},
"ConclusionId": {
"id": "#/$defs/ConclusionId",
"type": "string"
},
"ConclusionComment": {
"id": "#/$defs/ConclusionComment",
"type": "string"
},
"Obligation": {
"id": "#/$defs/Obligation",
"type": "string"
},
"Default": {
"id": "#/$defs/Default",
"type": "string"
},
"Redirect": {
"id": "#/$defs/Redirect",
"type": "object",
"properties": {
"nextQuestionId": {
"$ref": "#/$defs/QuestionId"
},
"if": {
"$ref": "#/$defs/If"
},
"default": {
"$ref": "#/$defs/Default"
}
},
"oneOf": [
{
"required": ["nextQuestionId"]
},
{
"required": ["default"]
}
]
},
"Answer": {
"id": "#/$defs/Question",
"type": "object",
Expand All @@ -53,8 +112,24 @@
"result": {
"$ref": "#/$defs/Result"
},
"subresult": {
"$ref": "#/$defs/Result"
},
"answerComment": {
"$ref": "#/$defs/AnswerComment"
},
"labels": {
"type": "array",
"items": {
"$ref": "#/$defs/Label"
}
},
"redirects": {
"type": "array",
"items": {
"$ref": "#/$defs/Redirect"
},
"minItems": 2
}
},
"required": ["answer"],
Expand All @@ -64,6 +139,9 @@
},
{
"required": ["result"]
},
{
"required": ["redirects"]
}
]
},
Expand All @@ -76,6 +154,9 @@
},
{
"$ref": "#/$defs/SingleChoiceQuestion"
},
{
"$ref": "#/$defs/Conclusion"
}
]
},
Expand All @@ -89,6 +170,15 @@
"question": {
"$ref": "#/$defs/Question"
},
"source": {
"$ref": "#/$defs/Source"
},
"source_url": {
"$ref": "#/$defs/SourceURL"
},
"description": {
"$ref": "#/$defs/Description"
},
"questionType": {
"type": "string",
"enum": ["MultipleChoice"]
Expand All @@ -113,6 +203,15 @@
"question": {
"$ref": "#/$defs/Question"
},
"source": {
"$ref": "#/$defs/Source"
},
"source_url": {
"$ref": "#/$defs/SourceURL"
},
"description": {
"$ref": "#/$defs/Description"
},
"questionType": {
"type": "string",
"enum": ["SingleChoice"]
Expand All @@ -127,6 +226,35 @@
}
},
"required": ["questionId", "question", "questionType", "answers"]
},
"Conclusion": {
"id": "#/$defs/Conclusion",
"type": "object",
"properties": {
"conclusionId": {
"$ref": "#/$defs/ConclusionId"
},
"conclusion": {
"type": "string"
},
"conclusionComment": {
"$ref": "#/$defs/ConclusionComment"
},
"obligation": {
"$ref": "#/$defs/Obligation"
},
"source": {
"$ref": "#/$defs/Source"
},
"source_url": {
"$ref": "#/$defs/SourceURL"
},
"questionType": {
"type": "string",
"enum": ["Conclusion"]
}
},
"required": ["conclusionId", "conclusion", "obligation"]
}
}
}
34 changes: 34 additions & 0 deletions scripts/validate
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#!/usr/bin/env python3

import json
import sys
import argparse

import jsonschema
import yaml
from jsonschema import validate

# Argument parsing
parser = argparse.ArgumentParser()
parser.add_argument("--schema_file", help="file location of the JSON schema file to check the YAML against",
type=str)
parser.add_argument("--yaml_file", help="file location of te YAML file to check the schema against",
type=str)
args = parser.parse_args()

# Load the JSON schema
with open(args.schema_file, "r") as schema_file:
schema = json.load(schema_file)

# Load the YAML file
f = args.yaml_file
with open(f, "r") as yaml_file:
data = yaml.safe_load(yaml_file)

# Validate the data
try:
validate(instance=data, schema=schema)
print("Validation of '{f}' successful.".format(f=f))
except jsonschema.exceptions.ValidationError as err:
print("Validation error in '{f}': {err.message}".format(f=f, err=err))
sys.exit(1)
53 changes: 53 additions & 0 deletions tests/sample-decision-tree-complex.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
version: 1.0.0
name: Test Decision Tree
questions:

### 1e vraag
- questionId: '0'
question: "Is het een algoritme?"
questionType: SingleChoice
source: "AI Wetgeving MinBZK"
source_url: "https://minbzk.nl/AI_wetgeving/"
description: "Een klein test beschrijving"
answers:
- answer: Ja
labels:
- "Wel een algoritme"
- "Hoog risico AI"
subresult: "Het is eeen algoritme en een hoog risico AI systeem"
nextQuestionId: '1.0'
- answer: Nee
labels:
- "Geen algoritme"
nextQuestionId: '1.0'

## Checkvraag
- questionId: '1.0'
question: "Is het écht geen algoritme?"
questionType: SingleChoice
answers:
- answer: Ja
redirects:
- nextQuestionId: '2.0'
if: '"Wel een algoritme" in labels'
- nextQuestionId: '3.0'
if: '"Geen algoritme" in labels'
- answer: Nee
redirects:
- nextQuestionId: '3.0'
if: '"Wel een algoritme" in labels'
- nextQuestionId: '2.0'
if: '"Geen algoritme" in labels'
- default: '3.0'

## Resultaat dat alles oke is
- conclusionId: '2.0'
conclusion: "Je antwoord is consistent, goed zo"
questionType: Conclusion
obligation: "Volgens Art. 5 lid 2 ben je goed bezig"

### Resultaat dat iemand inconsistent is
- conclusionId: '3.0'
conclusion: "Je antwoord is niet consistent, probeer opnieuw"
questionType: Conclusion
obligation: "Volgens Art. 5 lid 2 ben je slecht bezig"

0 comments on commit 87b0323

Please sign in to comment.