generated from maykinmedia/default-app
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from maykinmedia/features/jsonschema-model
Features/jsonschema model
- Loading branch information
Showing
12 changed files
with
244 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import json | ||
|
||
from django import forms | ||
from django.contrib import admin | ||
|
||
from .models import JsonSchema | ||
|
||
|
||
class IndentedJSONEncoder(json.JSONEncoder): | ||
def __init__(self, *args, indent, sort_keys, **kwargs): | ||
super().__init__(*args, indent=2, **kwargs) | ||
|
||
|
||
class JsonSchemaAdminForm(forms.ModelForm): | ||
schema = forms.JSONField(encoder=IndentedJSONEncoder) | ||
|
||
class Meta: | ||
model = JsonSchema | ||
fields = "__all__" | ||
|
||
|
||
@admin.register(JsonSchema) | ||
class JsonSchemaAdmin(admin.ModelAdmin): | ||
form = JsonSchemaAdminForm | ||
search_fields = ["name"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# SOME DESCRIPTIVE TITLE. | ||
# Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER | ||
# This file is distributed under the same license as the PACKAGE package. | ||
# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR. | ||
# | ||
#, fuzzy | ||
msgid "" | ||
msgstr "" | ||
"Project-Id-Version: PACKAGE VERSION\n" | ||
"Report-Msgid-Bugs-To: \n" | ||
"POT-Creation-Date: 2025-01-07 05:36-0600\n" | ||
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" | ||
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n" | ||
"Language-Team: LANGUAGE <[email protected]>\n" | ||
"Language: \n" | ||
"MIME-Version: 1.0\n" | ||
"Content-Type: text/plain; charset=UTF-8\n" | ||
"Content-Transfer-Encoding: 8bit\n" | ||
"Plural-Forms: nplurals=2; plural=(n != 1);\n" | ||
|
||
#: models.py:10 | ||
msgid "name" | ||
msgstr "naam" | ||
|
||
#: models.py:10 | ||
msgid "Name of the json schema." | ||
msgstr "Naam van het json schema." | ||
|
||
#: models.py:14 | ||
msgid "schema" | ||
msgstr "" | ||
|
||
#: models.py:14 | ||
msgid "The schema that can be validated against." | ||
msgstr "Het schema waartegen gevalideerd kan worden." | ||
|
||
#: models.py:18 | ||
msgid "Json schema" | ||
msgstr "" | ||
|
||
#: models.py:19 | ||
msgid "Json Schemas" | ||
msgstr "Json Schema's" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
# Generated by Django 4.2.17 on 2025-01-03 16:37 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
initial = True | ||
|
||
dependencies = [] | ||
|
||
operations = [ | ||
migrations.CreateModel( | ||
name="JsonSchema", | ||
fields=[ | ||
( | ||
"id", | ||
models.BigAutoField( | ||
auto_created=True, | ||
primary_key=True, | ||
serialize=False, | ||
verbose_name="ID", | ||
), | ||
), | ||
( | ||
"name", | ||
models.CharField( | ||
help_text="Name of the json schema.", | ||
max_length=200, | ||
verbose_name="name", | ||
), | ||
), | ||
( | ||
"schema", | ||
models.JSONField( | ||
help_text="The schema that can be validated against.", | ||
verbose_name="schema", | ||
), | ||
), | ||
], | ||
options={ | ||
"verbose_name": "Json schema", | ||
"verbose_name_plural": "Json Schemas", | ||
}, | ||
), | ||
] |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
from django.core.exceptions import ValidationError | ||
from django.db import models | ||
from django.utils.translation import gettext_lazy as _ | ||
|
||
from jsonschema import Draft202012Validator, validate | ||
from jsonschema.exceptions import ( | ||
SchemaError, | ||
ValidationError as JsonSchemaValidationError, | ||
) | ||
|
||
|
||
class JsonSchema(models.Model): | ||
name = models.CharField( | ||
_("name"), help_text=_("Name of the json schema."), max_length=200 | ||
) | ||
|
||
schema = models.JSONField( | ||
_("schema"), help_text=_("The schema that can be validated against.") | ||
) | ||
|
||
class Meta: | ||
verbose_name = _("Json schema") | ||
verbose_name_plural = _("Json Schemas") | ||
|
||
def __str__(self): | ||
return self.name | ||
|
||
def clean(self): | ||
try: | ||
Draft202012Validator.check_schema(self.schema) | ||
except SchemaError as e: | ||
raise ValidationError(e.message) | ||
|
||
def validate(self, json: dict) -> None: | ||
try: | ||
validate(json, self.schema, cls=Draft202012Validator) | ||
except JsonSchemaValidationError as e: | ||
raise ValidationError(e.message) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
#!/usr/bin/env python | ||
import os | ||
import sys | ||
|
||
if __name__ == "__main__": | ||
os.environ["DJANGO_SETTINGS_MODULE"] = "testapp.settings" | ||
|
||
from django.core.management import execute_from_command_line | ||
|
||
execute_from_command_line(sys.argv) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
from django.core.exceptions import ValidationError | ||
from django.test import TestCase | ||
|
||
from django_json_schema.models import JsonSchema | ||
|
||
|
||
class TestJsonSchema(TestCase): | ||
def setUp(self): | ||
self.schema = JsonSchema.objects.create( | ||
name="schema", | ||
schema={ | ||
"type": "object", | ||
"properties": { | ||
"price": {"type": "number"}, | ||
"name": {"type": "string"}, | ||
}, | ||
"required": ["price", "name"], | ||
}, | ||
) | ||
|
||
def test_valid_json(self): | ||
self.schema.validate( | ||
{ | ||
"price": 10, | ||
"name": "test", | ||
} | ||
) | ||
|
||
def test_invalid_json(self): | ||
with self.assertRaisesMessage( | ||
ValidationError, "'price' is a required property" | ||
): | ||
self.schema.validate({"name": "Eggs"}) | ||
|
||
def test_clean_with_invalid_schema(self): | ||
self.schema.schema = {"type": []} | ||
with self.assertRaisesMessage( | ||
ValidationError, "[] is not valid under any of the given schemas" | ||
): | ||
self.schema.clean() | ||
|
||
def test_clean_with_valid_schema(self): | ||
self.schema.clean() |