Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
Matrix
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Container Registry
Model registry
Operate
Environments
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
TeDomum
Matrix
Commits
0292d991
Commit
0292d991
authored
10 years ago
by
Erik Johnston
Browse files
Options
Downloads
Patches
Plain Diff
Add EventValidator module
parent
a8e565ec
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
synapse/api/events/validator.py
+81
-0
81 additions, 0 deletions
synapse/api/events/validator.py
with
81 additions
and
0 deletions
synapse/api/events/validator.py
0 → 100644
+
81
−
0
View file @
0292d991
# -*- coding: utf-8 -*-
# Copyright 2014 OpenMarket Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from
synapse.api.errors
import
SynapseError
,
Codes
class
EventValidator
(
object
):
def
__init__
(
self
,
hs
):
pass
def
validate
(
self
,
event
):
"""
Checks the given JSON content abides by the rules of the template.
Args:
content : A JSON object to check.
raises: True to raise a SynapseError if the check fails.
Returns:
True if the content passes the template. Returns False if the check
fails and raises=False.
Raises:
SynapseError if the check fails and raises=True.
"""
# recursively call to inspect each layer
err_msg
=
self
.
_check_json_template
(
event
.
content
,
event
.
get_content_template
()
)
if
err_msg
:
raise
SynapseError
(
400
,
err_msg
,
Codes
.
BAD_JSON
)
else
:
return
True
def
_check_json_template
(
self
,
content
,
template
):
"""
Check content and template matches.
If the template is a dict, each key in the dict will be validated with
the content, else it will just compare the types of content and
template. This basic type check is required because this function will
be recursively called and could be called with just strs or ints.
Args:
content: The content to validate.
template: The validation template.
Returns:
str: An error message if the validation fails, else None.
"""
if
type
(
content
)
!=
type
(
template
):
return
"
Mismatched types: %s
"
%
template
if
type
(
template
)
==
dict
:
for
key
in
template
:
if
key
not
in
content
:
return
"
Missing %s key
"
%
key
if
type
(
content
[
key
])
!=
type
(
template
[
key
]):
return
"
Key %s is of the wrong type (got %s, want %s)
"
%
(
key
,
type
(
content
[
key
]),
type
(
template
[
key
]))
if
type
(
content
[
key
])
==
dict
:
# we must go deeper
msg
=
self
.
_check_json
(
content
[
key
],
template
[
key
])
if
msg
:
return
msg
elif
type
(
content
[
key
])
==
list
:
# make sure each item type in content matches the template
for
entry
in
content
[
key
]:
msg
=
self
.
_check_json
(
entry
,
template
[
key
][
0
])
if
msg
:
return
msg
\ No newline at end of file
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