Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: skip validation on default value #1708

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion packages/runtime/src/routeGeneration/templateHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,10 @@ export class ValidationService {
return value;
}
}

// performance improvement: if the provided value is the default value, no need to validate
if( property.default === value ){
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if( property.default === value ){
if(property.default === value){

return value;
}
switch (property.dataType) {
case 'string':
return this.validateString(name, value, fieldErrors, property.validators as StringValidator, parent);
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/swagger/templateHelpers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ describe('ValidationService', () => {
describe('Param validate', () => {
it('should apply defaults for optional properties', () => {
const value = undefined;
const propertySchema: TsoaRoute.PropertySchema = { dataType: 'integer', default: '666', required: false, validators: {} };
const propertySchema: TsoaRoute.PropertySchema = { dataType: 'integer', default: 666, required: false, validators: {} };
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you revert these changes? At least while we don't check/fix when creating the metadata for validation.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If revert it, test fails... it's ok?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem then is that this is the schema with the default being a string is what we would generate "in the real world".

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah ok, so this PR can't work!

const result = new ValidationService(
{},
{
Expand All @@ -351,7 +351,7 @@ describe('ValidationService', () => {

it('should apply defaults for required properties', () => {
const value = undefined;
const propertySchema: TsoaRoute.PropertySchema = { dataType: 'integer', default: '666', required: true, validators: {} };
const propertySchema: TsoaRoute.PropertySchema = { dataType: 'integer', default: 666, required: true, validators: {} };
const result = new ValidationService(
{},
{
Expand Down
Loading