File: shortcuts.py

package info (click to toggle)
json-schema-validator 2.3.1-3
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, stretch
  • size: 308 kB
  • ctags: 129
  • sloc: python: 2,256; makefile: 21; sh: 2
file content (57 lines) | stat: -rw-r--r-- 2,001 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# Copyright (C) 2010, 2011 Linaro Limited
#
# Author: Zygmunt Krynicki <zygmunt.krynicki@linaro.org>
#
# This file is part of json-schema-validator.
#
# json-schema-validator is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License version 3
# as published by the Free Software Foundation
#
# json-schema-validator is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with json-schema-validator.  If not, see <http://www.gnu.org/licenses/>.

"""
One liners that make the code shorter
"""

import simplejson

from json_schema_validator.schema import Schema
from json_schema_validator.validator import Validator


def validate(schema_text, data_text):
    """
    Validate specified JSON text (data_text) with specified schema (schema
    text). Both are converted to JSON objects with :func:`simplesjon.loads`.

    :param schema_text:
        Text of the JSON schema to check against
    :type schema_text:
        :class:`str`
    :param data_text:
        Text of the JSON object to check
    :type data_text:
        :class:`str`
    :returns:
        Same as :meth:`json_schema_validator.validator.Validator.validate`
    :raises:
        Whatever may be raised by simplejson (in particular
        :class:`simplejson.decoder.JSONDecoderError`, a subclass of :class:`ValueError`)
    :raises:
        Whatever may be raised by
        :meth:`json_schema_validator.validator.Validator.validate`. In particular
        :class:`json_schema_validator.errors.ValidationError` and
        :class:`json_schema_validator.errors.SchemaError`


    """
    schema = Schema(simplejson.loads(schema_text))
    data = simplejson.loads(data_text)
    return Validator.validate(schema, data)