Skip to content

Commit

Permalink
feat(isLicensePlate): add support for pt-BR locale (#1588)
Browse files Browse the repository at this point in the history
* License Plate validators: pt-BR and mercosur

* Removing mercosur as it's not an actual valid country locale
  • Loading branch information
renanmontebelo committed Mar 12, 2021
1 parent 3c771e8 commit 7989e5b
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -131,7 +131,7 @@ Validator | Description
**isJWT(str)** | check if the string is valid JWT token.
**isLatLong(str [, options])** | check if the string is a valid latitude-longitude coordinate in the format `lat,long` or `lat, long`.<br/><br/>`options` is an object that defaults to `{ checkDMS: false }`. Pass `checkDMS` as `true` to validate DMS(degrees, minutes, and seconds) latitude-longitude format.
**isLength(str [, options])** | check if the string's length falls in a range.<br/><br/>`options` is an object which defaults to `{min:0, max: undefined}`. Note: this function takes into account surrogate pairs.
**isLicensePlate(str [, locale])** | check if string matches the format of a country's license plate.<br/><br/>(locale is one of `['de-DE', 'de-LI', 'pt-PT', 'sq-AL']` or `any`)
**isLicensePlate(str [, locale])** | check if string matches the format of a country's license plate.<br/><br/>(locale is one of `['de-DE', 'de-LI', 'pt-PT', 'sq-AL', 'pt-BR'']` or `any`).
**isLocale(str)** | check if the string is a locale
**isLowercase(str)** | check if the string is lowercase.
**isMACAddress(str)** | check if the string is a MAC address.<br/><br/>`options` is an object which defaults to `{no_separators: false}`. If `no_separators` is true, the validator will allow MAC addresses without separators. Also, it allows the use of hyphens, spaces or dots e.g '01 02 03 04 05 ab', '01-02-03-04-05-ab' or '0102.0304.05ab'.
Expand Down
2 changes: 2 additions & 0 deletions src/lib/isLicensePlate.js
Expand Up @@ -8,6 +8,8 @@ const validators = {
/^([A-Z]{2}|[0-9]{2})[ -·]?([A-Z]{2}|[0-9]{2})[ -·]?([A-Z]{2}|[0-9]{2})$/.test(str),
'sq-AL': str =>
/^[A-Z]{2}[- ]?((\d{3}[- ]?(([A-Z]{2})|T))|(R[- ]?\d{3}))$/.test(str),
'pt-BR': str =>
/^[A-Z]{3}[ -]?[0-9][A-Z][0-9]{2}|[A-Z]{3}[ -]?[0-9]{4}$/.test(str),
};

export default function isLicensePlate(str, locale) {
Expand Down
22 changes: 22 additions & 0 deletions test/validators.js
Expand Up @@ -10403,6 +10403,28 @@ describe('Validators', () => {
'AAA 00 AAA',
],
});
test({
validator: 'isLicensePlate',
args: ['pt-BR'],
valid: [
'ABC1234',
'ABC 1234',
'ABC-1234',
'ABC1D23',
'ABC1K23',
'ABC1Z23',
'ABC 1D23',
'ABC-1D23',
],
invalid: [
'',
'AA 0 A',
'AAA 00 AAA',
'ABCD123',
'AB12345',
'AB123DC',
],
});
test({
validator: 'isLicensePlate',
args: ['any'],
Expand Down

0 comments on commit 7989e5b

Please sign in to comment.