1
+ /*
2
+ * Licensed to the Apache Software Foundation (ASF) under one or more
3
+ * contributor license agreements. See the NOTICE file distributed with
4
+ * this work for additional information regarding copyright ownership.
5
+ * The ASF licenses this file to You under the Apache License, Version 2.0
6
+ * (the "License"); you may not use this file except in compliance with
7
+ * the License. You may obtain a copy of the License at
8
+ *
9
+ * http://www.apache.org/licenses/LICENSE-2.0
10
+ *
11
+ * Unless required by applicable law or agreed to in writing, software
12
+ * distributed under the License is distributed on an "AS IS" BASIS,
13
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ * See the License for the specific language governing permissions and
15
+ * limitations under the License.
16
+ */
17
+ package com .networknt .schema .format ;
18
+
19
+ import com .fasterxml .jackson .databind .JsonNode ;
20
+ import com .networknt .schema .*;
21
+ import org .slf4j .Logger ;
22
+ import org .slf4j .LoggerFactory ;
23
+
24
+ import java .util .Collections ;
25
+ import java .util .LinkedHashSet ;
26
+ import java .util .Set ;
27
+ import java .util .regex .Matcher ;
28
+ import java .util .regex .Pattern ;
29
+
30
+ /**
31
+ *
32
+ * <p>This class provides methods to validate a duration format .
33
+ *
34
+ * @version $Revision$
35
+ * @since Validator 1.4
36
+ */
37
+ public class DurationValidator extends BaseJsonValidator implements JsonValidator {
38
+ private static final Logger logger = LoggerFactory .getLogger (DurationValidator .class );
39
+
40
+ private static final String DURATION_REGEX = "^(-?)P(?=\\ d|T\\ d)(?:(\\ d+)Y)?(?:(\\ d+)M)?(?:(\\ d+)([DW]))?(?:T(?:(\\ d+)H)?(?:(\\ d+)M)?(?:(\\ d+(?:\\ .\\ d+)?)S)?)?$" ;
41
+ private static final Pattern DURATION_PATTERN = Pattern .compile (DURATION_REGEX );
42
+ private final String formatName ;
43
+
44
+
45
+
46
+ /**
47
+ * <p>Checks if a field has a valid duration.</p>
48
+ *
49
+ * @param duration The value validation is being performed on. A <code>null</code>
50
+ * value is considered invalid.
51
+ * @return true if the duration valid.
52
+ */
53
+ public boolean isValid (String duration ) {
54
+ if (duration == null ) {
55
+ return false ;
56
+ }
57
+
58
+ Matcher durationMatcher = DURATION_PATTERN .matcher (duration );
59
+ if (!durationMatcher .matches ()) {
60
+ return false ;
61
+ }
62
+
63
+ return true ;
64
+ }
65
+
66
+ public DurationValidator (String schemaPath , JsonNode schemaNode , JsonSchema parentSchema , ValidationContext validationContext , String formatName ) {
67
+ super (schemaPath , schemaNode , parentSchema , ValidatorTypeCode .FORMAT , validationContext );
68
+ this .formatName = formatName ;
69
+ this .validationContext = validationContext ;
70
+ parseErrorCode (getValidatorType ().getErrorCodeKey ());
71
+ }
72
+
73
+ @ Override
74
+ public Set <ValidationMessage > validate (JsonNode node , JsonNode rootNode , String at ) {
75
+ debug (logger , node , rootNode , at );
76
+
77
+ Set <ValidationMessage > errors = new LinkedHashSet <ValidationMessage >();
78
+
79
+ JsonType nodeType = TypeFactory .getValueNodeType (node , this .validationContext .getConfig ());
80
+ if (nodeType != JsonType .STRING ) {
81
+ return errors ;
82
+ }
83
+ if (!isValid (node .textValue ())) {
84
+ errors .add (buildValidationMessage (at , node .textValue (), formatName ));
85
+ }
86
+ return Collections .unmodifiableSet (errors );
87
+
88
+ }
89
+ }
0 commit comments