Skip to content

Commit

Permalink
feat(230): add validations to json-schemas in some forms
Browse files Browse the repository at this point in the history
- Draft7Validator
- Draft6Validator
- Draft4Validator
- Draft3Validator
- Draft201909Validator
- Draft202012Validator
  • Loading branch information
nicolaspastorbueno-yape committed May 7, 2024
1 parent 09a514e commit 6066663
Show file tree
Hide file tree
Showing 20 changed files with 888 additions and 28 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -157,4 +157,4 @@ cython_debug/
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/
.idea/
39 changes: 32 additions & 7 deletions pre_commit_hooks/check_json_schemas.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
from __future__ import annotations
from typing import Sequence

import argparse
import json
from fastjsonschema import compile, JsonSchemaDefinitionException, JsonSchemaException
from typing import Sequence

from jsonschema.validators import Draft7Validator, Draft3Validator, Draft4Validator, Draft6Validator, \
Draft201909Validator, Draft202012Validator
from jsonschema.exceptions import SchemaError

SCHEMA = '$schema'


def main(argv: Sequence[str] | None = None) -> int:
retval = 0
Expand All @@ -14,13 +21,31 @@ def main(argv: Sequence[str] | None = None) -> int:
try:
with open(filename, 'rb') as f:
schema = json.load(f)
compile(schema)
except JsonSchemaDefinitionException as e:

if SCHEMA not in schema:
print(f'{filename}: invalid schema, because there is not an entry with $schema in json file')
retval = 1
continue

if Draft3Validator.META_SCHEMA[SCHEMA] in schema.get(SCHEMA):
Draft3Validator.check_schema(schema)
elif Draft4Validator.META_SCHEMA[SCHEMA] in schema.get(SCHEMA):
Draft4Validator.check_schema(schema)
elif Draft6Validator.META_SCHEMA[SCHEMA] in schema.get(SCHEMA):
Draft6Validator.check_schema(schema)
elif Draft201909Validator.META_SCHEMA[SCHEMA] in schema.get(SCHEMA):
Draft201909Validator.check_schema(schema)
elif Draft202012Validator.META_SCHEMA[SCHEMA] in schema.get(SCHEMA):
Draft202012Validator.check_schema(schema)
elif Draft7Validator.META_SCHEMA[SCHEMA] in schema.get(SCHEMA):
Draft7Validator.check_schema(schema)

except SchemaError as e:
print(f'{filename}: invalid schema: {e}')
retval = 1
except JsonSchemaException:
None

return retval


if __name__ == '__main__':
raise SystemExit(main())
raise SystemExit(main())
3 changes: 2 additions & 1 deletion requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
covdefaults
coverage
pytest
pytest
jsonschema
3 changes: 3 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pytest~=8.2.0
setuptools~=68.2.0
jsonschema~=4.22.0
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ long_description = file: README.md
[options]
packages = find:
install_requires =
fastjsonschema >= 2.17.1
jsonschema >= 4.22.0

python_requires = >=3.7

Expand Down
17 changes: 15 additions & 2 deletions tests/check_json_schemas_test.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,25 @@
import pytest
from pre_commit_hooks.check_json_schemas import main


def test_valid_schemas(valid_set_of_files):
for arg in valid_set_of_files:
print(f': [test_valid_schemas] assert if {arg} is a valid schema-file')
assert main([arg]) == 0


def test_all_valid_schema_files(valid_set_of_files):
print(f': [test_all_valid_schema_files] assert if all schema-files are valid')
args = valid_set_of_files
assert main(args) == 0


def test_invalid_schemas(invalid_set_of_files):
for arg in invalid_set_of_files:
print(f': [test_invalid_schemas] assert if {arg} is a invalid schema-file')
assert main([arg]) == 1


def test_all_invalid_schemas(invalid_set_of_files):
print(f': [test_all_invalid_schemas] assert if all schema-files are invalid')
args = invalid_set_of_files
assert main(args) == 1

