diff --git a/libs/common/src/lib/dtos/country.dto.spec.ts b/libs/common/src/lib/dtos/country.dto.spec.ts new file mode 100644 index 0000000000..66f15d32dd --- /dev/null +++ b/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']); + }); +}); diff --git a/libs/common/src/lib/dtos/country.dto.ts b/libs/common/src/lib/dtos/country.dto.ts index 7fc8c38ab3..236ecf97db 100644 --- a/libs/common/src/lib/dtos/country.dto.ts +++ b/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)