Skip to content

Commit

Permalink
validation errors
Browse files Browse the repository at this point in the history
  • Loading branch information
jacoobia committed Jan 12, 2024
1 parent f1d41da commit 3de3a70
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 12 deletions.
5 changes: 5 additions & 0 deletions .changeset/silver-mayflies-melt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"consumejs": patch
---

update validator to allow for error messages (matching schemeit)
2 changes: 1 addition & 1 deletion __tests__/security/validation/validation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ describe('validator', () => {
middleware(mockRequest, mockResponse, mockController);

expect(mockResponse.reply).toHaveBeenCalledWith(400, {
errors: { foo: 'Invalid type: expected' }
errors: { foo: 'Invalid type, expected a number' }
});
});

Expand Down
12 changes: 9 additions & 3 deletions example/validation/userValidation.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import { Middleware, StringValidator, NumberValidator, createValidator } from '../../dist';
import { Middleware, StringValidator, createValidator, objectValidator } from '../../dist';

const ageValidator = objectValidator((object: unknown) => {
if (typeof object !== 'number') return false;
const age = Number(object);
return age > 18;
}, 'User is too young!');

export const addUserValidator: Middleware = createValidator({
firstname: StringValidator(),
surname: StringValidator(),
age: NumberValidator()
surname: StringValidator.optional(),
age: ageValidator()
});
3 changes: 2 additions & 1 deletion lib/@types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ export type EndpointDefinition = {
export type ElementValidator = {
validate: (object: unknown) => boolean;
optional?: boolean;
message?: string;
};

/** Options for validators */
Expand All @@ -77,7 +78,7 @@ export type ValidatorOptions = {
errorListName?: string;
};

/** A validation functopn */
/** A validation function */
export type ValidatorFunction = (() => ElementValidator) & { optional: () => ElementValidator };

/** Validation error response */
Expand Down
36 changes: 29 additions & 7 deletions lib/security/validation/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,46 @@ import {
* @param validateFunc The validator function
* @returns {ValidatorFunction} validator function
*/
export const objectValidator = (validateFunc: (object: unknown) => boolean): ValidatorFunction => {
const validatorFunc = (): ElementValidator => ({ validate: validateFunc, optional: false });
validatorFunc.optional = (): ElementValidator => ({ validate: validateFunc, optional: true });
export const objectValidator = (
validateFunc: (object: unknown) => boolean,
errorMessage?: string
): ValidatorFunction => {
const validatorFunc = (): ElementValidator => ({
validate: (object) => validateFunc(object),
message: errorMessage,
optional: false
});

validatorFunc.optional = (): ElementValidator => ({
validate: (object) => object === undefined || validateFunc(object),
message: errorMessage,
optional: true
});

return validatorFunc;
};

/**
* Default validator, validates a string
*/
export const StringValidator = objectValidator((object) => typeof object === 'string');
export const StringValidator = objectValidator(
(object) => typeof object === 'string',
'Invalid type, expected a string'
);
/**
* Default validator, validates a number
*/
export const NumberValidator = objectValidator((object) => typeof object === 'number');
export const NumberValidator = objectValidator(
(object) => typeof object === 'number',
'Invalid type, expected a number'
);
/**
* Default validator, validates a boolean
*/
export const BooleanValidator = objectValidator((object) => typeof object === 'boolean');
export const BooleanValidator = objectValidator(
(object) => typeof object === 'boolean',
'Invalid type, expected a boolean'
);

const extractPayload = (request: Request): UrlParams | RequestBody => {
if (request.body && Object.keys(request.body).length > 0) {
Expand Down Expand Up @@ -74,7 +96,7 @@ export const createValidator = (
errors[key] = 'Required element is missing or undefined';
}
} else if (!validator.validate(value)) {
errors[key] = 'Invalid type: expected';
errors[key] = validator.message ?? 'Invalid type';
}
}
} else {
Expand Down

0 comments on commit 3de3a70

Please sign in to comment.