Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions packages/components/src/internal/shared/validators.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import BaseValidator from '../../validators/BaseValidator';

export interface ValidateReturnType {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should avoid naming return types as ...ReturnType because this decreases flexibility. E.g. it may feel strange later to create something like:

const alwaysTrueValidation: ValidateReturnType = {...}

I think we should name it e.g. ValidationResult:

Suggested change
export interface ValidateReturnType {
export interface ValidationResult {

isValid: boolean;
messages: string[];
}

export const validateInput = <T>(value: T, validators: BaseValidator<T>[]): ValidateReturnType => {
const errors = validators
.filter((validator: BaseValidator<T>) => !validator.validate(value))
.map((validator: BaseValidator<T>) => validator.getErrorMessage());
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess this is somewhat fine, but it feels wrong to call filter first and then map to collect error messages :D

Suggested change
.filter((validator: BaseValidator<T>) => !validator.validate(value))
.map((validator: BaseValidator<T>) => validator.getErrorMessage());
.reduce((errors , validator) => {
const isValid = validator.validate(value);
if (isValid) {
continue;
}
return [...errors, validator.getErrorMessage()];
}, [])

this is longer but faster to read. :)


return { isValid: !errors.length, messages: errors };
};
13 changes: 13 additions & 0 deletions packages/components/src/validators/BaseValidator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { TranslatorType } from '@ids-context/Translator';

export default abstract class BaseValidator<T> {
protected _translator: TranslatorType;

constructor(translator: TranslatorType) {
this._translator = translator;
}

abstract getErrorMessage(): string;

abstract validate(_value: T): boolean;
}
13 changes: 13 additions & 0 deletions packages/components/src/validators/IsEmptyStringValidator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import BaseValidator from './BaseValidator';

export default class IsEmptyStringValidator extends BaseValidator<string> {
getErrorMessage(): string {
const Translator = this._translator;

return Translator.trans(/*@Desc("This field cannot be empty.")*/ 'ibexa.validators.is_empty_string');
}

validate(value: string): boolean {
return value.trim() !== '';
}
}