Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
H
hattivatti
Manage
Activity
Members
Labels
Plan
Issues
0
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
0
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
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
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
GDP-Public
hattivatti
Commits
54bc3f98
Commit
54bc3f98
authored
1 year ago
by
Benjamin Wingfield
Browse files
Options
Downloads
Patches
Plain Diff
set up schema submodule with local resolver
parent
0a3fd9c8
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/request/mod.rs
+2
-0
2 additions, 0 deletions
src/request/mod.rs
src/request/schema.rs
+54
-0
54 additions, 0 deletions
src/request/schema.rs
with
56 additions
and
0 deletions
src/request/mod.rs
0 → 100644
+
2
−
0
View file @
54bc3f98
pub
mod
message
;
pub
mod
schema
;
\ No newline at end of file
This diff is collapsed.
Click to expand it.
src/request/schema.rs
0 → 100644
+
54
−
0
View file @
54bc3f98
use
std
::
fs
;
use
std
::
path
::{
Path
,
PathBuf
};
use
std
::
sync
::
Arc
;
use
anyhow
::
anyhow
;
use
jsonschema
::{
JSONSchema
,
SchemaResolver
,
SchemaResolverError
};
use
log
::
warn
;
use
serde_json
::{
json
,
Value
};
use
url
::
Url
;
pub
fn
load_schema
(
schema_dir
:
&
Path
)
->
JSONSchema
{
let
schema_json
=
read_schema
(
schema_dir
);
compile_schema
(
&
schema_json
,
schema_dir
)
}
fn
read_schema
(
schema_dir
:
&
Path
)
->
Value
{
let
schema_path
=
schema_dir
.join
(
"api.json"
);
read_json_from_path
(
schema_path
.as_path
())
}
fn
read_json_from_path
(
path
:
&
Path
)
->
Value
{
let
json_string
=
fs
::
read_to_string
(
path
)
.expect
(
"Valid path"
);
serde_json
::
from_str
(
&
json_string
)
.expect
(
"Valid JSON"
)
}
fn
compile_schema
(
schema
:
&
Value
,
schema_dir
:
&
Path
)
->
JSONSchema
{
let
resolver
=
LocalResolver
{
schema_dir
:
PathBuf
::
from
(
schema_dir
)
};
JSONSchema
::
options
()
.with_resolver
(
resolver
)
.compile
(
schema
)
.expect
(
"Valid schema"
)
}
/*
Set up a resolver that will work with local JSON schema
The local schema contain relative references to local files in the same directory
In the future we should change to online schemas with absolute references
*/
struct
LocalResolver
{
schema_dir
:
PathBuf
,
}
impl
SchemaResolver
for
LocalResolver
{
fn
resolve
(
&
self
,
_root_schema
:
&
Value
,
url
:
&
Url
,
_original_reference
:
&
str
)
->
Result
<
Arc
<
Value
>
,
SchemaResolverError
>
{
match
url
.scheme
()
{
"json-schema"
=>
{
let
local_schema_path
:
PathBuf
=
self
.schema_dir
.join
(
_original_reference
);
Ok
(
Arc
::
new
(
read_json_from_path
(
local_schema_path
.as_path
())))
}
_
=>
Err
(
anyhow!
(
"scheme is not supported"
))
}
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
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