Skip to content

Commit

Permalink
fix: validate typing and config (#1388)
Browse files Browse the repository at this point in the history
  • Loading branch information
czy88840616 committed Nov 27, 2021
1 parent e4595d3 commit 0883569
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 6 deletions.
8 changes: 5 additions & 3 deletions packages/validate/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
export * from './dist/index';

import { validate } from './dist/config.default';
import * as Joi from 'joi';

declare module '@midwayjs/core/dist/interface' {
interface MidwayConfig {
validate?: validate;
validate?: {
validationOptions?: Joi.ValidationOptions;
errorStatus?: number;
};
}
}
2 changes: 1 addition & 1 deletion packages/validate/src/config.default.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export const validate = {
allowUnknown: false,
validationOptions: {},
errorStatus: 422,
};
10 changes: 8 additions & 2 deletions packages/validate/src/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,13 @@ export class ValidateService {
const rules = getClassExtendedMetadata(RULES_KEY, ClzType);
if (rules) {
const schema = Joi.object(rules);
const result = schema.validate(value);
const result = schema.validate(
value,
Object.assign(
this.validateConfig.validationOptions,
options.validateOptions ?? {}
)
);
if (result.error) {
throw new MidwayValidationError(
result.error.message,
Expand Down Expand Up @@ -82,7 +88,7 @@ export class ValidateConfiguration {
const result = this.validateService.validate(
item,
joinPoint.args[i],
options.metadata
options.metadata?.options
);
if (result && result.value) {
joinPoint.args[i] = result.value;
Expand Down
2 changes: 2 additions & 0 deletions packages/validate/src/decorator/validate.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { createCustomMethodDecorator } from '@midwayjs/decorator';
import { VALIDATE_KEY } from '../constants';
import * as Joi from 'joi';

export interface ValidateOptions {
errorStatus?: number;
validateOptions?: Joi.ValidationOptions;
}

export function Validate(options: ValidateOptions = {}) {
Expand Down
46 changes: 46 additions & 0 deletions packages/validate/test/check.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -365,4 +365,50 @@ describe('/test/check.test.ts', () => {
});
expect(typeof result.value.age).toEqual('number');
});

it('should test global validate config', async () => {
const app = await createLightApp('', {
configurationModule: [Valid]
});

class UserDTO {
@Rule(RuleType.number().max(10))
age: number;
}

@Provide()
class Hello {
@Validate({
validateOptions: {
allowUnknown: true,
},
errorStatus: 400
})
school(data: UserDTO) {
return data;
}
}
app.getApplicationContext().bind(Hello);
const hello = await app.getApplicationContext().getAsync(Hello);

let error;
try {
hello.school({
age: 11
});
} catch (err) {
error = err;
}

expect(error).toBeDefined();
expect(error.status).toEqual(400);

const result = hello.school({
age: 1,
name: 'hello',
} as any);
expect(result['name']).toEqual('hello');

await close(app);
});
});

0 comments on commit 0883569

Please sign in to comment.