MQTTSuite
Loading...
Searching...
No Matches
anonymous_namespace{json-validator.cpp}::string Class Reference
Inheritance diagram for anonymous_namespace{json-validator.cpp}::string:
Collaboration diagram for anonymous_namespace{json-validator.cpp}::string:

Public Member Functions

 string (json &sch, root_schema *root)
 string (json &sch, root_schema *root)
Public Member Functions inherited from anonymous_namespace{json-validator.cpp}::schema
virtual ~schema ()=default
 schema (root_schema *root)
virtual const jsondefault_value (const json::json_pointer &, const json &, error_handler &) const
void set_default_value (const json &v)
virtual ~schema ()=default
 schema (root_schema *root)
virtual const jsondefault_value (const json::json_pointer &, const json &, error_handler &) const
void set_default_value (const json &v)

Private Member Functions

std::size_t utf8_length (const std::string &s) const
void validate (const json::json_pointer &ptr, const json &instance, json_patch &, error_handler &e) const override
std::size_t utf8_length (const std::string &s) const
void validate (const json::json_pointer &ptr, const json &instance, json_patch &, error_handler &e) const override

Private Attributes

std::pair< bool, size_t > maxLength_ {false, 0}
std::pair< bool, size_t > minLength_ {false, 0}
std::pair< bool, std::regex > pattern_ {false, std::regex()}
std::string patternString_
std::pair< bool, std::string > format_
std::tuple< bool, std::string, std::string > content_ {false, "", ""}

Additional Inherited Members

Static Public Member Functions inherited from anonymous_namespace{json-validator.cpp}::schema
static std::shared_ptr< schemamake (json &schema, root_schema *root, const std::vector< std::string > &key, std::vector< nlohmann::json_uri > uris)
static std::shared_ptr< schemamake (json &schema, root_schema *root, const std::vector< std::string > &key, std::vector< nlohmann::json_uri > uris)
Protected Member Functions inherited from anonymous_namespace{json-validator.cpp}::schema
virtual std::shared_ptr< schemamake_for_default_ (std::shared_ptr<::schema > &, root_schema *, std::vector< nlohmann::json_uri > &, nlohmann::json &) const
virtual std::shared_ptr< schemamake_for_default_ (std::shared_ptr<::schema > &, root_schema *, std::vector< nlohmann::json_uri > &, nlohmann::json &) const
Protected Attributes inherited from anonymous_namespace{json-validator.cpp}::schema
root_schemaroot_
json default_value_ = nullptr

Detailed Description

Definition at line 739 of file json-validator.cpp.

Constructor & Destructor Documentation

◆ string() [1/2]

anonymous_namespace{json-validator.cpp}::string::string ( json & sch,
root_schema * root )
inline

Definition at line 817 of file json-validator.cpp.

