Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
A
AKPlanning
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Felix Blanke
AKPlanning
Commits
73a06f90
Commit
73a06f90
authored
1 month ago
by
Felix Blanke
Browse files
Options
Downloads
Patches
Plain Diff
Add schema validator as utils
parent
6d9d8dca
No related branches found
No related tags found
1 merge request
!25
Feature: Use JSON schema
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
AKModel/utils.py
+28
-0
28 additions, 0 deletions
AKModel/utils.py
with
28 additions
and
0 deletions
AKModel/utils.py
0 → 100644
+
28
−
0
View file @
73a06f90
import
json
from
pathlib
import
Path
from
jsonschema
import
Draft202012Validator
from
jsonschema.protocols
import
Validator
from
referencing
import
Registry
,
Resource
from
AKPlanning
import
settings
def
construct_schema_validator
(
schema
:
str
|
dict
)
->
Validator
:
"""
Construct a validator for a JSON schema.
In particular, all schemas from the
'
schemas
'
directory
are loaded into the registry.
"""
schema_base_path
=
Path
(
settings
.
BASE_DIR
)
/
"
schemas
"
resources
=
[]
for
schema_path
in
schema_base_path
.
glob
(
"
**/*.schema.json
"
):
with
schema_path
.
open
(
"
r
"
)
as
ff
:
res
=
Resource
.
from_contents
(
json
.
load
(
ff
))
resources
.
append
((
res
.
id
(),
res
))
registry
=
Registry
().
with_resources
(
resources
)
if
isinstance
(
schema
,
str
):
with
(
schema_base_path
/
schema
).
open
(
"
r
"
)
as
ff
:
schema
=
json
.
load
(
ff
)
return
Draft202012Validator
(
schema
=
schema
,
registry
=
registry
)
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment