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 MinLength extends Constraint {
30 // Constraint class for "min_length"
32 // Constrains the property or parameter to a value of a minimum length.
35 protected void setValues() {
37 setConstraintKey(MIN_LENGTH);
39 addValidTypes(Collections.singletonList("Integer"));
41 validPropTypes.add(Schema.STRING);
42 validPropTypes.add(Schema.MAP);
46 public MinLength(String name, String type, Object c) {
49 if (!validTypes.contains(constraintValue.getClass().getSimpleName())) {
50 ThreadLocalsHolder.getCollector().appendValidationIssue(new JToscaValidationIssue("JE113", "InvalidSchemaError: The property \"min_length\" expects an integer"));
54 @SuppressWarnings("unchecked")
56 protected boolean isValid(Object value) {
57 if (value instanceof String && constraintValue instanceof Integer
58 && ((String) value).length() >= (Integer) constraintValue) {
61 return value instanceof LinkedHashMap && constraintValue instanceof Integer
62 && ((LinkedHashMap<String, Object>) value).size() >= (Integer) constraintValue;
67 protected String errMsg(Object value) {
68 return String.format("Length of value \"%s\" of property \"%s\" must be at least \"%s\"",
69 value.toString(), propertyName, constraintValue.toString());
76 class MinLength(Constraint):
77 """Constraint class for "min_length"
79 Constrains the property or parameter to a value to a minimum length.
82 constraint_key = Constraint.MIN_LENGTH
86 valid_prop_types = (Schema.STRING, Schema.MAP)
88 def __init__(self, property_name, property_type, constraint):
89 super(MinLength, self).__init__(property_name, property_type,
91 if not isinstance(self.constraint_value, self.valid_types):
92 ValidationIsshueCollector.appendException(
93 InvalidSchemaError(message=_('The property "min_length" '
94 'expects an integer.')))
96 def _is_valid(self, value):
97 if ((isinstance(value, str) or isinstance(value, dict)) and
98 len(value) >= self.constraint_value):
103 def _err_msg(self, value):
104 return (_('Length of value "%(pvalue)s" of property "%(pname)s" '
105 'must be at least "%(cvalue)s".') %
106 dict(pname=self.property_name,
108 cvalue=self.constraint_value))