From 360b2c6a95058831dae7130fdf483721fba2faf6 Mon Sep 17 00:00:00 2001 From: Amir Rossert Date: Sun, 17 Aug 2025 15:28:44 +0300 Subject: [PATCH] Add `mac-address` format validation --- README.md | 1 + src/formats.ts | 3 +++ tests/extras/format.json | 16 ++++++++++++++++ 3 files changed, 20 insertions(+) diff --git a/README.md b/README.md index 8e3b918..14b0106 100644 --- a/README.md +++ b/README.md @@ -50,6 +50,7 @@ The package defines these formats: - _double_: double according to the [openApi 3.0.0 specification](https://spec.openapis.org/oas/v3.0.0#data-types) - _password_: password string according to the [openApi 3.0.0 specification](https://spec.openapis.org/oas/v3.0.0#data-types) - _binary_: binary string according to the [openApi 3.0.0 specification](https://spec.openapis.org/oas/v3.0.0#data-types) +- _mac-address_: MAC address format [RFC6901](https://tools.ietf.org/html/rfc6901). See regular expressions used for format validation and the sources that were used in [formats.ts](https://github.com/ajv-validator/ajv-formats/blob/master/src/formats.ts). diff --git a/src/formats.ts b/src/formats.ts index d3cde9b..1cb9972 100644 --- a/src/formats.ts +++ b/src/formats.ts @@ -30,6 +30,7 @@ export type FormatName = | "double" | "password" | "binary" + | "mac-address" export type DefinedFormats = { [key in FormatName]: Format @@ -92,6 +93,8 @@ export const fullFormats: DefinedFormats = { password: true, // unchecked string payload binary: true, + // mac-address: https://datatracker.ietf.org/doc/html/rfc9542 + "mac-address": /^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$/, } export const fastFormats: DefinedFormats = { diff --git a/tests/extras/format.json b/tests/extras/format.json index 1a72ec4..f4f2350 100644 --- a/tests/extras/format.json +++ b/tests/extras/format.json @@ -875,5 +875,21 @@ "valid": true } ] + }, + { + "description": "validation of mac address", + "schema": {"format": "mac-address"}, + "tests": [ + { + "description": "'mac-address string' is ok", + "data": "00:1A:2B:3C:4D:5E", + "valid": true + }, + { + "description": "'mac-address string' is ok", + "data": "00:1A:2B:", + "valid": false + } + ] } ]