818 : schema(root)
819 {
820 auto attr = sch.find("maxLength");
821 if (attr != sch.end()) {
822 maxLength_ = {true, attr.value().get<size_t>()};
823 sch.erase(attr);
824 }
825
826 attr = sch.find("minLength");
827 if (attr != sch.end()) {
828 minLength_ = {true, attr.value().get<size_t>()};
829 sch.erase(attr);
830 }
831
832 attr = sch.find("contentEncoding");
833 if (attr != sch.end()) {
834 std::get<0>(content_) = true;
835 std::get<1>(content_) = attr.value().get<std::string>();
836
837 // special case for nlohmann::json-binary-types
838 //
839 // https://github.com/pboettch/json-schema-validator/pull/114
840 //
841 // We cannot use explicitly in a schema: {"type": "binary"} or
842 // "type": ["binary", "number"] we have to be implicit. For a
843 // schema where "contentEncoding" is set to "binary", an instance
844 // of type json::value_t::binary is accepted. If a
845 // contentEncoding-callback has to be provided and is called
846 // accordingly. For encoding=binary, no other type validations are done
847
848 sch.erase(attr);
849 }
850
851 attr = sch.find("contentMediaType");
852 if (attr != sch.end()) {
853 std::get<0>(content_) = true;
854 std::get<2>(content_) = attr.value().get<std::string>();
855
856 sch.erase(attr);
857 }
858
859 if (std::get<0>(content_) == true && root_->content_check() == nullptr) {
860 throw std::invalid_argument{"schema contains contentEncoding/contentMediaType but content checker was not set"};
861 }
862
863#ifndef NO_STD_REGEX
864 attr = sch.find("pattern");
865 if (attr != sch.end()) {
866 patternString_ = attr.value().get<std::string>();
867 pattern_ = {true, REGEX_NAMESPACE::regex(attr.value().get<std::string>(),
868 REGEX_NAMESPACE::regex::ECMAScript)};
869 sch.erase(attr);
870 }
871#endif
872
873 attr = sch.find("format");
874 if (attr != sch.end()) {
875 if (root_->format_check() == nullptr)
876 throw std::invalid_argument{"a format checker was not provided but a format keyword for this string is present: " + format_.second};
877
878 format_ = {true, attr.value().get<std::string>()};
879 sch.erase(attr);
880 }
881 }
std::tuple< bool, std::string, std::string > content_
auto schema
Definition id-ref.cpp:69

References content_, nlohmann::json_schema::root_schema::content_check(), format_, nlohmann::json_schema::root_schema::format_check(), maxLength_, minLength_, pattern_, patternString_, anonymous_namespace{json-validator.cpp}::schema::root_, and anonymous_namespace{json-validator.cpp}::schema::schema().

Here is the call graph for this function:

◆ string() [2/2]

anonymous_namespace{json-validator.cpp}::string::string ( json & sch,
root_schema * root )
inline

Definition at line 817 of file json-validator.cpp.

818 : schema(root)
819 {
820 auto attr = sch.find("maxLength");
821 if (attr != sch.end()) {
822 maxLength_ = {true, attr.value().get<size_t>()};
823 sch.erase(attr);
824 }
825
826 attr = sch.find("minLength");
827 if (attr != sch.end()) {
828 minLength_ = {true, attr.value().get<size_t>()};
829 sch.erase(attr);
830 }
831
832 attr = sch.find("contentEncoding");
833 if (attr != sch.end()) {
834 std::get<0>(content_) = true;
835 std::get<1>(content_) = attr.value().get<std::string>();
836
837 // special case for nlohmann::json-binary-types
838 //
839 // https://github.com/pboettch/json-schema-validator/pull/114
840 //
841 // We cannot use explicitly in a schema: {"type": "binary"} or
842 // "type": ["binary", "number"] we have to be implicit. For a
843 // schema where "contentEncoding" is set to "binary", an instance
844 // of type json::value_t::binary is accepted. If a
845 // contentEncoding-callback has to be provided and is called
846 // accordingly. For encoding=binary, no other type validations are done
847
848 sch.erase(attr);
849 }
850
851 attr = sch.find("contentMediaType");
852 if (attr != sch.end()) {
853 std::get<0>(content_) = true;
854 std::get<2>(content_) = attr.value().get<std::string>();
855
856 sch.erase(attr);
857 }
858
859 if (std::get<0>(content_) == true && root_->content_check() == nullptr) {
860 throw std::invalid_argument{"schema contains contentEncoding/contentMediaType but content checker was not set"};
861 }
862
863#ifndef NO_STD_REGEX
864 attr = sch.find("pattern");
865 if (attr != sch.end()) {
866 patternString_ = attr.value().get<std::string>();
867 pattern_ = {true, REGEX_NAMESPACE::regex(attr.value().get<std::string>(),
868 REGEX_NAMESPACE::regex::ECMAScript)};
869 sch.erase(attr);
870 }
871#endif
872
873 attr = sch.find("format");
874 if (attr != sch.end()) {
875 if (root_->format_check() == nullptr)
876 throw std::invalid_argument{"a format checker was not provided but a format keyword for this string is present: " + format_.second};
877
878 format_ = {true, attr.value().get<std::string>()};
879 sch.erase(attr);
880 }
881 }

