refactor: improve domainRegex
This commit is contained in:
parent
5767d4595b
commit
8c64481320
@ -582,6 +582,10 @@ importers:
|
|||||||
dayjs:
|
dayjs:
|
||||||
specifier: 1.11.10
|
specifier: 1.11.10
|
||||||
version: 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:
|
website:
|
||||||
dependencies:
|
dependencies:
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
import { RuleObject } from 'antd/es/form';
|
import { RuleObject } from 'antd/es/form';
|
||||||
import { z } from 'zod';
|
import { z } from 'zod';
|
||||||
import { hostnameRegex, slugRegex } from '@tianji/shared';
|
import { hostnameRegex, slugRegex, domainRegex } from '@tianji/shared';
|
||||||
|
|
||||||
type Validator = (
|
type Validator = (
|
||||||
rule: RuleObject,
|
rule: RuleObject,
|
||||||
@ -24,7 +24,7 @@ export const domainValidator: Validator = (rule, value, callback) => {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
z.string().regex(hostnameRegex).parse(value);
|
z.string().regex(domainRegex).parse(value);
|
||||||
callback();
|
callback();
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
callback('Not valid, it should be domain, for example: example.com');
|
callback('Not valid, it should be domain, for example: example.com');
|
||||||
|
@ -10,6 +10,7 @@
|
|||||||
},
|
},
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"check:type": "tsc --noEmit --skipLibCheck --module esnext",
|
"check:type": "tsc --noEmit --skipLibCheck --module esnext",
|
||||||
|
"test": "vitest",
|
||||||
"build": "concurrently npm:build:cjs npm:build:esm",
|
"build": "concurrently npm:build:cjs npm:build:esm",
|
||||||
"build:cjs": "tsc --module commonjs --outDir ./dist/cjs",
|
"build:cjs": "tsc --module commonjs --outDir ./dist/cjs",
|
||||||
"build:esm": "tsc --module esnext --outDir ./dist/esm",
|
"build:esm": "tsc --module esnext --outDir ./dist/esm",
|
||||||
@ -19,5 +20,8 @@
|
|||||||
"author": "moonrailgun <moonrailgun@gmail.com>",
|
"author": "moonrailgun <moonrailgun@gmail.com>",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"dayjs": "^1.11.9"
|
"dayjs": "^1.11.9"
|
||||||
|
},
|
||||||
|
"devDependencies": {
|
||||||
|
"vitest": "^1.2.1"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
17
src/shared/src/regex.spec.ts
Normal file
17
src/shared/src/regex.spec.ts
Normal file
@ -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);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
@ -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])$/;
|
/^(([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 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]$/;
|
||||||
|
Loading…
Reference in New Issue
Block a user