35 changes: 19 additions & 16 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
import pytest

@pytest.fixture
def build_content(tmp_path):
def inner(contents):
files = []
count = 0
for content in contents:
f = tmp_path/ str(count)
f.write_text(content)
files.append(str(f))
count = count + 1
return files
return inner

@pytest.fixture
def valid_set_of_files(build_content):
return build_content(["""{"format": "date-time"}""", """{"format": "date"}"""])
def valid_set_of_files():
return [
"fixtures/valid-schemas/draft-03.json",
"fixtures/valid-schemas/draft-04.json",
"fixtures/valid-schemas/draft-06.json",
"fixtures/valid-schemas/draft-07.json",
"fixtures/valid-schemas/draft-201909.json",
"fixtures/valid-schemas/draft-202012.json",
]


@pytest.fixture
def invalid_set_of_files(build_content):
return build_content(["""{"format": "invalid="}"""])
def invalid_set_of_files():
return [
"fixtures/invalid-schemas/without-schema-entry.json",
"fixtures/invalid-schemas/draft-03.json",
"fixtures/invalid-schemas/draft-04.json",
"fixtures/invalid-schemas/draft-06.json",
"fixtures/invalid-schemas/draft-07.json",
"fixtures/invalid-schemas/draft-201909.json",
"fixtures/invalid-schemas/draft-202012.json",
]
14 changes: 14 additions & 0 deletions tests/fixtures/invalid-schemas/draft-03.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"$schema": "http://json-schema.org/draft-03/schema#",
"id": "http://jsonschema.net",
"type": "object",
"properties": {
"title": {
"id": "http://jsonschema.net/title",
"type": "string"
}
},
"required": [
"title"
]
}
12 changes: 12 additions & 0 deletions tests/fixtures/invalid-schemas/draft-04.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"$schema": "http://json-schema.org/draft-04/schema#",
"id": "http://jsonschema.net",
"type": "any",
"properties": {
"title": {
"id": "http://jsonschema.net/title",
"type": "string",
"required": true
}
}
}
12 changes: 12 additions & 0 deletions tests/fixtures/invalid-schemas/draft-06.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"$schema": "http://json-schema.org/draft-06/schema#",
"id": "http://jsonschema.net",
"type": "any",
"properties": {
"title": {
"id": "http://jsonschema.net/title",
"type": "string",
"required": true
}
}
}
12 changes: 12 additions & 0 deletions tests/fixtures/invalid-schemas/draft-07.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"id": "http://jsonschema.net",
"type": "any",
"properties": {
"title": {
"id": "http://jsonschema.net/title",
"type": "string",
"required": true
}
}
}
12 changes: 12 additions & 0 deletions tests/fixtures/invalid-schemas/draft-201909.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"$schema": "https://json-schema.org/draft/2019-09/schema",
"id": "http://jsonschema.net",
"type": "any",
"properties": {
"title": {
"id": "http://jsonschema.net/title",
"type": "string",
"required": true
}
}
}
27 changes: 27 additions & 0 deletions tests/fixtures/invalid-schemas/draft-202012.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"$id": "https://jsoneditoronline.org/friends.schema.json",
"$schema": "https://json-schema.org/draft/2020-12/schema",
"title": "Friends",
"type": "array",
"items": {
"type": "object",
"properties": {
"name": {
"type": "string",
"description": "The friend's name."
},
"age": {
"description": "Age in years which must be equal to or greater than zero.",
"type": "integer",
"minimum": 0
},
"email": {
"type": "string",
"format": "email",
"description": "Optional email address of the friend.",
"required": true
}
},
"required": ["name", "age"]
}
}
13 changes: 13 additions & 0 deletions tests/fixtures/invalid-schemas/without-schema-entry.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"id": "http://jsonschema.net",
"type": "object",
"properties": {
"title": {
"id": "http://jsonschema.net/title",
"type": "string"
}
},
"required": [
"title"
]
}
Loading

0 comments on commit 6066663

Please sign in to comment.