Member Function Documentation

◆ utf8_length() [1/2]

std::size_t anonymous_namespace{json-validator.cpp}::string::utf8_length ( const std::string & s) const
inlineprivate

Definition at line 752 of file json-validator.cpp.

753 {
754 size_t len = 0;
755 for (auto c : s)
756 if ((c & 0xc0) != 0x80)
757 len++;
758 return len;
759 }

Referenced by validate().

Here is the caller graph for this function:

◆ utf8_length() [2/2]

std::size_t anonymous_namespace{json-validator.cpp}::string::utf8_length ( const std::string & s) const
inlineprivate

Definition at line 752 of file json-validator.cpp.

753 {
754 size_t len = 0;
755 for (auto c : s)
756 if ((c & 0xc0) != 0x80)
757 len++;
758 return len;
759 }

◆ validate() [1/2]

void anonymous_namespace{json-validator.cpp}::string::validate ( const json::json_pointer & ptr,
const json & instance,
json_patch & ,
error_handler & e ) const
inlineoverrideprivatevirtual

Implements anonymous_namespace{json-validator.cpp}::schema.

Definition at line 761 of file json-validator.cpp.

762 {
763 if (minLength_.first) {
764 if (utf8_length(instance.get<std::string>()) < minLength_.second) {
765 std::ostringstream s;
766 s << "instance is too short as per minLength:" << minLength_.second;
767 e.error(ptr, instance, s.str());
768 }
769 }
770
771 if (maxLength_.first) {
772 if (utf8_length(instance.get<std::string>()) > maxLength_.second) {
773 std::ostringstream s;
774 s << "instance is too long as per maxLength: " << maxLength_.second;
775 e.error(ptr, instance, s.str());
776 }
777 }
778
779 if (std::get<0>(content_)) {
780 if (root_->content_check() == nullptr)
781 e.error(ptr, instance, std::string("a content checker was not provided but a contentEncoding or contentMediaType for this string have been present: '") + std::get<1>(content_) + "' '" + std::get<2>(content_) + "'");
782 else {
783 try {
784 root_->content_check()(std::get<1>(content_), std::get<2>(content_), instance);
785 } catch (const std::exception &ex) {
786 e.error(ptr, instance, std::string("content-checking failed: ") + ex.what());
787 }
788 }
789 } else if (instance.type() == json::value_t::binary) {
790 e.error(ptr, instance, "expected string, but get binary data");
791 }
792
793 if (instance.type() != json::value_t::string) {
794 return; // next checks only for strings
795 }
796
797#ifndef NO_STD_REGEX
798 if (pattern_.first &&
799 !REGEX_NAMESPACE::regex_search(instance.get<std::string>(), pattern_.second))
800 e.error(ptr, instance, "instance does not match regex pattern: " + patternString_);
801#endif
802
803 if (format_.first) {
804 if (root_->format_check() == nullptr)
805 e.error(ptr, instance, std::string("a format checker was not provided but a format keyword for this string is present: ") + format_.second);
806 else {
807 try {
808 root_->format_check()(format_.second, instance.get<std::string>());
809 } catch (const std::exception &ex) {
810 e.error(ptr, instance, std::string("format-checking failed: ") + ex.what());
811 }
812 }
813 }
814 }
std::size_t utf8_length(const std::string &s) const
virtual void error(const json::json_pointer &, const json &, const std::string &)=0
static const auto instance
Definition issue-93.cpp:14

References content_, nlohmann::json_schema::root_schema::content_check(), nlohmann::json_schema::error_handler::error(), format_, nlohmann::json_schema::root_schema::format_check(), maxLength_, minLength_, pattern_, patternString_, anonymous_namespace{json-validator.cpp}::schema::root_, and utf8_length().

