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

fix(class-validator): return instance unless rawValues is set #617

Merged
merged 1 commit into from
Aug 17, 2023
Merged
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
41 changes: 41 additions & 0 deletions class-validator/src/__tests__/class-validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,19 @@ describe('classValidatorResolver', () => {
expect(schemaSpy).toHaveBeenCalledTimes(1);
expect(schemaSyncSpy).not.toHaveBeenCalled();
expect(result).toEqual({ errors: {}, values: validData });
expect(result.values).toBeInstanceOf(Schema);
});

it('should return values as a raw object from classValidatorResolver when `rawValues` set to true', async () => {
const result = await classValidatorResolver(Schema, undefined, {
rawValues: true,
})(validData, undefined, {
fields,
shouldUseNativeValidation,
});

expect(result).toEqual({ errors: {}, values: validData });
expect(result.values).not.toBeInstanceOf(Schema);
});

it('should return values from classValidatorResolver with `mode: sync` when validation pass', async () => {
Expand All @@ -33,6 +46,7 @@ describe('classValidatorResolver', () => {
expect(validateSyncSpy).toHaveBeenCalledTimes(1);
expect(validateSpy).not.toHaveBeenCalled();
expect(result).toEqual({ errors: {}, values: validData });
expect(result.values).toBeInstanceOf(Schema);
});

it('should return a single error from classValidatorResolver when validation fails', async () => {
Expand Down Expand Up @@ -145,3 +159,30 @@ it('validate data with validator option', async () => {

expect(result).toMatchSnapshot();
});

it('should return from classValidatorResolver with `excludeExtraneousValues` set to true', async () => {
class SchemaTest {
@Expose()
@IsNumber({}, { message: `Must be a number`, always: true })
random: number;
}

const result = await classValidatorResolver(SchemaTest, {
transformer: {
excludeExtraneousValues: true,
Copy link
Member

Choose a reason for hiding this comment

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

I don't find any documentation about excludeExtraneousValues and extraneousField. Do you have a link that explains these options?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure! Here it's explained: https://github.com/typestack/class-transformer#enforcing-type-safe-instance

This was just to make sure that the class-transformer acted correctly and didn't send extra fields, but we could use any other use case for the test if necessary (like the example in the codesandbox linked in the issue)

},
})(
{
random: 10,
extraneousField: true,
},
undefined,
{
fields,
shouldUseNativeValidation,
},
);

expect(result).toEqual({ errors: {}, values: { random: 10 } });
expect(result.values).toBeInstanceOf(SchemaTest);
});
5 changes: 4 additions & 1 deletion class-validator/src/class-validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,8 @@ export const classValidatorResolver: Resolver =

options.shouldUseNativeValidation && validateFieldsNatively({}, options);

return { values, errors: {} };
return {
values: resolverOptions.rawValues ? values : data,
errors: {},
};
};
Loading