@lionweb/validation 0.5.0-beta.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,90 @@
1
+ import { Language_PropertyValue_Issue } from "../issues/LanguageIssues";
2
+ import { Syntax_IdFormat_Issue, Syntax_KeyFormat_Issue, Syntax_SerializationFormatVersion_Issue, Syntax_VersionFormat_Issue } from "../issues/SyntaxIssues";
3
+ import { JsonContext } from "../issues/ValidationIssue";
4
+ import { LionWebJsonProperty } from "../json/LionWebJson";
5
+ import { ValidationResult } from "./ValidationResult";
6
+
7
+ export type ValidatorFunction = <T>(obj: T, ctx: JsonContext) => void;
8
+
9
+ export class SimpleFieldValidator {
10
+ validationResult: ValidationResult;
11
+
12
+ constructor(validationResult: ValidationResult) {
13
+ this.validationResult = validationResult;
14
+ }
15
+
16
+ /**
17
+ * Check whether `id` is a valid LionWeb id.
18
+ * @param id
19
+ * @param context
20
+ */
21
+ // eslint-disable-next-line @typescript-eslint/ban-types
22
+ validateId: ValidatorFunction = <String>(id: String, context: JsonContext): void => {
23
+ if (id === null || id === undefined) {
24
+ return;
25
+ }
26
+ const idString: string = id.toString();
27
+ const egexp = /^[a-zA-Z0-9$_-][a-zA-Z0-9$_-]*$/;
28
+ if (!egexp.test(idString)) {
29
+ this.validationResult.issue(new Syntax_IdFormat_Issue(context, idString));
30
+ }
31
+ }
32
+
33
+ /**
34
+ *
35
+ * @param key The `key` to be checked.
36
+ * @param context The context for the error message in errors.
37
+ * @return true if `key` is a correct LionWeb key.
38
+ */
39
+ // eslint-disable-next-line @typescript-eslint/ban-types
40
+ validateKey: ValidatorFunction = <String>(key: String, context: JsonContext): void => {
41
+ if (key === undefined || key === null) {
42
+ this.validationResult.issue(new Syntax_KeyFormat_Issue(context, "null-or-undefined"));
43
+ }
44
+ const keyString: string = "" + key;
45
+ const egexp = /^[a-zA-Z0-9$_-][a-zA-Z$_0-9-]*$/;
46
+ if (!egexp.test(keyString)) {
47
+ this.validationResult.issue(new Syntax_KeyFormat_Issue(context, keyString));
48
+ }
49
+ }
50
+
51
+ // eslint-disable-next-line @typescript-eslint/ban-types
52
+ validateVersion: ValidatorFunction = <String>(version: String, context: JsonContext): void => {
53
+ const versionString: string = "" + version;
54
+ if (versionString.length === 0) {
55
+ this.validationResult.issue(new Syntax_VersionFormat_Issue(context, versionString));
56
+ }
57
+ }
58
+
59
+ validateBoolean = (property: LionWebJsonProperty, propName: string, context: JsonContext): void => {
60
+ if (property.value !== "true" && property.value !== "false") {
61
+ this.validationResult.issue(new Language_PropertyValue_Issue(context, propName, property.value, "boolean"));
62
+ }
63
+ }
64
+
65
+ validateInteger = (property: LionWebJsonProperty, propName: string, context: JsonContext): void => {
66
+ const regexp = /^[+-]?(0|[1-9][0-9]*)$/;
67
+ if (!regexp.test(property.value)) {
68
+ this.validationResult.issue(new Language_PropertyValue_Issue(context, propName, property.value, "integer"));
69
+ }
70
+ }
71
+
72
+ validateJSON = (property: LionWebJsonProperty, propName: string, context: JsonContext): void => {
73
+ try {
74
+ JSON.parse(property.value);
75
+ } catch (e) {
76
+ this.validationResult.issue(new Language_PropertyValue_Issue(context, propName, property.value, "JSON"));
77
+ }
78
+ }
79
+
80
+ validateSerializationFormatVersion = (objElement: unknown, context: JsonContext): void => {
81
+ if (typeof objElement !== "string") {
82
+ this.validationResult.issue(new Syntax_SerializationFormatVersion_Issue(context, JSON.stringify(objElement)));
83
+ return;
84
+ }
85
+ if (objElement.length === 0) {
86
+ this.validationResult.issue(new Syntax_SerializationFormatVersion_Issue(context, objElement));
87
+ return;
88
+ }
89
+ }
90
+ }
@@ -0,0 +1,18 @@
1
+ import { ValidationIssue } from "../issues/ValidationIssue";
2
+
3
+ export class ValidationResult {
4
+ issues: ValidationIssue[] = [];
5
+
6
+ issue(issue: ValidationIssue) {
7
+ this.issues.push(issue);
8
+ }
9
+
10
+ reset() {
11
+ this.issues = [];
12
+ }
13
+
14
+ hasErrors(): boolean {
15
+ return this.issues.length !== 0;
16
+ }
17
+
18
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,8 @@
1
+ {
2
+ "extends": "../../tsconfig.json",
3
+ "compilerOptions": {
4
+ "rootDir": "src",
5
+ "outDir": "dist/",
6
+ "resolveJsonModule": true, /* Enable importing .json files */
7
+ }
8
+ }