Here is the call graph for this function:

◆ validate() [2/2]

void anonymous_namespace{json-validator.cpp}::string::validate ( const json::json_pointer & ptr,
const json & instance,
json_patch & ,
error_handler & e ) const
inlineoverrideprivatevirtual

Implements anonymous_namespace{json-validator.cpp}::schema.

Definition at line 761 of file json-validator.cpp.

762 {
763 if (minLength_.first) {
764 if (utf8_length(instance.get<std::string>()) < minLength_.second) {
765 std::ostringstream s;
766 s << "instance is too short as per minLength:" << minLength_.second;
767 e.error(ptr, instance, s.str());
768 }
769 }
770
771 if (maxLength_.first) {
772 if (utf8_length(instance.get<std::string>()) > maxLength_.second) {
773 std::ostringstream s;
774 s << "instance is too long as per maxLength: " << maxLength_.second;
775 e.error(ptr, instance, s.str());
776 }
777 }
778
779 if (std::get<0>(content_)) {
780 if (root_->content_check() == nullptr)
781 e.error(ptr, instance, std::string("a content checker was not provided but a contentEncoding or contentMediaType for this string have been present: '") + std::get<1>(content_) + "' '" + std::get<2>(content_) + "'");
782 else {
783 try {
784 root_->content_check()(std::get<1>(content_), std::get<2>(content_), instance);
785 } catch (const std::exception &ex) {
786 e.error(ptr, instance, std::string("content-checking failed: ") + ex.what());
787 }
788 }
789 } else if (instance.type() == json::value_t::binary) {
790 e.error(ptr, instance, "expected string, but get binary data");
791 }
792
793 if (instance.type() != json::value_t::string) {
794 return; // next checks only for strings
795 }
796
797#ifndef NO_STD_REGEX
798 if (pattern_.first &&
799 !REGEX_NAMESPACE::regex_search(instance.get<std::string>(), pattern_.second))
800 e.error(ptr, instance, "instance does not match regex pattern: " + patternString_);
801#endif
802
803 if (format_.first) {
804 if (root_->format_check() == nullptr)
805 e.error(ptr, instance, std::string("a format checker was not provided but a format keyword for this string is present: ") + format_.second);
806 else {
807 try {
808 root_->format_check()(format_.second, instance.get<std::string>());
809 } catch (const std::exception &ex) {
810 e.error(ptr, instance, std::string("format-checking failed: ") + ex.what());
811 }
812 }
813 }
814 }

Member Data Documentation

◆ content_

std::tuple< bool, std::string, std::string > anonymous_namespace{json-validator.cpp}::string::content_ {false, "", ""}
private

Definition at line 750 of file json-validator.cpp.

750{false, "", ""};

Referenced by string(), and validate().

◆ format_

std::pair< bool, std::string > anonymous_namespace{json-validator.cpp}::string::format_
private

Definition at line 749 of file json-validator.cpp.

Referenced by string(), and validate().

◆ maxLength_

std::pair< bool, size_t > anonymous_namespace{json-validator.cpp}::string::maxLength_ {false, 0}
private

Definition at line 741 of file json-validator.cpp.

741{false, 0};

Referenced by string(), and validate().

◆ minLength_

std::pair< bool, size_t > anonymous_namespace{json-validator.cpp}::string::minLength_ {false, 0}
private

Definition at line 742 of file json-validator.cpp.

742{false, 0};

Referenced by string(), and validate().

◆ pattern_

std::pair< bool, std::regex > anonymous_namespace{json-validator.cpp}::string::pattern_ {false, std::regex()}
private

Definition at line 745 of file json-validator.cpp.

745{false, REGEX_NAMESPACE::regex()};

Referenced by string(), and validate().

◆ patternString_

std::string anonymous_namespace{json-validator.cpp}::string::patternString_
private

Definition at line 746 of file json-validator.cpp.

Referenced by string(), and validate().


The documentation for this class was generated from the following file: