29 #ifndef INCLUDE_OLA_WEB_JSONSCHEMA_H_ 30 #define INCLUDE_OLA_WEB_JSONSCHEMA_H_ 51 class SchemaDefinitions;
68 virtual bool IsValid()
const = 0;
82 virtual void SetSchema(
const std::string &schema) = 0;
87 virtual void SetId(
const std::string &
id) = 0;
92 virtual void SetTitle(
const std::string &title) = 0;
131 virtual bool IsValid()
const {
return m_is_valid; }
137 virtual void Visit(
const JsonBool&) {
141 virtual void Visit(
const JsonNull&) {
157 virtual void Visit(
const JsonUInt&) {
165 virtual void Visit(
const JsonInt&) {
182 void SetSchema(
const std::string &schema);
187 void SetId(
const std::string &
id);
192 void SetTitle(
const std::string &title);
215 void AddEnumValue(
const JsonValue *value);
220 std::string m_schema;
223 std::string m_description;
224 std::auto_ptr<const JsonValue> m_default_value;
225 std::vector<const JsonValue*> m_enums;
230 virtual void ExtendSchema(
JsonObject *schema)
const {
257 const std::string &schema);
269 void Visit(
const JsonInt &value);
275 void SetSchema(
const std::string &schema);
276 void SetId(
const std::string &
id);
277 void SetTitle(
const std::string &title);
284 const std::string m_schema;
287 template <
typename T>
288 void Validate(
const T &value);
302 unsigned int min_length;
331 void Visit(
const JsonBool &value) { m_is_valid = CheckEnums(value); }
344 void Visit(
const JsonNull &value) { m_is_valid = CheckEnums(value); }
360 virtual void ExtendSchema(
JsonObject *schema)
const = 0;
369 : m_multiple_of(value) {
377 schema->
AddValue(
"multipleOf", m_multiple_of->Clone());
381 std::auto_ptr<const JsonNumber> m_multiple_of;
396 m_has_exclusive(true),
397 m_is_exclusive(is_exclusive) {
406 m_has_exclusive(false),
407 m_is_exclusive(false) {
411 return (m_has_exclusive && m_is_exclusive) ? value < *m_limit
416 schema->
AddValue(
"maximum", m_limit->Clone());
417 if (m_has_exclusive) {
418 schema->
Add(
"exclusiveMaximum", m_is_exclusive);
423 std::auto_ptr<const JsonNumber> m_limit;
424 bool m_has_exclusive, m_is_exclusive;
439 m_has_exclusive(true),
440 m_is_exclusive(is_exclusive) {
449 m_has_exclusive(false),
450 m_is_exclusive(false) {
454 return (m_has_exclusive && m_is_exclusive) ? value > *m_limit
459 schema->
AddValue(
"minimum", m_limit->Clone());
460 if (m_has_exclusive) {
461 schema->
Add(
"exclusiveMinimum", m_is_exclusive);
466 std::auto_ptr<const JsonNumber> m_limit;
467 bool m_has_exclusive, m_is_exclusive;
496 std::vector<NumberConstraint*> m_constraints;
530 : max_properties(-1),
532 has_required_properties(
false),
533 has_allow_additional_properties(
false),
534 allow_additional_properties(
false) {
537 void SetRequiredProperties(
538 const std::set<std::string> &required_properties_arg) {
539 required_properties = required_properties_arg;
540 has_required_properties =
true;
543 void SetAdditionalProperties(
bool allow_additional) {
544 has_allow_additional_properties =
true;
545 allow_additional_properties = allow_additional;
549 unsigned int min_properties;
550 bool has_required_properties;
551 std::set<std::string> required_properties;
552 bool has_allow_additional_properties;
553 bool allow_additional_properties;
581 void AddSchemaDependency(
const std::string &property,
593 void AddPropertyDependency(
const std::string &property,
594 const std::set<std::string> &properties);
598 void VisitProperty(
const std::string &property,
const JsonValue &value);
601 typedef std::set<std::string> StringSet;
602 typedef std::map<std::string, ValidatorInterface*> PropertyValidators;
603 typedef std::map<std::string, ValidatorInterface*> SchemaDependencies;
604 typedef std::map<std::string, StringSet> PropertyDependencies;
608 PropertyValidators m_property_validators;
609 std::auto_ptr<ValidatorInterface> m_additional_property_validator;
610 PropertyDependencies m_property_dependencies;
611 SchemaDependencies m_schema_dependencies;
613 StringSet m_seen_properties;
632 : m_validator(validator) {
637 m_validator_list(*validators) {
645 const ValidatorList& Validators()
const {
return m_validator_list; }
648 std::auto_ptr<ValidatorInterface> m_validator;
660 : m_allowed(allow_additional),
666 m_validator(validator) {
670 bool AllowAdditional()
const {
return m_allowed; }
674 std::auto_ptr<ValidatorInterface> m_validator;
683 unique_items(
false) {
687 unsigned int min_items;
706 typedef std::deque<ValidatorInterface*> ValidatorQueue;
708 const std::auto_ptr<Items> m_items;
709 const std::auto_ptr<AdditionalItems> m_additional_items;
713 std::auto_ptr<WildcardValidator> m_wildcard_validator;
733 ValidatorQueue m_item_validators;
736 template <
typename T>
737 void ValidateItem(
const T &item);
743 ArrayElementValidator* ConstructElementValidator()
const;
796 void Visit(
const JsonInt &value) {
809 std::string m_keyword;
814 virtual void Validate(
const JsonValue &value) = 0;
889 m_validator(validator) {
924 void Visit(
const JsonInt &value) {
937 std::auto_ptr<ValidatorInterface> m_validator;
956 bool HasDefinitions()
const {
return !m_validators.empty(); }
959 typedef std::map<std::string, ValidatorInterface*> SchemaMap;
961 SchemaMap m_validators;
977 std::string SchemaURI()
const;
993 static JsonSchema* FromString(
const std::string& schema_string,
997 std::string m_schema_uri;
998 std::auto_ptr<ValidatorInterface> m_root_validator;
999 std::auto_ptr<SchemaDefinitions> m_schema_defs;
1011 #endif // INCLUDE_OLA_WEB_JSONSCHEMA_H_ The base class for JSON values.
Definition: Json.h:119
void Add(const std::string &key, const std::string &value)
Add a key to string mapping.
Definition: Json.cpp:607
AllOfValidator(ValidatorList *validators)
Definition: JsonSchema.h:827
A signed 64bit int value.
Definition: Json.h:577
An unsigned 32bit int value.
Definition: Json.h:379
void AddValue(const std::string &key, JsonValue *value)
Set the key to the supplied JsonValue.
Definition: Json.cpp:667
#define DISALLOW_COPY_AND_ASSIGN(TypeName)
Creates dummy copy constructor and assignment operator declarations.
Definition: Macro.h:44
A raw value, useful if you want to cheat.
Definition: Json.h:842
A validator which ensures all child validators pass (allOf).
Definition: JsonSchema.h:820
MinimumConstraint(const JsonNumber *limit, bool is_exclusive)
Create a new MaximumConstraint.
Definition: JsonSchema.h:437
Definition: JsonSchema.h:296
Definition: JsonTypes.h:51
Definition: JsonSchema.h:679
The base class for Json BaseValidators. All Visit methods return false.
Definition: JsonSchema.h:121
virtual void SetId(const std::string &id)=0
Set the id property for this validator.
The validator for JsonArray.
Definition: JsonSchema.h:623
virtual bool MultipleOf(const JsonNumber &other) const =0
Checks if the remainder if non-0;.
Definition: JsonSchema.h:657
A class used to visit Json values within a JsonObject.
Definition: Json.h:1215
Definition: Preferences.h:43
virtual JsonObject * GetSchema() const =0
Returns the Schema as a JsonObject.
JsonType
The type of JSON data element.
Definition: JsonTypes.h:44
The base class for validators that operate with a list of child validators (allOf, anyOf, oneOf).
Definition: JsonSchema.h:753
AnyOfValidator(ValidatorList *validators)
Definition: JsonSchema.h:849
virtual const JsonValue * GetDefaultValue() const =0
Return the default value for this validator.
MaximumConstraint(const JsonNumber *limit)
Create a new MaximumConstraint.
Definition: JsonSchema.h:404
The null value.
Definition: Json.h:811
Definition: JsonSchema.h:946
Enums used to identfy JSON types.
Definition: JsonTypes.h:47
A JSON object. JSON Objects are key : value mappings, similar to dictionaries in Python.
Definition: Json.h:890
void STLDeleteElements(T *sequence)
Delete the elements of a Sequence.
Definition: STLUtils.h:99
A Bool value.
Definition: Json.h:768
An array of JSON values. Arrays in JSON can contain values of different types.
Definition: Json.h:1033
virtual void SetDefaultValue(const JsonValue *value)=0
Set the default value for this validator.
Definition: JsonSchema.h:629
Enforces a maximum.
Definition: JsonSchema.h:387
The base class for constraints that can be applies to the Json number type.
Definition: JsonSchema.h:354
virtual bool IsValid() const =0
Check if the value was valid according to this validator.
A reference validator holds a pointer to another schema.
Definition: JsonSchema.h:249
The wildcard validator matches everything. This corresponds to the empty schema, i.e. {}.
Definition: JsonSchema.h:239
Confirms the valid is a multiple of the specified value.
Definition: JsonSchema.h:366
A string value.
Definition: Json.h:279
virtual void SetTitle(const std::string &title)=0
Set the title property for this validator.
virtual bool IsValid() const
Check if the value was valid according to this validator.
Definition: JsonSchema.h:131
std::vector< ValidatorInterface * > ValidatorList
a list of Validators.
Definition: JsonSchema.h:61
The validator for JsonString.
Definition: JsonSchema.h:294
An unsigned 64bit int value.
Definition: Json.h:510
The validator for JsonNull.
Definition: JsonSchema.h:340
Helper functions for STL classes.
A validator which ensures at only one of the child validators pass (oneOf).
Definition: JsonSchema.h:864
A signed 32bit int value.
Definition: Json.h:445
Definition: JsonTypes.h:52
The validator for Json integers.
Definition: JsonSchema.h:473
The interface Json Schema Validators.
Definition: JsonSchema.h:56
Definition: JsonSchema.h:528
A double value.
Definition: Json.h:649
Definition: JsonTypes.h:48
Definition: JsonTypes.h:46
virtual void SetDescription(const std::string &title)=0
Set the description property for this validator.
MaximumConstraint(const JsonNumber *limit, bool is_exclusive)
Create a new MaximumConstraint.
Definition: JsonSchema.h:394
The namespace containing all OLA symbols.
Definition: Credentials.cpp:44
OneOfValidator(ValidatorList *validators)
Definition: JsonSchema.h:871
MinimumConstraint(const JsonNumber *limit)
Create a new MaximumConstraint.
Definition: JsonSchema.h:447
Definition: JsonTypes.h:49
The validator for Json numbers.
Definition: JsonSchema.h:509
A JsonHandlerInterface implementation that builds a parse tree.
Definition: JsonSchema.h:970
The validator for JsonObject.
Definition: JsonSchema.h:526
virtual void SetSchema(const std::string &schema)=0
Set the $schema property for this validator.
JsonNumber is the base class for various integer / number classes.
Definition: Json.h:324
The const interface for the JsonValueVisitor class.
Definition: Json.h:99
Basic data types used to represent elements in a JSON document.
bool IsValid() const
Check if the value was valid according to this validator.
Definition: JsonSchema.h:243
A validator which ensures at least one of the child validators pass (anyOf).
Definition: JsonSchema.h:842
Enforces a minimum.
Definition: JsonSchema.h:430
The validator for JsonBool.
Definition: JsonSchema.h:327
A validator that inverts the result of the child (not).
Definition: JsonSchema.h:885