1 | /* |
2 | * Copyright 2015-present Facebook, Inc. |
3 | * |
4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | * you may not use this file except in compliance with the License. |
6 | * You may obtain a copy of the License at |
7 | * |
8 | * http://www.apache.org/licenses/LICENSE-2.0 |
9 | * |
10 | * Unless required by applicable law or agreed to in writing, software |
11 | * distributed under the License is distributed on an "AS IS" BASIS, |
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 | * See the License for the specific language governing permissions and |
14 | * limitations under the License. |
15 | */ |
16 | |
17 | #pragma once |
18 | |
19 | #include <folly/ExceptionWrapper.h> |
20 | #include <folly/Range.h> |
21 | #include <folly/dynamic.h> |
22 | |
23 | /** |
24 | * Validation according to the draft v4 standard: http://json-schema.org/ |
25 | * |
26 | * If your schema is invalid, then it won't validate anything. For example, if |
27 | * you set "type": "invalid_type" in your schema, then it won't check for any |
28 | * type, as if you had left that property out. If you want to make sure your |
29 | * schema is valid, you can optionally validate it first according to the |
30 | * metaschema. |
31 | * |
32 | * Limitations: |
33 | * - We don't support fetching schemas via HTTP. |
34 | * - We don't support remote $refs. |
35 | * - We don't support $ref via id (only by path). |
36 | * - We don't support UTF-8 for string lengths, i.e. we will count bytes for |
37 | * schemas that use minLength/maxLength. |
38 | */ |
39 | |
40 | namespace folly { |
41 | namespace jsonschema { |
42 | |
43 | /** |
44 | * Interface for a schema validator. |
45 | */ |
46 | struct Validator { |
47 | virtual ~Validator() = 0; |
48 | |
49 | /** |
50 | * Check whether the given value passes the schema. Throws if it fails. |
51 | */ |
52 | virtual void validate(const dynamic& value) const = 0; |
53 | |
54 | /** |
55 | * Check whether the given value passes the schema. Returns an |
56 | * exception_wrapper indicating success or what the failure was. |
57 | */ |
58 | virtual exception_wrapper try_validate(const dynamic& value) const |
59 | noexcept = 0; |
60 | }; |
61 | |
62 | /** |
63 | * Make a validator that can be used to check various json. Thread-safe. |
64 | */ |
65 | std::unique_ptr<Validator> makeValidator(const dynamic& schema); |
66 | |
67 | /** |
68 | * Makes a validator for schemas. You should probably check your schema with |
69 | * this before you use makeValidator(). |
70 | */ |
71 | std::shared_ptr<Validator> makeSchemaValidator(); |
72 | } // namespace jsonschema |
73 | } // namespace folly |
74 | |