Skip to content

Commit

Permalink
feat: change #toString() behaviour for arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
fedeci committed Mar 2, 2021
1 parent 42e1af4 commit 564341f
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 26 deletions.
15 changes: 8 additions & 7 deletions src/context-items/sanitization.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,26 +58,27 @@ describe('when sanitizer is a standard one', () => {
sanitization = new Sanitization(sanitizer, false);

await sanitization.run(context, false, meta);
expect(sanitizer).toHaveBeenLastCalledWith('false');
expect(sanitizer).toHaveBeenNthCalledWith(1, 'false');

await sanitization.run(context, 42, meta);
expect(sanitizer).toHaveBeenLastCalledWith('42');
expect(sanitizer).toHaveBeenNthCalledWith(2, '42');

// new Date(Date.UTC()) makes sure we'll not have to deal with timezones
await sanitization.run(context, new Date(Date.UTC(2019, 4, 1, 10, 30, 50, 0)), meta);
expect(sanitizer).toHaveBeenLastCalledWith('2019-05-01T10:30:50.000Z');
expect(sanitizer).toHaveBeenNthCalledWith(3, '2019-05-01T10:30:50.000Z');

await sanitization.run(context, null, meta);
expect(sanitizer).toHaveBeenLastCalledWith('');
expect(sanitizer).toHaveBeenNthCalledWith(4, '');

await sanitization.run(context, undefined, meta);
expect(sanitizer).toHaveBeenLastCalledWith('');
expect(sanitizer).toHaveBeenNthCalledWith(5, '');

await sanitization.run(context, [42, 1], meta);
expect(sanitizer).toHaveBeenLastCalledWith('42');
expect(sanitizer).toHaveBeenNthCalledWith(6, '42');
expect(sanitizer).toHaveBeenNthCalledWith(7, '1');

await sanitization.run(context, { toString: () => 'wow' }, meta);
expect(sanitizer).toHaveBeenLastCalledWith('wow');
expect(sanitizer).toHaveBeenNthCalledWith(8, 'wow');
});

it('calls it with the options', async () => {
Expand Down
15 changes: 11 additions & 4 deletions src/context-items/sanitization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,17 @@ export class Sanitization implements ContextItem {
return Promise.resolve(sanitizerValue);
};

const newValue = this.custom
? await runCustomSanitizer()
: (this.sanitizer as StandardSanitizer)(toString(value), ...this.options);
if (this.custom) {
const newValue = await runCustomSanitizer();
context.setData(path, newValue, location);
return;
}
const values = Array.isArray(value) ? value : [value];
const newValues = values.map(value => {
return (this.sanitizer as StandardSanitizer)(toString(value), ...this.options);
});

context.setData(path, newValue, location);
// We get only the first value of the array if the orginal value was wrapped.
context.setData(path, values !== value ? newValues[0] : newValues, location);
}
}
15 changes: 8 additions & 7 deletions src/context-items/standard-validation.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,26 +33,27 @@ const createTest = (options: { returnValue: any; addsError: boolean }) => async

it('calls the validator with the value as a string', async () => {
await validation.run(context, false, meta);
expect(validator).toHaveBeenLastCalledWith('false');
expect(validator).toHaveBeenNthCalledWith(1, 'false');

await validation.run(context, 42, meta);
expect(validator).toHaveBeenLastCalledWith('42');
expect(validator).toHaveBeenNthCalledWith(2, '42');

// new Date(Date.UTC()) makes sure we'll not have to deal with timezones
await validation.run(context, new Date(Date.UTC(2019, 4, 1, 10, 30, 50, 0)), meta);
expect(validator).toHaveBeenLastCalledWith('2019-05-01T10:30:50.000Z');
expect(validator).toHaveBeenNthCalledWith(3, '2019-05-01T10:30:50.000Z');

await validation.run(context, null, meta);
expect(validator).toHaveBeenLastCalledWith('');
expect(validator).toHaveBeenNthCalledWith(4, '');

await validation.run(context, undefined, meta);
expect(validator).toHaveBeenLastCalledWith('');
expect(validator).toHaveBeenNthCalledWith(5, '');

await validation.run(context, [42, 1], meta);
expect(validator).toHaveBeenLastCalledWith('42');
expect(validator).toHaveBeenNthCalledWith(6, '42');
expect(validator).toHaveBeenNthCalledWith(7, '1');

await validation.run(context, { toString: () => 'wow' }, meta);
expect(validator).toHaveBeenLastCalledWith('wow');
expect(validator).toHaveBeenNthCalledWith(8, 'wow');
});

it('calls the validator with the options', async () => {
Expand Down
11 changes: 7 additions & 4 deletions src/context-items/standard-validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,12 @@ export class StandardValidation implements ContextItem {
) {}

async run(context: Context, value: any, meta: Meta) {
const result = this.validator(toString(value), ...this.options);
if (this.negated ? result : !result) {
context.addError(this.message, value, meta);
}
const values = Array.isArray(value) ? value : [value];
values.forEach(value => {
const result = this.validator(toString(value), ...this.options);
if (this.negated ? result : !result) {
context.addError(this.message, value, meta);
}
});
}
}
5 changes: 5 additions & 0 deletions src/utils.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { toString } from './utils';

describe('#toString', () => {
it('arrays should be converted to a single string', () => {
const value = ['foo', 1, false];
expect(toString(value)).toEqual('foo,1,false');
});

it('calls custom toString function', () => {
const value = {
toString() {
Expand Down
6 changes: 2 additions & 4 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,8 @@ export const bindAll = <T>(object: T): { [K in keyof T]: T[K] } => {
return object;
};

export function toString(value: any, deep = true): string {
if (Array.isArray(value) && value.length && deep) {
return toString(value[0], false);
} else if (value instanceof Date) {
export function toString(value: any): string {
if (value instanceof Date) {
return value.toISOString();
} else if (value && typeof value === 'object' && value.toString) {
if (typeof value.toString !== 'function') {
Expand Down

0 comments on commit 564341f

Please sign in to comment.