From 30ac2df9dc31c0e79a92025c4c1a7c60a7c877f2 Mon Sep 17 00:00:00 2001 From: Julien Mailleret <8582351+jmlrt@users.noreply.github.com> Date: Thu, 4 Jul 2024 19:04:43 +0200 Subject: [PATCH] Add simple test (#7) * Add simple test * fix gh action * format --- .github/workflows/tests.yml | 17 ++++++++ .pre-commit-config.yaml | 3 +- .pre-commit-hooks.yaml | 1 + README.md | 2 +- setup.cfg | 2 +- tests/food.json | 51 +++++++++++++++++++++++ tests/person.json | 82 +++++++++++++++++++++++++++++++++++++ tests/test.yaml | 27 ++++++++++++ 8 files changed, 182 insertions(+), 3 deletions(-) create mode 100644 .github/workflows/tests.yml create mode 100644 tests/food.json create mode 100644 tests/person.json create mode 100644 tests/test.yaml diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml new file mode 100644 index 0000000..52f3eb3 --- /dev/null +++ b/.github/workflows/tests.yml @@ -0,0 +1,17 @@ +name: tests + +on: + pull_request: + push: + branches: [main] + +jobs: + tests: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - uses: actions/setup-python@v3 + - run: python -m pip install . + shell: bash + - run: check-yamlschema -v tests/test.yaml .pre-commit-config.yaml .pre-commit-hooks.yaml .github/workflows/pre-commit.yml + shell: bash diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 2dc954a..77b72d8 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,4 +1,4 @@ -# .pre-commit-config.yaml +# yaml-language-server: $schema=https://json.schemastore.org/pre-commit-config.json repos: - repo: https://github.com/pre-commit/pre-commit-hooks rev: v4.4.0 @@ -6,6 +6,7 @@ repos: - id: trailing-whitespace - id: end-of-file-fixer - id: check-yaml + args: [--allow-multiple-documents] - id: check-ast - id: debug-statements - repo: https://github.com/asottile/pyupgrade diff --git a/.pre-commit-hooks.yaml b/.pre-commit-hooks.yaml index 6c54eaf..e76b4af 100644 --- a/.pre-commit-hooks.yaml +++ b/.pre-commit-hooks.yaml @@ -1,3 +1,4 @@ +# yaml-language-server: $schema=https://json.schemastore.org/pre-commit-hooks.json - id: check-yamlschema name: Check YAML schemas description: Validates YAML files with multiple documents according to their json schemas diff --git a/README.md b/README.md index 6b56f3b..2d8d97a 100644 --- a/README.md +++ b/README.md @@ -47,7 +47,7 @@ Add this to your `.pre-commit-config.yaml`: ```yaml - repo: https://github.com/jmlrt/check-yamlschema - rev: v0.0.3 + rev: v0.0.4 hooks: - id: check-yamlschema ``` diff --git a/setup.cfg b/setup.cfg index 347912e..ba06a40 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,6 +1,6 @@ [metadata] name = check-yamlschema -version = 0.0.3 +version = 0.0.4 description = A CLI and pre-commit hooks for jsonschema validation in YAML files with multiple documents long_description = file: README.md long_description_content_type = text/markdown diff --git a/tests/food.json b/tests/food.json new file mode 100644 index 0000000..500bbbf --- /dev/null +++ b/tests/food.json @@ -0,0 +1,51 @@ +{ + "$schema": "http://json-schema.org/draft-07/schema#", + "type": "object", + "properties": { + "food": { + "type": "array", + "items": { + "oneOf": [ + { + "type": "object", + "properties": { + "vegetables": { + "type": "string" + } + }, + "required": ["vegetables"], + "additionalProperties": false + }, + { + "type": "object", + "properties": { + "fruits": { + "type": "object", + "properties": { + "citrics": { + "type": "string" + }, + "tropical": { + "type": "string" + }, + "nuts": { + "type": "string" + }, + "sweets": { + "type": "string" + } + }, + "required": ["citrics", "tropical", "nuts", "sweets"], + "additionalProperties": false + } + }, + "required": ["fruits"], + "additionalProperties": false + } + ] + } + } + }, + "required": ["food"], + "additionalProperties": false + } diff --git a/tests/person.json b/tests/person.json new file mode 100644 index 0000000..b191b7d --- /dev/null +++ b/tests/person.json @@ -0,0 +1,82 @@ +{ + "$schema": "http://json-schema.org/draft-07/schema#", + "type": "object", + "properties": { + "name": { + "type": "string" + }, + "age": { + "type": "integer" + }, + "hobbies": { + "type": "array", + "items": { + "type": "string" + } + }, + "programming_languages": { + "type": "object", + "properties": { + "java": { + "type": "string", + "enum": ["Beginner", "Intermediate", "Advanced"] + }, + "python": { + "type": "string", + "enum": ["Beginner", "Intermediate", "Advanced"] + }, + "javascript": { + "type": "string", + "enum": ["Beginner", "Intermediate", "Advanced"] + } + }, + "required": ["java", "python", "javascript"], + "additionalProperties": false + }, + "favorite_food": { + "type": "array", + "items": { + "oneOf": [ + { + "type": "object", + "properties": { + "vegetables": { + "type": "string" + } + }, + "required": ["vegetables"], + "additionalProperties": false + }, + { + "type": "object", + "properties": { + "fruits": { + "type": "object", + "properties": { + "citrics": { + "type": "string" + }, + "tropical": { + "type": "string" + }, + "nuts": { + "type": "string" + }, + "sweets": { + "type": "string" + } + }, + "required": ["citrics", "tropical", "nuts", "sweets"], + "additionalProperties": false + } + }, + "required": ["fruits"], + "additionalProperties": false + } + ] + } + } + }, + "required": ["name", "age", "hobbies", "programming_languages", "favorite_food"], + "additionalProperties": false + } diff --git a/tests/test.yaml b/tests/test.yaml new file mode 100644 index 0000000..503877a --- /dev/null +++ b/tests/test.yaml @@ -0,0 +1,27 @@ +# yaml-language-server: $schema=./food.json +food: # Some YAML document + - vegetables: tomatoes + - fruits: + citrics: oranges + tropical: bananas + nuts: peanuts + sweets: raisins +--- +# yaml-language-server: $schema=./person.json +name: Martin Devloper # Some other YAML document +age: 26 +hobbies: + - painting + - playing_music + - cooking +programming_languages: + java: Intermediate + python: Advanced + javascript: Beginner +favorite_food: + - vegetables: tomatoes + - fruits: + citrics: oranges + tropical: bananas + nuts: peanuts + sweets: raisins