Skip to content

Commit

Permalink
SHIBUI-907 Fixed issue with validation messages
Browse files Browse the repository at this point in the history
  • Loading branch information
rmathis committed Jan 23, 2019
1 parent 17c0cfe commit 69fd222
Show file tree
Hide file tree
Showing 13 changed files with 71 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -598,8 +598,7 @@
"certificateFile": {
"title": "label.certificate-file",
"description": "tooltip.certificate-file",
"type": "string",
"default": ""
"type": "string"
}
},
"anyOf": [
Expand Down
9 changes: 7 additions & 2 deletions ui/src/app/metadata/metadata.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import { MetadataRoutingModule } from './metadata.routing';
import { ProviderModule } from './provider/provider.module';
import { I18nModule } from '../i18n/i18n.module';
import { CustomWidgetRegistry } from '../schema-form/registry';
import { WidgetRegistry } from 'ngx-schema-form';
import { WidgetRegistry, SchemaValidatorFactory } from 'ngx-schema-form';
import { CustomSchemaValidatorFactory } from '../schema-form/service/schema-validator';


@NgModule({
Expand All @@ -23,7 +24,11 @@ import { WidgetRegistry } from 'ngx-schema-form';
I18nModule
],
providers: [
{ provide: WidgetRegistry, useClass: CustomWidgetRegistry }
{ provide: WidgetRegistry, useClass: CustomWidgetRegistry },
{
provide: SchemaValidatorFactory,
useClass: CustomSchemaValidatorFactory
}
],
declarations: [
MetadataPageComponent
Expand Down
3 changes: 3 additions & 0 deletions ui/src/app/schema-form/model/messages.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const HARD_CODED_REQUIRED_MSG = RegExp('Missing required property');

export const REQUIRED_MSG_OVERRIDE = 'message.required';
3 changes: 2 additions & 1 deletion ui/src/app/schema-form/schema-form.module.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { NgModule } from '@angular/core';
import { SchemaFormModule } from 'ngx-schema-form';
import { SchemaFormModule, SchemaValidatorFactory } from 'ngx-schema-form';
import { CommonModule } from '@angular/common';
import { ReactiveFormsModule } from '@angular/forms';

Expand All @@ -23,6 +23,7 @@ import { CustomObjectWidget } from './widget/object/object.component';
import { CustomRadioComponent } from './widget/radio/radio.component';
import { InlineObjectListComponent } from './widget/array/inline-obj-list.component';
import { InlineObjectComponent } from './widget/object/inline-obj.component';
import { CustomSchemaValidatorFactory } from './service/schema-validator';

export const COMPONENTS = [
BooleanRadioComponent,
Expand Down
19 changes: 19 additions & 0 deletions ui/src/app/schema-form/service/schema-validator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import * as ZSchema from 'z-schema';
import { ZSchemaValidatorFactory } from 'ngx-schema-form';

export class CustomSchemaValidatorFactory extends ZSchemaValidatorFactory {

protected zschema;

constructor() {
super();
this.createSchemaValidator();
}

private createSchemaValidator() {
this.zschema = new ZSchema({
breakOnFirstError: false
});
}
}

3 changes: 2 additions & 1 deletion ui/src/app/schema-form/service/schema.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ export class SchemaService {
Object
.keys(condition.properties)
.some(
key => values.hasOwnProperty(key) ? condition.properties[key].enum[0] === values[key] : false
key => values.hasOwnProperty(key) && condition.properties[key].enum ?
condition.properties[key].enum[0] === values[key] : false
)
);
currentConditions.forEach(el => {
Expand Down
2 changes: 1 addition & 1 deletion ui/src/app/schema-form/widget/select/select.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
<small class="form-text text-danger" *ngIf="errorMessages && errorMessages.length && control.touched">
<ng-container *ngFor="let error of errorMessages; let first=first;">
<ng-container *ngIf="!first">, </ng-container>
<translate-i18n [key]="error">error</translate-i18n>
<translate-i18n [key]="getError(error)">error</translate-i18n>
</ng-container>
</small>
<input *ngIf="schema.readOnly" [attr.name]="name" type="hidden" [formControl]="control">
Expand Down
7 changes: 6 additions & 1 deletion ui/src/app/schema-form/widget/select/select.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { SelectWidget } from 'ngx-schema-form';
import { SchemaService } from '../../service/schema.service';
import { map, shareReplay, startWith } from 'rxjs/operators';
import { Subscription } from 'rxjs';
import { HARD_CODED_REQUIRED_MSG } from '../../model/messages';

@Component({
selector: 'select-component',
Expand Down Expand Up @@ -43,7 +44,7 @@ export class CustomSelectComponent extends SelectWidget implements AfterViewInit
}

this.errorSub = this.control.valueChanges.pipe(startWith(this.control.value)).subscribe(v => {
if (!v && this.required && !this.errorMessages.some(msg => !!msg.toLowerCase().match('required').length)) {
if (!v && this.required && !this.errorMessages.some(msg => HARD_CODED_REQUIRED_MSG.test(msg))) {
this.errorMessages.push('message.required');
}
});
Expand All @@ -56,4 +57,8 @@ export class CustomSelectComponent extends SelectWidget implements AfterViewInit
get required(): boolean {
return this.widgetService.isRequired(this.formProperty);
}

getError(error: string): string {
return HARD_CODED_REQUIRED_MSG.test(error) ? 'message.required' : error;
}
}
3 changes: 2 additions & 1 deletion ui/src/app/schema-form/widget/string/string.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
validate="true"
[attr.readonly]="schema.readOnly?true:null"
class="text-widget.id textline-widget form-control"
[class.is-invalid]="control.touched && !control.value && errorMessages.length"
[attr.type]="this.getInputType()"
[attr.id]="id"
[formControl]="control"
Expand All @@ -25,7 +26,7 @@
<small class="form-text text-danger" *ngIf="errorMessages && errorMessages.length && control.touched">
<ng-container *ngFor="let error of errorMessages; let first=first;">
<ng-container *ngIf="!first">, </ng-container>
<translate-i18n [key]="error">error</translate-i18n>
<translate-i18n [key]="getError(error)">error</translate-i18n>
</ng-container>
</small>
<small class="form-text text-secondary"
Expand Down
19 changes: 16 additions & 3 deletions ui/src/app/schema-form/widget/string/string.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { SchemaService } from '../../service/schema.service';
import { startWith } from 'rxjs/operators';
import { Subscription } from 'rxjs';

import { HARD_CODED_REQUIRED_MSG, REQUIRED_MSG_OVERRIDE } from '../../model/messages';

@Component({
selector: 'custom-string',
templateUrl: `./string.component.html`,
Expand All @@ -22,9 +24,16 @@ export class CustomStringComponent extends StringWidget implements AfterViewInit

ngAfterViewInit(): void {
super.ngAfterViewInit();
this.errorSub = this.control.valueChanges.pipe(startWith(this.control.value)).subscribe(v => {
if (!v && this.required && !this.errorMessages.some(msg => !!msg.toLowerCase().match('required').length)) {
this.errorMessages.push('message.required');
let listener = this.formProperty.parent ? this.formProperty.parent : this.control;
this.errorSub = listener.valueChanges.pipe(startWith(listener.value)).subscribe(v => {
if (!this.control.value
&& this.required
&& !this.errorMessages.some(msg => HARD_CODED_REQUIRED_MSG.test(msg))
&& this.errorMessages.indexOf(REQUIRED_MSG_OVERRIDE) < 0) {
this.errorMessages.push(REQUIRED_MSG_OVERRIDE);
}
if (!this.required) {
this.errorMessages = this.errorMessages.filter(e => e !== REQUIRED_MSG_OVERRIDE);
}
});
}
Expand All @@ -38,4 +47,8 @@ export class CustomStringComponent extends StringWidget implements AfterViewInit

return req;
}

getError(error: string): string {
return HARD_CODED_REQUIRED_MSG.test(error) ? REQUIRED_MSG_OVERRIDE : error;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<small class="form-text text-danger" *ngIf="errorMessages && errorMessages.length && control.touched">
<ng-container *ngFor="let error of errorMessages; let first=first;">
<ng-container *ngIf="!first">, </ng-container>
<translate-i18n [key]="error">error</translate-i18n>
<translate-i18n [key]="getError(error)">error</translate-i18n>
</ng-container>
</small>
</div>
7 changes: 6 additions & 1 deletion ui/src/app/schema-form/widget/textarea/textarea.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { TextAreaWidget } from 'ngx-schema-form';
import { SchemaService } from '../../service/schema.service';
import { Subscription } from 'rxjs';
import { startWith } from 'rxjs/operators';
import { HARD_CODED_REQUIRED_MSG } from '../../model/messages';

@Component({
selector: 'textarea-component',
Expand All @@ -22,7 +23,7 @@ export class CustomTextAreaComponent extends TextAreaWidget implements AfterView
ngAfterViewInit(): void {
super.ngAfterViewInit();
this.errorSub = this.control.valueChanges.pipe(startWith(this.control.value)).subscribe(v => {
if (!v && this.required && !this.errorMessages.some(msg => !!msg.toLowerCase().match('required').length)) {
if (!v && this.required && !this.errorMessages.some(msg => HARD_CODED_REQUIRED_MSG.test(msg))) {
this.errorMessages.push('message.required');
}
});
Expand All @@ -35,4 +36,8 @@ export class CustomTextAreaComponent extends TextAreaWidget implements AfterView
get required(): boolean {
return this.widgetService.isRequired(this.formProperty);
}

getError(error: string): string {
return error.match('required').length ? 'message.required' : error;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,18 @@
"title": "label.certificate-file",
"description": "tooltip.certificate-file",
"type": "string",
"widget": "textline",
"default": ""
"widget": "textline"
}
},
"anyOf": [
{
"properties": {
"requireSignedRoot": {
"enum": [ true ]
},
"certificateFile": {
"minLength": 1,
"type": "string"
}
},
"required": [
Expand Down

0 comments on commit 69fd222

Please sign in to comment.