2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.sdc.toscaparser.api.elements.constraints;
23 import org.onap.sdc.toscaparser.api.common.JToscaValidationIssue;
24 import org.onap.sdc.toscaparser.api.utils.ThreadLocalsHolder;
26 import java.util.Collections;
27 import java.util.LinkedHashMap;
29 public class MaxLength extends Constraint {
30 // Constraint class for "min_length"
32 // Constrains the property or parameter to a value of a maximum length.
35 protected void setValues() {
37 setConstraintKey(MAX_LENGTH);
39 addValidTypes(Collections.singletonList("Integer"));
42 validPropTypes.add(Schema.STRING);
43 validPropTypes.add(Schema.MAP);
47 public MaxLength(String name, String type, Object c) {
50 if (!validTypes.contains(constraintValue.getClass().getSimpleName())) {
51 ThreadLocalsHolder.getCollector().appendValidationIssue(new JToscaValidationIssue("JE112", "InvalidSchemaError: The property \"max_length\" expects an integer"));
55 @SuppressWarnings("unchecked")
57 protected boolean isValid(Object value) {
58 if (value instanceof String && constraintValue instanceof Integer
59 && ((String) value).length() <= (Integer) constraintValue) {
62 return value instanceof LinkedHashMap && constraintValue instanceof Integer
63 && ((LinkedHashMap<String, Object>) value).size() <= (Integer) constraintValue;
68 protected String errMsg(Object value) {
69 return String.format("Length of value \"%s\" of property \"%s\" must be no greater than \"%s\"",
70 value.toString(), propertyName, constraintValue.toString());
77 class MaxLength(Constraint):
78 """Constraint class for "max_length"
80 Constrains the property or parameter to a value to a maximum length.
83 constraint_key = Constraint.MAX_LENGTH
87 valid_prop_types = (Schema.STRING, Schema.MAP)
89 def __init__(self, property_name, property_type, constraint):
90 super(MaxLength, self).__init__(property_name, property_type,
92 if not isinstance(self.constraint_value, self.valid_types):
93 ValidationIsshueCollector.appendException(
94 InvalidSchemaError(message=_('The property "max_length" '
95 'expects an integer.')))
97 def _is_valid(self, value):
98 if ((isinstance(value, str) or isinstance(value, dict)) and
99 len(value) <= self.constraint_value):
104 def _err_msg(self, value):
105 return (_('Length of value "%(pvalue)s" of property "%(pname)s" '
106 'must be no greater than "%(cvalue)s".') %
107 dict(pname=self.property_name,
109 cvalue=self.constraint_value))