Skip to content

Commit

Permalink
feat: add string min and max length
Browse files Browse the repository at this point in the history
  • Loading branch information
ASafaeirad committed Dec 28, 2023
1 parent c07b7f2 commit e2d8e0e
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/Schema/NumberSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@ export class NumberSchema<TInput = any> extends Schema<TInput, number> {
});
}

min(min: number) {
public min(min: number) {
this.guards.push(new MinNumberGuard(min));
return this;
}

max(max: number) {
public max(max: number) {
this.guards.push(new MaxNumberGuard(max));
return this;
}
Expand Down
62 changes: 62 additions & 0 deletions src/Schema/StringSchema.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,66 @@ describe('String Schema', () => {

expect(schema.parse()).toBe('3000');
});

it('should throw when string length is less than min', () => {
const schema = new StringSchema({ coerce: false })
.min(3)
.setValue(' '.repeat(1));
schema.key = 'key';

expect(() => schema.validate()).toThrow(
new TypeError(
'Invalid configuration: The "key" length expected to be more than or equal to "3" but "1" was provided',
),
);
});

it('should not throw when string length is more than min', () => {
const schema = new StringSchema({ coerce: false })
.min(3)
.setValue(' '.repeat(4));
schema.key = 'key';

expect(() => schema.validate()).not.toThrow();
});

it('should not throw when string length is equal than min', () => {
const schema = new StringSchema({ coerce: false })
.min(3)
.setValue(' '.repeat(3));
schema.key = 'key';

expect(() => schema.validate()).not.toThrow();
});

it('should throw when string length is more than max', () => {
const schema = new StringSchema({ coerce: false })
.max(1)
.setValue(' '.repeat(3));
schema.key = 'key';

expect(() => schema.validate()).toThrow(
new TypeError(
'Invalid configuration: The "key" length expected to be less than or equal to "1" but "3" was provided',
),
);
});

it('should not throw when string length is less than max', () => {
const schema = new StringSchema({ coerce: false })
.max(4)
.setValue(' '.repeat(3));
schema.key = 'key';

expect(() => schema.validate()).not.toThrow();
});

it('should not throw when string length is equal than max', () => {
const schema = new StringSchema({ coerce: false })
.max(3)
.setValue(' '.repeat(3));
schema.key = 'key';

expect(() => schema.validate()).not.toThrow();
});
});
33 changes: 33 additions & 0 deletions src/Schema/StringSchema.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,30 @@
import type { Guard } from '../Guard';
import { Schema } from './Schema';
import type { SchemaOptions } from './SchemaOptions';
import { TypeGuard } from './TypeGuard';

class MinLengthGuard implements Guard<string> {
constructor(private min: number) {}

validate(input: string, field: string) {
if (input.length < this.min)
throw new RangeError(
`Invalid configuration: The "${field}" length expected to be more than or equal to "${this.min}" but "${input.length}" was provided`,
);
}
}

class MaxLengthGuard implements Guard<string> {
constructor(private max: number) {}

validate(input: string, field: string) {
if (input.length > this.max)
throw new RangeError(
`Invalid configuration: The "${field}" length expected to be less than or equal to "${this.max}" but "${input.length}" was provided`,
);
}
}

export class StringSchema<TInput = any> extends Schema<TInput, string> {
#type = 'string'; // eslint-disable-line no-unused-private-class-members

Expand All @@ -12,4 +35,14 @@ export class StringSchema<TInput = any> extends Schema<TInput, string> {
initialGuards: [new TypeGuard('string')],
});
}

public min(min: number) {
this.guards.push(new MinLengthGuard(min));
return this;
}

public max(max: number) {
this.guards.push(new MaxLengthGuard(max));
return this;
}
}

0 comments on commit e2d8e0e

Please sign in to comment.