/* * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * * Copyright 2018 Danny Robson */ #include "length.hpp" #include "../tree.hpp" using util::json::schema::constraint::length; /////////////////////////////////////////////////////////////////////////////// namespace { template struct field_name { }; template <> struct field_name> { static constexpr char const *const value = "minLength"; }; template <> struct field_name> { static constexpr char const *const value = "maxLength"; }; template static constexpr auto field_name_v = field_name::value; } /////////////////////////////////////////////////////////////////////////////// template length::length(::json::tree::node const &def): m_length(def.as ()) { ; } /////////////////////////////////////////////////////////////////////////////// template typename length::output_iterator length::validate (output_iterator res, ::json::tree::node &target) const noexcept { if (!target.is_string ()) { return *res++ = failure { .rule = *this, .target = target }; } auto const &val = target.as_string (); if (!ComparatorT{}(val.size (), m_length)) *res++ = failure { .rule = *this, .target = target }; return res; } /////////////////////////////////////////////////////////////////////////////// template std::ostream& length::describe (std::ostream &os) const { return os << "{ " << ::field_name_v << ": " << m_length << " }"; } /////////////////////////////////////////////////////////////////////////////// template class util::json::schema::constraint::length>; template class util::json::schema::constraint::length>;