Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
1
Merge Requests
1
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
nexedi
gitlab-ce
Commits
5e0a7e52
Commit
5e0a7e52
authored
Dec 09, 2020
by
Alan (Maciej) Paruszewski
Committed by
Kerri Miller
Dec 09, 2020
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add validating jsonb fields with json schema draft-07
parent
b5aee638
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
46 additions
and
2 deletions
+46
-2
app/validators/json_schema_validator.rb
app/validators/json_schema_validator.rb
+10
-1
changelogs/unreleased/263497-add-validating-json-schema-draft-7.yml
.../unreleased/263497-add-validating-json-schema-draft-7.yml
+5
-0
ee/app/models/vulnerabilities/finding.rb
ee/app/models/vulnerabilities/finding.rb
+1
-1
spec/validators/json_schema_validator_spec.rb
spec/validators/json_schema_validator_spec.rb
+30
-0
No files found.
app/validators/json_schema_validator.rb
View file @
5e0a7e52
...
@@ -12,6 +12,7 @@
...
@@ -12,6 +12,7 @@
class
JsonSchemaValidator
<
ActiveModel
::
EachValidator
class
JsonSchemaValidator
<
ActiveModel
::
EachValidator
FILENAME_ALLOWED
=
/\A[a-z0-9_-]*\Z/
.
freeze
FILENAME_ALLOWED
=
/\A[a-z0-9_-]*\Z/
.
freeze
FilenameError
=
Class
.
new
(
StandardError
)
FilenameError
=
Class
.
new
(
StandardError
)
JSON_VALIDATOR_MAX_DRAFT_VERSION
=
4
def
initialize
(
options
)
def
initialize
(
options
)
raise
ArgumentError
,
"Expected 'filename' as an argument"
unless
options
[
:filename
]
raise
ArgumentError
,
"Expected 'filename' as an argument"
unless
options
[
:filename
]
...
@@ -29,10 +30,18 @@ class JsonSchemaValidator < ActiveModel::EachValidator
...
@@ -29,10 +30,18 @@ class JsonSchemaValidator < ActiveModel::EachValidator
private
private
def
valid_schema?
(
value
)
def
valid_schema?
(
value
)
if
draft_version
>
JSON_VALIDATOR_MAX_DRAFT_VERSION
JSONSchemer
.
schema
(
Pathname
.
new
(
schema_path
)).
valid?
(
value
)
else
JSON
::
Validator
.
validate
(
schema_path
,
value
)
JSON
::
Validator
.
validate
(
schema_path
,
value
)
end
end
end
def
schema_path
def
schema_path
Rails
.
root
.
join
(
'app'
,
'validators'
,
'json_schemas'
,
"
#{
options
[
:filename
]
}
.json"
).
to_s
Rails
.
root
.
join
(
'app'
,
'validators'
,
'json_schemas'
,
"
#{
options
[
:filename
]
}
.json"
).
to_s
end
end
def
draft_version
options
[
:draft
]
||
JSON_VALIDATOR_MAX_DRAFT_VERSION
end
end
end
changelogs/unreleased/263497-add-validating-json-schema-draft-7.yml
0 → 100644
View file @
5e0a7e52
---
title
:
Add validating jsonb fields with json schema draft-07
merge_request
:
49451
author
:
type
:
added
ee/app/models/vulnerabilities/finding.rb
View file @
5e0a7e52
...
@@ -92,7 +92,7 @@ module Vulnerabilities
...
@@ -92,7 +92,7 @@ module Vulnerabilities
validates
:metadata_version
,
presence:
true
validates
:metadata_version
,
presence:
true
validates
:raw_metadata
,
presence:
true
validates
:raw_metadata
,
presence:
true
validates
:details
,
json_schema:
{
filename:
'vulnerability_finding_details'
}
validates
:details
,
json_schema:
{
filename:
'vulnerability_finding_details'
,
draft:
7
}
delegate
:name
,
:external_id
,
to: :scanner
,
prefix:
true
,
allow_nil:
true
delegate
:name
,
:external_id
,
to: :scanner
,
prefix:
true
,
allow_nil:
true
...
...
spec/validators/json_schema_validator_spec.rb
View file @
5e0a7e52
...
@@ -29,6 +29,36 @@ RSpec.describe JsonSchemaValidator do
...
@@ -29,6 +29,36 @@ RSpec.describe JsonSchemaValidator do
expect
(
build_report_result
.
errors
.
full_messages
).
to
eq
([
"Data must be a valid json schema"
])
expect
(
build_report_result
.
errors
.
full_messages
).
to
eq
([
"Data must be a valid json schema"
])
end
end
end
end
context
'when draft is > 4'
do
let
(
:validator
)
{
described_class
.
new
(
attributes:
[
:data
],
filename:
"build_report_result_data"
,
draft:
6
)
}
it
'uses JSONSchemer to perform validations'
do
expect
(
JSONSchemer
).
to
receive
(
:schema
).
with
(
Pathname
.
new
(
Rails
.
root
.
join
(
'app'
,
'validators'
,
'json_schemas'
,
'build_report_result_data.json'
).
to_s
)).
and_call_original
subject
end
end
context
'when draft is <= 4'
do
let
(
:validator
)
{
described_class
.
new
(
attributes:
[
:data
],
filename:
"build_report_result_data"
,
draft:
4
)
}
it
'uses JSON::Validator to perform validations'
do
expect
(
JSON
::
Validator
).
to
receive
(
:validate
).
with
(
Rails
.
root
.
join
(
'app'
,
'validators'
,
'json_schemas'
,
'build_report_result_data.json'
).
to_s
,
build_report_result
.
data
)
subject
end
end
context
'when draft value is not provided'
do
let
(
:validator
)
{
described_class
.
new
(
attributes:
[
:data
],
filename:
"build_report_result_data"
)
}
it
'uses JSON::Validator to perform validations'
do
expect
(
JSON
::
Validator
).
to
receive
(
:validate
).
with
(
Rails
.
root
.
join
(
'app'
,
'validators'
,
'json_schemas'
,
'build_report_result_data.json'
).
to_s
,
build_report_result
.
data
)
subject
end
end
end
end
context
'when filename is not set'
do
context
'when filename is not set'
do
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment