UNPKG

2.19 kBJavaScriptView Raw
1/*
2 * Licensed under the Apache License, Version 2.0 (the "License");
3 * you may not use this file except in compliance with the License.
4 * You may obtain a copy of the License at
5 *
6 * http://www.apache.org/licenses/LICENSE-2.0
7 *
8 * Unless required by applicable law or agreed to in writing, software
9 * distributed under the License is distributed on an "AS IS" BASIS,
10 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 * See the License for the specific language governing permissions and
12 * limitations under the License.
13 */
14
15'use strict';
16
17/**
18 * An Abstract field validator. Extend this class and override the
19 * validate method.
20 * @private
21 * @class
22 * @abstract
23 * @memberof module:concerto-core
24 */
25class Validator {
26
27 /**
28 * Create a Property.
29 * @param {Field} field - the field this validator is attached to
30 * @param {Object} validator - The validation string
31 * @throws {IllegalModelException}
32 */
33 constructor(field, validator) {
34 this.validator = validator;
35 this.field = field;
36 }
37
38 /**
39 * @param {string} id the identifier of the instance
40 * @param {string} msg the exception message
41 * @throws {Error} throws an error to report the message
42 */
43 reportError(id, msg) {
44 throw new Error( 'Validator error for field ' + id + ' ' + this.getField().getFullyQualifiedName() + ': ' + msg );
45 }
46
47 /**
48 * Visitor design pattern
49 * @param {Object} visitor - the visitor
50 * @param {Object} parameters - the parameter
51 * @return {Object} the result of visiting or null
52 * @private
53 */
54 accept(visitor,parameters) {
55 return visitor.visit(this, parameters);
56 }
57
58 /**
59 * Returns the field that this validator applies to
60 * @return {Field} the field
61 */
62 getField() {
63 return this.field;
64 }
65
66 /**
67 * Validate the property against a value
68 * @param {string} identifier the identifier of the instance being validated
69 * @param {Object} value the value to validate
70 * @throws {IllegalModelException}
71 * @private
72 */
73 validate(identifier, value) {
74 }
75}
76
77module.exports = Validator;