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

feat: add nationality def in common fields and in customer fields #771

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
12 changes: 12 additions & 0 deletions maas-schemas/schemas/core/components/common.json
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,18 @@
"type": "object",
"properties": {},
"additionalProperties": false
},
"nationality": {
"description": "ISO 3166-1 alpha-2 country code, see https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2",
"type": "string",
"pattern": "^[A-Z]{2,2}$",
"examples": ["FI", "GB"],
"invalid": {
"IiI=": "empty string",
"IvCfkqki": "emoji",
"ImZpIg==": "lower case alpha-2 country code",
"IkZJTiI=": "alpha-3 country code"
}
}
}
}
3 changes: 3 additions & 0 deletions maas-schemas/schemas/core/customer.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@
"zipCode": {
"$ref": "https://schemas.maas.global/core/components/address.json#/definitions/zipCode"
},
"nationality": {
"$ref": "https://schemas.maas.global/core/components/common.json#/definitions/nationality"
},
"locale": {
"$ref": "https://schemas.maas.global/core/components/i18n.json#/definitions/locale"
},
Expand Down
27 changes: 27 additions & 0 deletions maas-schemas/src/io-ts/_types/core/components/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,33 @@ export type EmptyObjectBrand = {
readonly EmptyObject: unique symbol;
};

// Nationality
// ISO 3166-1 alpha-2 country code, see https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2
export type Nationality = t.Branded<string, NationalityBrand>;
export type NationalityC = t.BrandC<t.StringC, NationalityBrand>;
export const Nationality: NationalityC = t.brand(
t.string,
(x): x is t.Branded<string, NationalityBrand> =>
typeof x !== 'string' || x.match(RegExp('^[A-Z]{2,2}$', 'u')) !== null,
'Nationality',
);
export type NationalityBrand = {
readonly Nationality: unique symbol;
};
/** require('io-ts-validator').validator(nonEmptyArray(Nationality)).decodeSync(examplesNationality) // => examplesNationality */
export const examplesNationality: NonEmptyArray<Nationality> = [
'FI',
'GB',
] as unknown as NonEmptyArray<Nationality>;
// NEGATIVE Test Case: empty string
/** require('io-ts-validator').validator(Nationality).decodeEither("")._tag // => 'Left' */
// NEGATIVE Test Case: emoji
/** require('io-ts-validator').validator(Nationality).decodeEither("💩")._tag // => 'Left' */
// NEGATIVE Test Case: lower case alpha-2 country code
/** require('io-ts-validator').validator(Nationality).decodeEither("fi")._tag // => 'Left' */
// NEGATIVE Test Case: alpha-3 country code
/** require('io-ts-validator').validator(Nationality).decodeEither("FIN")._tag // => 'Left' */

// Common
// The default export. More information at the top.
export type Common = t.Branded<unknown, CommonBrand>;
Expand Down
4 changes: 4 additions & 0 deletions maas-schemas/src/io-ts/_types/core/customer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ export type Customer = t.Branded<
state?: Address_2d0a_.State;
country?: Address_2d0a_.Country;
zipCode?: Address_2d0a_.ZipCode;
nationality?: Common_ffba_.Nationality;
locale?: I18n_b70d_.Locale;
appInstanceId?: Common_ffba_.AppInstanceId;
opaqueId?: Common_ffba_.OpaqueId;
Expand Down Expand Up @@ -118,6 +119,7 @@ export type CustomerC = t.BrandC<
state: typeof Address_2d0a_.State;
country: typeof Address_2d0a_.Country;
zipCode: typeof Address_2d0a_.ZipCode;
nationality: typeof Common_ffba_.Nationality;
locale: typeof I18n_b70d_.Locale;
appInstanceId: typeof Common_ffba_.AppInstanceId;
opaqueId: typeof Common_ffba_.OpaqueId;
Expand Down Expand Up @@ -204,6 +206,7 @@ export const Customer: CustomerC = t.brand(
state: Address_2d0a_.State,
country: Address_2d0a_.Country,
zipCode: Address_2d0a_.ZipCode,
nationality: Common_ffba_.Nationality,
locale: I18n_b70d_.Locale,
appInstanceId: Common_ffba_.AppInstanceId,
opaqueId: Common_ffba_.OpaqueId,
Expand Down Expand Up @@ -274,6 +277,7 @@ export const Customer: CustomerC = t.brand(
state?: Address_2d0a_.State;
country?: Address_2d0a_.Country;
zipCode?: Address_2d0a_.ZipCode;
nationality?: Common_ffba_.Nationality;
locale?: I18n_b70d_.Locale;
appInstanceId?: Common_ffba_.AppInstanceId;
opaqueId?: Common_ffba_.OpaqueId;
Expand Down