Browse Source

Fix validation of countries in import of asset profiles

pull/8358/head
Thomas Kaul 4 weeks ago
parent
commit
c576fdd4da
  1. 49
      libs/common/src/lib/dtos/country.dto.spec.ts
  2. 19
      libs/common/src/lib/dtos/country.dto.ts

49
libs/common/src/lib/dtos/country.dto.spec.ts

@ -0,0 +1,49 @@
import { plainToInstance } from 'class-transformer';
import { validateSync } from 'class-validator';
import { CountryDto } from './country.dto';
describe('CountryDto', () => {
const getInvalidProperties = (country: object) => {
return validateSync(plainToInstance(CountryDto, country), {
forbidNonWhitelisted: true,
whitelist: true
}).map(({ property }) => {
return property;
});
};
it('Accepts a country with a code and a weight', () => {
expect(getInvalidProperties({ code: 'US', weight: 1 })).toEqual([]);
});
it('Accepts a country of an export', () => {
expect(
getInvalidProperties({
code: 'US',
continent: 'North America',
name: 'United States',
weight: 1
})
).toEqual([]);
});
it('Rejects a continent and a name which are not strings', () => {
expect(
getInvalidProperties({ code: 'US', continent: 1, name: 2, weight: 1 })
).toEqual(['continent', 'name']);
});
it('Rejects an invalid code and a weight out of range', () => {
expect(getInvalidProperties({ code: 'USA', weight: 1.5 })).toEqual([
'code',
'weight'
]);
});
it('Rejects an unknown property', () => {
expect(
getInvalidProperties({ code: 'US', other: 'value', weight: 1 })
).toEqual(['other']);
});
});

19
libs/common/src/lib/dtos/country.dto.ts

@ -1,9 +1,26 @@
import { IsISO31661Alpha2, IsNumber, Max, Min } from 'class-validator';
import {
IsISO31661Alpha2,
IsNumber,
IsOptional,
IsString,
Max,
Min
} from 'class-validator';
export class CountryDto {
@IsISO31661Alpha2()
code: string;
/** Part of the export, not used because it is derived from the code */
@IsOptional()
@IsString()
continent?: string;
/** Part of the export, not used because it is derived from the code */
@IsOptional()
@IsString()
name?: string;
@IsNumber()
@Max(1)
@Min(0)

Loading…
Cancel
Save