Skip to content

Commit 303aa06

Browse files
committed
support custom schema validation error messages
* Closes inveniosoftware-contrib#417
1 parent 6ba019a commit 303aa06

File tree

6 files changed

+46
-3
lines changed

6 files changed

+46
-3
lines changed

example/app/app.config.ts

+11
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,17 @@ export class AppConfig {
9898
}
9999
}
100100
}
101+
},
102+
imprints: {
103+
items: {
104+
properties: {
105+
date: {
106+
errorMessage: {
107+
format: 'This is not a date!'
108+
}
109+
}
110+
}
111+
}
101112
}
102113
}
103114
},

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@
4343
"zone.js": "^0.8.4"
4444
},
4545
"dependencies": {
46-
"ajv": "^4.11.3",
46+
"ajv": "^5.0.0",
47+
"ajv-errors": "^1.0.0",
4748
"diff": "^3.3.0",
4849
"immutable": "^3.8.1",
4950
"katex": "^0.7.1",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
export interface CustomErrorMessages {
2+
pattern?: string;
3+
format?: string;
4+
multipleOf?: string;
5+
maximum?: string;
6+
exclusiveMaximum?: string;
7+
minimum?: string;
8+
exclusiveMinimum?: string;
9+
10+
/**
11+
* Schema validation for pritimive types so below is not supported yet
12+
* maxLength?: string;
13+
* minLength?: string;
14+
* maxItems?: string;
15+
* minItems?: string;
16+
* uniqueItems?: string;
17+
* maxProperties?: string;
18+
* minProperties?: string;
19+
*/
20+
}

src/shared/interfaces/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,4 @@ export { CustomFormatValidation } from './custom-format-validation';
2727
export { JsonPatchesByPath } from './json-patches-by-path';
2828
export { ViewTemplateConfig } from './view-template-config';
2929
export { ShortcutActionFunction } from './shortcut-action-function';
30+
export { CustomErrorMessages } from './custom-error-messages';

src/shared/interfaces/schema-option.ts

+6
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { LongListNavigatorConfig } from './long-list-navigator-config';
33
import { ViewTemplateConfig } from './view-template-config';
44
import { RefConfig } from './ref-config';
55
import { OnValueChangeFunction } from './on-value-change-function';
6+
import { CustomErrorMessages } from './custom-error-messages';
67

78
export interface SchemaOption {
89
/**
@@ -109,4 +110,9 @@ export interface SchemaOption {
109110
* Flag to enable LaTeX preview in a field.
110111
*/
111112
latexPreviewEnabled?: boolean;
113+
114+
/**
115+
* Custom error messages for schema validation
116+
*/
117+
errorMessage?: string | CustomErrorMessages;
112118
}

src/shared/services/schema-validation.service.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,16 @@
2222

2323
import { Injectable } from '@angular/core';
2424
import * as Ajv from 'ajv';
25-
import { AppGlobalsService } from './app-globals.service';
25+
import * as enableCustomErrorMessages from 'ajv-errors';
2626

27+
import { AppGlobalsService } from './app-globals.service';
2728
import { JSONSchema, ValidationError } from '../interfaces';
2829

2930
@Injectable()
3031
export class SchemaValidationService {
3132

32-
private ajv = new Ajv({ allErrors: true });
33+
// `jsonPointer: true` is required for `avj-errors` package
34+
private ajv = new Ajv({ allErrors: true, jsonPointers: true });
3335

3436
// https://gist.github.com/dperini/729294
3537
private reWebUrl = new RegExp(
@@ -72,6 +74,8 @@ export class SchemaValidationService {
7274
);
7375

7476
constructor(public appGlobalsService: AppGlobalsService) {
77+
enableCustomErrorMessages(this.ajv);
78+
7579
// ajv didn't support format:url, so was added using web url regex for validation
7680
this.ajv.addFormat('url', this.reWebUrl);
7781
if (this.appGlobalsService.config && this.appGlobalsService.config.customFormatValidation) {

0 commit comments

Comments
 (0)