From 8c6448132070ecc4402ca16c476d4a7e3255bd5c Mon Sep 17 00:00:00 2001 From: moonrailgun Date: Fri, 26 Apr 2024 00:37:06 +0800 Subject: [PATCH] refactor: improve domainRegex --- pnpm-lock.yaml | 4 ++++ src/client/utils/validator.ts | 4 ++-- src/shared/package.json | 4 ++++ src/shared/src/regex.spec.ts | 17 +++++++++++++++++ src/shared/src/regex.ts | 3 +++ 5 files changed, 30 insertions(+), 2 deletions(-) create mode 100644 src/shared/src/regex.spec.ts diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index f33ec37..0e2a8f3 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -582,6 +582,10 @@ importers: dayjs: specifier: 1.11.10 version: 1.11.10 + devDependencies: + vitest: + specifier: ^1.2.1 + version: 1.2.1(@types/node@18.17.12)(happy-dom@14.7.1) website: dependencies: diff --git a/src/client/utils/validator.ts b/src/client/utils/validator.ts index aa61629..b968c9a 100644 --- a/src/client/utils/validator.ts +++ b/src/client/utils/validator.ts @@ -1,6 +1,6 @@ import { RuleObject } from 'antd/es/form'; import { z } from 'zod'; -import { hostnameRegex, slugRegex } from '@tianji/shared'; +import { hostnameRegex, slugRegex, domainRegex } from '@tianji/shared'; type Validator = ( rule: RuleObject, @@ -24,7 +24,7 @@ export const domainValidator: Validator = (rule, value, callback) => { return; } - z.string().regex(hostnameRegex).parse(value); + z.string().regex(domainRegex).parse(value); callback(); } catch (err) { callback('Not valid, it should be domain, for example: example.com'); diff --git a/src/shared/package.json b/src/shared/package.json index 50fc24c..54c514f 100644 --- a/src/shared/package.json +++ b/src/shared/package.json @@ -10,6 +10,7 @@ }, "scripts": { "check:type": "tsc --noEmit --skipLibCheck --module esnext", + "test": "vitest", "build": "concurrently npm:build:cjs npm:build:esm", "build:cjs": "tsc --module commonjs --outDir ./dist/cjs", "build:esm": "tsc --module esnext --outDir ./dist/esm", @@ -19,5 +20,8 @@ "author": "moonrailgun ", "dependencies": { "dayjs": "^1.11.9" + }, + "devDependencies": { + "vitest": "^1.2.1" } } diff --git a/src/shared/src/regex.spec.ts b/src/shared/src/regex.spec.ts new file mode 100644 index 0000000..6b63bfa --- /dev/null +++ b/src/shared/src/regex.spec.ts @@ -0,0 +1,17 @@ +import { describe, expect, test } from 'vitest'; +import { domainRegex } from './regex'; + +describe('regex', () => { + describe('domainRegex', () => { + test.each([ + '555.123.4567', + 'www.demo.com', + 'bar.ba.test.co.uk', + 'g.com', + 'xn--d1ai6ai.xn--p1ai', + 'foodemo.net', + ])('test: %s', (input) => { + expect(domainRegex.test(input)).toBe(true); + }); + }); +}); diff --git a/src/shared/src/regex.ts b/src/shared/src/regex.ts index 1f60db9..ba51ac4 100644 --- a/src/shared/src/regex.ts +++ b/src/shared/src/regex.ts @@ -2,3 +2,6 @@ export const hostnameRegex = /^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9-]*[A-Za-z0-9])$/; export const slugRegex = /^[a-z0-9]+(?:-[a-z0-9]+)*$/; + +export const domainRegex = + /^(?:[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\.)+[a-z0-9][a-z0-9-]{0,61}[a-z0-9]$/;