Skip to content
Closed
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
8 changes: 8 additions & 0 deletions packages/content/src/attributes/AttributeValueTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ import {
IPseudonym,
ISchematizedXML,
ISex,
ISocialInsuranceNumber,
IStreetAddress,
ISurname,
IWebsite,
Expand Down Expand Up @@ -116,6 +117,8 @@ import {
SchematizedXMLJSON,
Sex,
SexJSON,
SocialInsuranceNumber,
SocialInsuranceNumberJSON,
StreetAddress,
StreetAddressJSON,
Surname,
Expand Down Expand Up @@ -153,6 +156,7 @@ export namespace AttributeValues {
| PostOfficeBoxAddressJSON
| PseudonymJSON
| SexJSON
| SocialInsuranceNumberJSON
| StreetAddressJSON
| SurnameJSON
| WebsiteJSON;
Expand Down Expand Up @@ -181,6 +185,7 @@ export namespace AttributeValues {
| IPostOfficeBoxAddress
| IPseudonym
| ISex
| ISocialInsuranceNumber
| IStreetAddress
| ISurname
| IWebsite;
Expand Down Expand Up @@ -209,6 +214,7 @@ export namespace AttributeValues {
| PostOfficeBoxAddress
| Pseudonym
| Sex
| SocialInsuranceNumber
| StreetAddress
| Surname
| Website;
Expand Down Expand Up @@ -237,6 +243,7 @@ export namespace AttributeValues {
PostOfficeBoxAddress,
Pseudonym,
Sex,
SocialInsuranceNumber,
StreetAddress,
Surname,
Website
Expand Down Expand Up @@ -266,6 +273,7 @@ export namespace AttributeValues {
"PostOfficeBoxAddress",
"Pseudonym",
"Sex",
"SocialInsuranceNumber",
"StreetAddress",
"Surname",
"Website"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { serialize, type, validate } from "@js-soft/ts-serval";
import { ValueHints } from "../../hints";
import { AbstractString, AbstractStringJSON, IAbstractString } from "../AbstractString";

const MIN_SOCIAL_INSURANCE_NUMBER_LENGTH = 4;
const MAX_SOCIAL_INSURANCE_NUMBER_LENGTH = 32;

export interface SocialInsuranceNumberJSON extends AbstractStringJSON {
"@type": "SocialInsuranceNumber";
}

export interface ISocialInsuranceNumber extends IAbstractString {}

@type("SocialInsuranceNumber")
export class SocialInsuranceNumber extends AbstractString implements ISocialInsuranceNumber {
private static readonly regExp = new RegExp(/^[A-Z0-9]+$/);
@serialize()
@validate({
min: MIN_SOCIAL_INSURANCE_NUMBER_LENGTH,
max: MAX_SOCIAL_INSURANCE_NUMBER_LENGTH,
regExp: SocialInsuranceNumber.regExp
})
public override value: string;

public static from(value: ISocialInsuranceNumber | Omit<SocialInsuranceNumberJSON, "@type"> | string): SocialInsuranceNumber {
return this.fromAny(value);
}

public static override get valueHints(): ValueHints {
return super.valueHints.copyWith({
min: MIN_SOCIAL_INSURANCE_NUMBER_LENGTH,
max: MAX_SOCIAL_INSURANCE_NUMBER_LENGTH,
pattern: SocialInsuranceNumber.regExp.toString().slice(1, -1).replaceAll("/", "\\/")
});
}

public override toJSON(verbose?: boolean | undefined, serializeAsString?: boolean | undefined): SocialInsuranceNumberJSON {
return super.toJSON(verbose, serializeAsString) as SocialInsuranceNumberJSON;
}
}
1 change: 1 addition & 0 deletions packages/content/src/attributes/types/identity/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from "./DisplayName";
export * from "./IdentityFileReference";
export * from "./SchematizedXML";
export * from "./SocialInsuranceNumber";
2 changes: 1 addition & 1 deletion packages/content/test/attributes/EMailAddress.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ describe("Test invalid EMailAddresses", () => {
);
});

test("returns an error when trying to create an Attribute Value Type EMailAddress wich is empty", function () {
test("returns an error when trying to create an Attribute Value Type EMailAddress which is empty", function () {
const invalidEMailAddressCall = () => {
EMailAddress.from({
value: ""
Expand Down
42 changes: 42 additions & 0 deletions packages/content/test/attributes/SocialInsuranceNumber.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { ParsingError } from "@js-soft/ts-serval";
import { SocialInsuranceNumber } from "../../src";

describe("Test valid SocialInsuranceNumbers", () => {
const validSocialInsuranceNumbers = ["123456789", "VALIDNUMBER", "VALIDNUMBER123456789"];

test.each(validSocialInsuranceNumbers)("SocialInsuranceNumber %s is recognized as valid", (number) => {
const validSocialInsuranceNumber = SocialInsuranceNumber.from({ value: number });
expect(validSocialInsuranceNumber.value.toString()).toBe(number);
});
});

describe("Test invalid SocialInsuranceNumbers", () => {
const invalidSocialInsuranceNumbers = ["lowercaseinvalidnumber", "INVALIDNUMBER_WITHSPECIALCHAR$"];

test.each(invalidSocialInsuranceNumbers)("SocialInsuranceNumber %s is recognized as invalid", (number) => {
const invalidSocialInsuranceNumberCall = () => {
SocialInsuranceNumber.from({
value: number
});
};
expect(invalidSocialInsuranceNumberCall).toThrow(new ParsingError("SocialInsuranceNumber", "value", "Value does not match regular expression /^[A-Z0-9]+$/"));
});

test("returns an error when trying to create an Attribute Value Type SocialInsuranceNumber which is empty", function () {
const invalidSocialInsuranceNumberCall = () => {
SocialInsuranceNumber.from({
value: ""
});
};
expect(invalidSocialInsuranceNumberCall).toThrow(new ParsingError("SocialInsuranceNumber", "value", "Value is shorter than 4 characters"));
});

test("returns an error when trying to create an Attribute Value Type SocialInsuranceNumber which is too long", function () {
const invalidSocialInsuranceNumberCall = () => {
SocialInsuranceNumber.from({
value: "INVALIDNUMBERWITHTOOMANYCHARACTERS"
});
};
expect(invalidSocialInsuranceNumberCall).toThrow(new ParsingError("SocialInsuranceNumber", "value", "Value is longer than 32 characters"));
});
});
2 changes: 1 addition & 1 deletion packages/content/test/attributes/Website.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ describe("Test invalid URLs", () => {
);
});

test("returns an error when trying to create an Attribute Value Type Website wich is empty", function () {
test("returns an error when trying to create an Attribute Value Type Website which is empty", function () {
const invalidWebsiteCall = () => {
Website.from({
value: ""
Expand Down
Loading