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.regex.Matcher;
28 import java.util.regex.PatternSyntaxException;
30 public class Pattern extends Constraint {
33 protected void setValues() {
35 setConstraintKey(PATTERN);
37 addValidTypes(Collections.singletonList("String"));
39 validPropTypes.add(Schema.STRING);
44 public Pattern(String name, String type, Object c) {
47 if (!validTypes.contains(constraintValue.getClass().getSimpleName())) {
48 ThreadLocalsHolder.getCollector().appendValidationIssue(new JToscaValidationIssue("JE114", "InvalidSchemaError: The property \"pattern\" expects a string"));
53 protected boolean isValid(Object value) {
55 if (!(value instanceof String)) {
56 ThreadLocalsHolder.getCollector().appendValidationIssue(new JToscaValidationIssue("JE115", String.format("ValueError: Input value \"%s\" to \"pattern\" property \"%s\" must be a string",
57 value.toString(), propertyName)));
60 String strp = constraintValue.toString();
61 String strm = value.toString();
62 java.util.regex.Pattern pattern = java.util.regex.Pattern.compile(strp);
63 Matcher matcher = pattern.matcher(strm);
64 if (matcher.find() && matcher.end() == strm.length()) {
68 } catch (PatternSyntaxException pse) {
69 ThreadLocalsHolder.getCollector().appendValidationIssue(new JToscaValidationIssue("JE116", String.format("ValueError: Invalid regex \"%s\" in \"pattern\" property \"%s\"",
70 constraintValue.toString(), propertyName)));
76 protected String errMsg(Object value) {
77 return String.format("The value \"%s\" of property \"%s\" does not match the pattern \"%s\"",
78 value.toString(), propertyName, constraintValue.toString());
85 class Pattern(Constraint):
86 """Constraint class for "pattern"
88 Constrains the property or parameter to a value that is allowed by
89 the provided regular expression.
92 constraint_key = Constraint.PATTERN
96 valid_prop_types = (Schema.STRING, )
98 def __init__(self, property_name, property_type, constraint):
99 super(Pattern, self).__init__(property_name, property_type, constraint)
100 if not isinstance(self.constraint_value, self.valid_types):
101 ValidationIsshueCollector.appendException(
102 InvalidSchemaError(message=_('The property "pattern" '
103 'expects a string.')))
104 self.match = re.compile(self.constraint_value).match
106 def _is_valid(self, value):
107 match = self.match(value)
108 return match is not None and match.end() == len(value)
110 def _err_msg(self, value):
111 return (_('The value "%(pvalue)s" of property "%(pname)s" does not '
112 'match pattern "%(cvalue)s".') %
113 dict(pname=self.property_name,
115 cvalue=self.constraint_value))