/*
- * Copyright 2016 Facebook, Inc.
+ * Copyright 2017 Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
#include <folly/Conv.h>
#include <folly/Memory.h>
#include <folly/Optional.h>
-#include <folly/String.h>
#include <folly/Singleton.h>
+#include <folly/String.h>
#include <folly/json.h>
namespace folly {
// We break apart the constructor and actually loading the schema so that
// we can handle the case where a schema refers to itself, e.g. via
// "$ref": "#".
- auto v = make_unique<SchemaValidator>();
+ auto v = std::make_unique<SchemaValidator>();
v->loadSchema(context, schema);
return v;
}
if (value.type() != type_) {
return none;
}
- if (!Comparison()(length_, value.size())) {
+ if (!Comparison()(length_, int64_t(value.size()))) {
return makeError("different length string/array/object", value);
}
return none;
errors.emplace_back(*se);
}
}
- const int success = validators_.size() - errors.size();
+ const auto success = validators_.size() - errors.size();
if (success == 0) {
return makeError("at least one valid schema", value);
} else if (success > 1 && type_ == Type::EXACTLY_ONE) {
if (p->isString() && p->stringPiece()[0] == '#') {
auto it = context.refs.find(p->getString());
if (it != context.refs.end()) {
- validators_.emplace_back(make_unique<RefValidator>(it->second));
+ validators_.emplace_back(std::make_unique<RefValidator>(it->second));
return;
}
s = s->get_ptr(pos);
continue;
}
- } catch (const std::range_error& e) {
+ } catch (const std::range_error&) {
// ignore
}
}
// future references to it will just see that pointer and won't try to
// keep parsing further.
if (s) {
- auto v = make_unique<SchemaValidator>();
+ auto v = std::make_unique<SchemaValidator>();
context.refs[p->getString()] = v.get();
v->loadSchema(context, *s);
validators_.emplace_back(std::move(v));
// Numeric validators
if (const auto* p = schema.get_ptr("multipleOf")) {
- validators_.emplace_back(make_unique<MultipleOfValidator>(*p));
+ validators_.emplace_back(std::make_unique<MultipleOfValidator>(*p));
}
if (const auto* p = schema.get_ptr("maximum")) {
- validators_.emplace_back(
- make_unique<ComparisonValidator>(*p,
- schema.get_ptr("exclusiveMaximum"),
- ComparisonValidator::Type::MAX));
+ validators_.emplace_back(std::make_unique<ComparisonValidator>(
+ *p,
+ schema.get_ptr("exclusiveMaximum"),
+ ComparisonValidator::Type::MAX));
}
if (const auto* p = schema.get_ptr("minimum")) {
- validators_.emplace_back(
- make_unique<ComparisonValidator>(*p,
- schema.get_ptr("exclusiveMinimum"),
- ComparisonValidator::Type::MIN));
+ validators_.emplace_back(std::make_unique<ComparisonValidator>(
+ *p,
+ schema.get_ptr("exclusiveMinimum"),
+ ComparisonValidator::Type::MIN));
}
// String validators
if (const auto* p = schema.get_ptr("maxLength")) {
validators_.emplace_back(
- make_unique<SizeValidator<std::greater_equal<int64_t>>>(
+ std::make_unique<SizeValidator<std::greater_equal<int64_t>>>(
*p, dynamic::Type::STRING));
}
if (const auto* p = schema.get_ptr("minLength")) {
validators_.emplace_back(
- make_unique<SizeValidator<std::less_equal<int64_t>>>(
+ std::make_unique<SizeValidator<std::less_equal<int64_t>>>(
*p, dynamic::Type::STRING));
}
if (const auto* p = schema.get_ptr("pattern")) {
- validators_.emplace_back(make_unique<StringPatternValidator>(*p));
+ validators_.emplace_back(std::make_unique<StringPatternValidator>(*p));
}
// Array validators
const auto* additionalItems = schema.get_ptr("additionalItems");
if (items || additionalItems) {
validators_.emplace_back(
- make_unique<ArrayItemsValidator>(context, items, additionalItems));
+ std::make_unique<ArrayItemsValidator>(context, items, additionalItems));
}
if (const auto* p = schema.get_ptr("maxItems")) {
validators_.emplace_back(
- make_unique<SizeValidator<std::greater_equal<int64_t>>>(
+ std::make_unique<SizeValidator<std::greater_equal<int64_t>>>(
*p, dynamic::Type::ARRAY));
}
if (const auto* p = schema.get_ptr("minItems")) {
validators_.emplace_back(
- make_unique<SizeValidator<std::less_equal<int64_t>>>(
+ std::make_unique<SizeValidator<std::less_equal<int64_t>>>(
*p, dynamic::Type::ARRAY));
}
if (const auto* p = schema.get_ptr("uniqueItems")) {
- validators_.emplace_back(make_unique<ArrayUniqueValidator>(*p));
+ validators_.emplace_back(std::make_unique<ArrayUniqueValidator>(*p));
}
// Object validators
const auto* patternProperties = schema.get_ptr("patternProperties");
const auto* additionalProperties = schema.get_ptr("additionalProperties");
if (properties || patternProperties || additionalProperties) {
- validators_.emplace_back(make_unique<PropertiesValidator>(
+ validators_.emplace_back(std::make_unique<PropertiesValidator>(
context, properties, patternProperties, additionalProperties));
}
if (const auto* p = schema.get_ptr("maxProperties")) {
validators_.emplace_back(
- make_unique<SizeValidator<std::greater_equal<int64_t>>>(
+ std::make_unique<SizeValidator<std::greater_equal<int64_t>>>(
*p, dynamic::Type::OBJECT));
}
if (const auto* p = schema.get_ptr("minProperties")) {
validators_.emplace_back(
- make_unique<SizeValidator<std::less_equal<int64_t>>>(
+ std::make_unique<SizeValidator<std::less_equal<int64_t>>>(
*p, dynamic::Type::OBJECT));
}
if (const auto* p = schema.get_ptr("required")) {
- validators_.emplace_back(make_unique<RequiredValidator>(*p));
+ validators_.emplace_back(std::make_unique<RequiredValidator>(*p));
}
// Misc validators
if (const auto* p = schema.get_ptr("dependencies")) {
- validators_.emplace_back(make_unique<DependencyValidator>(context, *p));
+ validators_.emplace_back(
+ std::make_unique<DependencyValidator>(context, *p));
}
if (const auto* p = schema.get_ptr("enum")) {
- validators_.emplace_back(make_unique<EnumValidator>(*p));
+ validators_.emplace_back(std::make_unique<EnumValidator>(*p));
}
if (const auto* p = schema.get_ptr("type")) {
- validators_.emplace_back(make_unique<TypeValidator>(*p));
+ validators_.emplace_back(std::make_unique<TypeValidator>(*p));
}
if (const auto* p = schema.get_ptr("allOf")) {
- validators_.emplace_back(make_unique<AllOfValidator>(context, *p));
+ validators_.emplace_back(std::make_unique<AllOfValidator>(context, *p));
}
if (const auto* p = schema.get_ptr("anyOf")) {
- validators_.emplace_back(make_unique<AnyOfValidator>(
+ validators_.emplace_back(std::make_unique<AnyOfValidator>(
context, *p, AnyOfValidator::Type::ONE_OR_MORE));
}
if (const auto* p = schema.get_ptr("oneOf")) {
- validators_.emplace_back(make_unique<AnyOfValidator>(
+ validators_.emplace_back(std::make_unique<AnyOfValidator>(
context, *p, AnyOfValidator::Type::EXACTLY_ONE));
}
if (const auto* p = schema.get_ptr("not")) {
- validators_.emplace_back(make_unique<NotValidator>(context, *p));
+ validators_.emplace_back(std::make_unique<NotValidator>(context, *p));
}
}
folly::Singleton<Validator> schemaValidator([]() {
return makeValidator(parseJson(metaschemaJson)).release();
});
-}
+} // namespace
Validator::~Validator() = default;
std::unique_ptr<Validator> makeValidator(const dynamic& schema) {
- auto v = make_unique<SchemaValidator>();
+ auto v = std::make_unique<SchemaValidator>();
SchemaValidatorContext context(schema);
context.refs["#"] = v.get();
v->loadSchema(context, schema);
std::shared_ptr<Validator> makeSchemaValidator() {
return schemaValidator.try_get();
}
-}
-}
+} // namespace jsonschema
+} // namespace folly