xref: /6.0.3/subjson/subdoc/validate.h (revision 64235ee1)
1*64235ee1SMark Nunberg /* -*- Mode: C++; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2*64235ee1SMark Nunberg /*
3*64235ee1SMark Nunberg *     Copyright 2015 Couchbase, Inc
4*64235ee1SMark Nunberg *
5*64235ee1SMark Nunberg *   Licensed under the Apache License, Version 2.0 (the "License");
6*64235ee1SMark Nunberg *   you may not use this file except in compliance with the License.
7*64235ee1SMark Nunberg *   You may obtain a copy of the License at
8*64235ee1SMark Nunberg *
9*64235ee1SMark Nunberg *       http://www.apache.org/licenses/LICENSE-2.0
10*64235ee1SMark Nunberg *
11*64235ee1SMark Nunberg *   Unless required by applicable law or agreed to in writing, software
12*64235ee1SMark Nunberg *   distributed under the License is distributed on an "AS IS" BASIS,
13*64235ee1SMark Nunberg *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14*64235ee1SMark Nunberg *   See the License for the specific language governing permissions and
15*64235ee1SMark Nunberg *   limitations under the License.
16*64235ee1SMark Nunberg */
17*64235ee1SMark Nunberg 
18*64235ee1SMark Nunberg #ifndef SUBDOC_VALIDATE_H
19*64235ee1SMark Nunberg #define SUBDOC_VALIDATE_H
20*64235ee1SMark Nunberg 
21*64235ee1SMark Nunberg namespace Subdoc {
22*64235ee1SMark Nunberg class Validator {
23*64235ee1SMark Nunberg public:
24*64235ee1SMark Nunberg     /// Possible values to pass as the 'flags' argument to ::validate().
25*64235ee1SMark Nunberg     /// The `PARENT_` and `VALUE_` flags may be bitwise-OR'd together.
26*64235ee1SMark Nunberg     enum Options {
27*64235ee1SMark Nunberg         /// Value should be valid in its own context
28*64235ee1SMark Nunberg         PARENT_NONE = 0x00,
29*64235ee1SMark Nunberg 
30*64235ee1SMark Nunberg         /// Value should be a valid series of array elements
31*64235ee1SMark Nunberg         PARENT_ARRAY = 0x02,
32*64235ee1SMark Nunberg 
33*64235ee1SMark Nunberg         /// Value should be a valid dictionary value
34*64235ee1SMark Nunberg         PARENT_DICT = 0x03,
35*64235ee1SMark Nunberg 
36*64235ee1SMark Nunberg         /// No constraint on the value type
37*64235ee1SMark Nunberg         VALUE_ANY = 0x000,
38*64235ee1SMark Nunberg 
39*64235ee1SMark Nunberg         /// Value must be a single item (not series)
40*64235ee1SMark Nunberg         VALUE_SINGLE = 0x100,
41*64235ee1SMark Nunberg 
42*64235ee1SMark Nunberg         /// Value must be a JSON primitive (not a container)
43*64235ee1SMark Nunberg         VALUE_PRIMITIVE = 0x200,
44*64235ee1SMark Nunberg 
45*64235ee1SMark Nunberg     };
46*64235ee1SMark Nunberg 
47*64235ee1SMark Nunberg     enum Status {
48*64235ee1SMark Nunberg         /// Requested a primitive, but value is not a primitive
49*64235ee1SMark Nunberg         ENOTPRIMITIVE = JSONSL_ERROR_GENERIC + 1,
50*64235ee1SMark Nunberg         /// Buffer contains more than a single top-level object
51*64235ee1SMark Nunberg         EMULTIELEM,
52*64235ee1SMark Nunberg         /// A full JSON value could not be obtained
53*64235ee1SMark Nunberg         EPARTIAL,
54*64235ee1SMark Nunberg         /// Max depth exceeded
55*64235ee1SMark Nunberg         ETOODEEP = JSONSL_ERROR_LEVELS_EXCEEDED
56*64235ee1SMark Nunberg     };
57*64235ee1SMark Nunberg 
58*64235ee1SMark Nunberg     /**
59*64235ee1SMark Nunberg      * Convenience function to scan an item and see if it's JSON.
60*64235ee1SMark Nunberg      *
61*64235ee1SMark Nunberg      * @param s Buffer to check
62*64235ee1SMark Nunberg      * @param n Size of buffer
63*64235ee1SMark Nunberg      * @param jsn Parser. If NULL, one will be allocated and freed internally
64*64235ee1SMark Nunberg      * @param maxdepth The maximum allowable depth of the value. This should
65*64235ee1SMark Nunberg      *        less than COMPONENTS_ALLOC
66*64235ee1SMark Nunberg      * @param mode The context in which the value should be checked. This is one of
67*64235ee1SMark Nunberg      * the @ref Option constants. The mode may also be combined
68*64235ee1SMark Nunberg      * with one of the flags to add additional constraints on the added value.
69*64235ee1SMark Nunberg      *
70*64235ee1SMark Nunberg      * @return JSONSL_ERROR_SUCCESS if JSON, error code otherwise.
71*64235ee1SMark Nunberg      */
72*64235ee1SMark Nunberg     static int validate(const char *s, size_t n, jsonsl_t jsn, int maxdepth = -1, int flags = 0);
73*64235ee1SMark Nunberg 
74*64235ee1SMark Nunberg     static int validate(const std::string& s, jsonsl_t jsn, int maxdepth = -1, int flags = 0) {
75*64235ee1SMark Nunberg         return validate(s.c_str(), s.size(), jsn, maxdepth, flags);
76*64235ee1SMark Nunberg     }
77*64235ee1SMark Nunberg 
78*64235ee1SMark Nunberg     static int validate(const Loc& loc, jsonsl_t jsn, int maxdepth = -1, int flags = 0) {
79*64235ee1SMark Nunberg         return validate(loc.at, loc.length, jsn, maxdepth, flags);
80*64235ee1SMark Nunberg     }
81*64235ee1SMark Nunberg 
82*64235ee1SMark Nunberg     static const char *errstr(int);
83*64235ee1SMark Nunberg 
84*64235ee1SMark Nunberg private:
85*64235ee1SMark Nunberg     static const int VALUE_MASK = 0xFF00;
86*64235ee1SMark Nunberg     static const int PARENT_MASK = 0xFF;
87*64235ee1SMark Nunberg };
88*64235ee1SMark Nunberg }
89*64235ee1SMark Nunberg #endif
90