Skip to content

Commit

Permalink
Merge pull request #10515 from buddh4/feat-custom-file-pipe-builder-v…
Browse files Browse the repository at this point in the history
…alidators

feat(common): Allow adding custom validators to ParseFilePipeBuilder
  • Loading branch information
kamilmysliwiec committed Nov 7, 2022
2 parents 8039161 + a4a4d57 commit 9ffdffd
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
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

0 comments on commit 9ffdffd

Please sign in to comment.