Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(common): Allow adding custom validators to ParseFilePipeBuilder #10515

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 6 additions & 3 deletions packages/common/pipes/file/parse-file-pipe.builder.ts
Expand Up @@ -14,12 +14,15 @@ export class ParseFilePipeBuilder {
private validators: FileValidator[] = [];

addMaxSizeValidator(options: MaxFileSizeValidatorOptions) {
this.validators.push(new MaxFileSizeValidator(options));
return this;
return this.addValidator(new MaxFileSizeValidator(options));
}

addFileTypeValidator(options: FileTypeValidatorOptions) {
this.validators.push(new FileTypeValidator(options));
return this.addValidator(new FileTypeValidator(options));
}

addValidator(validator: FileValidator) {
this.validators.push(validator);
return this;
}

Expand Down
28 changes: 28 additions & 0 deletions packages/common/test/pipes/file/parse-file-pipe.builder.spec.ts
@@ -1,6 +1,8 @@
import { expect } from 'chai';
import {
FileTypeValidator,
FileTypeValidatorOptions,
FileValidator,
MaxFileSizeValidator,
ParseFilePipeBuilder,
} from '../../../pipes';
Expand Down Expand Up @@ -50,6 +52,32 @@ describe('ParseFilePipeBuilder', () => {
});
});

describe('when custom validator was chained', () => {
it('should return a ParseFilePipe with TestFileValidator and given options', () => {
class TestFileValidator extends FileValidator<{ name: string }> {
buildErrorMessage(file: any): string {
return 'TestFileValidator failed';
}

isValid(file: any): boolean | Promise<boolean> {
return true;
}
}

const options = {
name: 'test',
};

const parseFilePipe = parseFilePipeBuilder
.addValidator(new TestFileValidator(options))
.build();

expect(parseFilePipe.getValidators()).to.deep.include(
new TestFileValidator(options),
);
});
});

describe('when it is called twice with different validators', () => {
it('should not reuse validators', () => {
const maxSizeValidatorOptions = {
Expand Down