|
1 | 1 | import type { ValidatorOptions } from './types'; |
2 | | -import { stripFormatting } from './utils'; |
| 2 | +import { isValidDate, mod10, stripFormatting } from './utils'; |
3 | 3 |
|
4 | 4 | type PostalCodeOptions = ValidatorOptions; |
5 | 5 |
|
@@ -84,5 +84,101 @@ export function validateOrganizationNumber( |
84 | 84 | return /^\d{10}$/.test(value); |
85 | 85 | } |
86 | 86 |
|
| 87 | +type NationalIdentityNumberFormat = 'short' | 'long'; |
| 88 | +type NationalIdentityNumberOptions = ValidatorOptions & { |
| 89 | + /** Specify this if you want to format to be only long (12 digits) or short (10 digits). By default, both formats are allowed */ |
| 90 | + format?: NationalIdentityNumberFormat; |
| 91 | +}; |
| 92 | + |
| 93 | +// the first two digts are optional, as they're the century in the long format version |
| 94 | +const PERSONNUMMER_FORMAT = /^(\d{2}){0,1}(\d{2})(\d{2})(\d{2})([+-]?)(\d{4})$/; |
| 95 | + |
| 96 | +/** |
| 97 | + * Validates that the input value is a Swedish national identity number (personnummer or samordningsnummer). |
| 98 | + * |
| 99 | + * It validates the control digits and checks if the date of birth is valid. |
| 100 | + * |
| 101 | + * @example |
| 102 | + * ``` |
| 103 | + * // Short format |
| 104 | + * validatePersonalIdentityNumber('YYMMDDXXXX') // => true |
| 105 | + * validatePersonalIdentityNumber('YYMMDD-XXXX', { allowFormatting: true }) // => true |
| 106 | + * |
| 107 | + * // Long format |
| 108 | + * validatePersonalIdentityNumber('YYYYMMDDXXXX') // => true |
| 109 | + * validatePersonalIdentityNumber('YYYYMMDD-XXXX', { allowFormatting: true }) // => true |
| 110 | + * ``` |
| 111 | + */ |
| 112 | +export function validateNationalIdentityNumber( |
| 113 | + value: string, |
| 114 | + options: NationalIdentityNumberOptions = {}, |
| 115 | +): boolean { |
| 116 | + const match = PERSONNUMMER_FORMAT.exec(value); |
| 117 | + |
| 118 | + if (!match) { |
| 119 | + return false; |
| 120 | + } |
| 121 | + |
| 122 | + const [_, centuryStr, yearStr, monthStr, dayStr, separator, rest] = match; |
| 123 | + |
| 124 | + if (centuryStr && options.format === 'short') { |
| 125 | + return false; |
| 126 | + } |
| 127 | + |
| 128 | + if (!centuryStr && options.format === 'long') { |
| 129 | + return false; |
| 130 | + } |
| 131 | + |
| 132 | + if (separator && !options.allowFormatting) { |
| 133 | + return false; |
| 134 | + } |
| 135 | + |
| 136 | + // when verifying the value, we must always use the short format, discarding the century |
| 137 | + // if we include the century it would generate a different checksum |
| 138 | + const isValid = mod10(`${yearStr}${monthStr}${dayStr}${rest}`); |
| 139 | + if (!isValid) { |
| 140 | + return false; |
| 141 | + } |
| 142 | + |
| 143 | + let year = 0; |
| 144 | + switch (true) { |
| 145 | + // if we have the long format version, we already have the full year |
| 146 | + case !!centuryStr: |
| 147 | + year = Number(centuryStr + yearStr); |
| 148 | + break; |
| 149 | + // otherwise, we can use the separator to determine the century of the personnummer |
| 150 | + // if the separator is '+', we know person is over a 100 years old |
| 151 | + // we can then calculate the full year |
| 152 | + case !!separator: { |
| 153 | + const date = new Date(); |
| 154 | + const baseYear = |
| 155 | + separator === '+' ? date.getUTCFullYear() - 100 : date.getUTCFullYear(); |
| 156 | + year = |
| 157 | + baseYear - ((baseYear - Number.parseInt(yearStr as string, 10)) % 100); |
| 158 | + break; |
| 159 | + } |
| 160 | + // if it's the short format, without a separator, we need to special handle the year for the date validation. |
| 161 | + // 1900 isn't a leap year, but 2000 is. Since JS two digits years to the Date constructor is an offset from the year 1900 |
| 162 | + // we need to special handle that case. For other cases it doesn't really matter if the year is 1925 or 2025. |
| 163 | + case yearStr === '00': |
| 164 | + year = 2000; |
| 165 | + break; |
| 166 | + // short version without separator |
| 167 | + default: |
| 168 | + year = Number(yearStr); |
| 169 | + } |
| 170 | + |
| 171 | + const month = Number(monthStr); |
| 172 | + |
| 173 | + let day = Number(dayStr); |
| 174 | + // for a samordningsnummer the day is increased by 60. Eg the 31st of a month would be 91, or the 3rd would be 63. |
| 175 | + // thus we need to subtract 60 to get the correct day of the month |
| 176 | + if (day > 60) { |
| 177 | + day = day - 60; |
| 178 | + } |
| 179 | + |
| 180 | + return isValidDate(year, month, day, Boolean(centuryStr || separator)); |
| 181 | +} |
| 182 | + |
87 | 183 | // just reexport the no method for API feature parity |
88 | 184 | export { validateObosMembershipNumber } from './no'; |
0 commit comments