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;
28 public class Length extends Constraint {
29 // Constraint class for "length"
31 // Constrains the property or parameter to a value of a given length.
34 protected void setValues() {
36 setConstraintKey(LENGTH);
37 addValidTypes(Collections.singletonList("Integer"));
39 validPropTypes.add(Schema.STRING);
43 public Length(String name, String type, Object c) {
46 if (!validTypes.contains(constraintValue.getClass().getSimpleName())) {
47 ThreadLocalsHolder.getCollector().appendValidationIssue(new JToscaValidationIssue("JE109", "InvalidSchemaError: The property \"length\" expects an integer"));
52 protected boolean isValid(Object value) {
53 if (value instanceof String && constraintValue instanceof Integer &&
54 ((String) value).length() == (Integer) constraintValue) {
61 protected String errMsg(Object value) {
62 return String.format("Length of value \"%s\" of property \"%s\" must be equal to \"%s\"",
63 value.toString(), propertyName, constraintValue.toString());
69 class Length(Constraint):
70 """Constraint class for "length"
72 Constrains the property or parameter to a value of a given length.
75 constraint_key = Constraint.LENGTH
79 valid_prop_types = (Schema.STRING, )
81 def __init__(self, property_name, property_type, constraint):
82 super(Length, self).__init__(property_name, property_type, constraint)
83 if not isinstance(self.constraint_value, self.valid_types):
84 ValidationIsshueCollector.appendException(
85 InvalidSchemaError(message=_('The property "length" expects '
88 def _is_valid(self, value):
89 if isinstance(value, str) and len(value) == self.constraint_value:
94 def _err_msg(self, value):
95 return (_('Length of value "%(pvalue)s" of property "%(pname)s" '
96 'must be equal to "%(cvalue)s".') %
97 dict(pname=self.property_name,
99 cvalue=self.constraint_value))