Skip to content
Merged
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ All notable changes to this project are documented here. The format follows [Kee

## [Unreleased]

### Added

- `validatePostalCode` / `isValidPostalCode` for six-digit Romanian postal code format validation

## [0.1.0] - 2026-10-27

### Added
Expand Down
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<a href="https://robert-georgedev.github.io/ValidRO/"><strong>Try it in your browser</strong></a>
</p>

Validate and decode Romanian identifiers (**CNP**, **CUI/CIF** and **IBAN**) with error messages you can show directly to users. Zero dependencies, fully typed, runs in Node and in the browser.
Validate and decode Romanian identifiers (**CNP**, **CUI/CIF**, **IBAN** and postal codes) with error messages you can show directly to users. Zero dependencies, fully typed, runs in Node and in the browser.

```bash
npm install validro
Expand Down Expand Up @@ -60,7 +60,7 @@ type ValidationResult<T> =
| { valid: false; error: string; data: null };
```

Only need true or false? Use `isValidCnp`, `isValidCui` and `isValidIbanRo`.
Only need true or false? Use `isValidCnp`, `isValidCui`, `isValidIbanRo` and `isValidPostalCode`.

## API

Expand All @@ -69,7 +69,8 @@ Only need true or false? Use `isValidCnp`, `isValidCui` and `isValidIbanRo`.
| `validateCnp(cnp, now?)` | `ValidationResult<CnpInfo>`; `now` sets the date used to reject future birth dates |
| `validateCui(cui)` | `ValidationResult<CuiInfo>` |
| `validateIbanRo(iban)` | `ValidationResult<IbanInfo>` |
| `isValidCnp`, `isValidCui`, `isValidIbanRo` | `boolean` |
| `validatePostalCode(postalCode)` | `ValidationResult<PostalCodeInfo>`; exactly 6 digits |
| `isValidCnp`, `isValidCui`, `isValidIbanRo`, `isValidPostalCode` | `boolean` |
| `cnpControlDigit(first12)` | the CNP control digit |
| `cuiControlDigit(body)` | the CUI control digit |
| `ibanChecksumOk(iban)` | mod-97 check, works for any country |
Expand Down Expand Up @@ -109,3 +110,5 @@ Ideas and bugs are welcome in [issues](https://github.com/Robert-GeorgeDev/Valid
## License

[MIT](LICENSE)


2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,7 @@ export { validateCui, isValidCui, cuiControlDigit } from "./cui.js";
export type { CuiInfo } from "./cui.js";
export { validateIbanRo, isValidIbanRo, ibanChecksumOk } from "./iban.js";
export type { IbanInfo } from "./iban.js";
export { validatePostalCode, isValidPostalCode } from "./postal-code.js";
export type { PostalCodeInfo } from "./postal-code.js";
export { CNP_COUNTIES } from "./counties.js";
export type { ValidationResult } from "./result.js";
14 changes: 14 additions & 0 deletions src/postal-code.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { fail, ok, type ValidationResult } from "./result.js";

export interface PostalCodeInfo {
postalCode: string;
}

/** Validates the six-digit format of a Romanian postal code. */
export function validatePostalCode(input: string): ValidationResult<PostalCodeInfo> {
if (typeof input !== "string") return fail("Codul poștal trebuie să fie un șir de caractere");
if (!/^\d{6}$/.test(input)) return fail("Codul poștal trebuie să conțină exact 6 cifre");
return ok({ postalCode: input });
}

export const isValidPostalCode = (input: string): boolean => validatePostalCode(input).valid;
11 changes: 11 additions & 0 deletions test/postal-code.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { describe, expect, it } from "vitest";
import { isValidPostalCode, validatePostalCode } from "../src/postal-code.js";

describe("Romanian postal code", () => {
it("accepts exactly six digits and rejects other formats", () => {
expect(validatePostalCode("010101")).toEqual({ valid: true, error: null, data: { postalCode: "010101" } });
expect(isValidPostalCode("12345")).toBe(false);
expect(isValidPostalCode("1234567")).toBe(false);
expect(isValidPostalCode("12345a")).toBe(false);
});
});