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.
- Loading branch information
Showing
10 changed files
with
195 additions
and
9 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,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,26 @@ | ||
from django.core.exceptions import ValidationError | ||
from django.db import models | ||
from django.utils.translation import gettext_lazy as _ | ||
|
||
from jsonschema import validate | ||
from jsonschema.exceptions import 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 validate(self, json: dict) -> None: | ||
try: | ||
validate(json, self.schema) | ||
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 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,33 @@ | ||
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"}) |