feat: add monitor validator

This commit is contained in:
moonrailgun 2023-12-05 20:27:38 +08:00
parent 53b2c042f7
commit 987c09be2f
6 changed files with 36 additions and 6 deletions

View File

@ -6,6 +6,7 @@ import { MonitorStatsBlock } from '../MonitorStatsBlock';
import dayjs from 'dayjs'; import dayjs from 'dayjs';
import { isEmpty } from 'lodash-es'; import { isEmpty } from 'lodash-es';
import { useCurrentWorkspaceId } from '../../../store/user'; import { useCurrentWorkspaceId } from '../../../store/user';
import { z } from 'zod';
const MonitorHttp: React.FC = React.memo(() => { const MonitorHttp: React.FC = React.memo(() => {
return ( return (
@ -13,7 +14,19 @@ const MonitorHttp: React.FC = React.memo(() => {
<Form.Item <Form.Item
label="Url" label="Url"
name={['payload', 'url']} name={['payload', 'url']}
rules={[{ required: true }]} rules={[
{ required: true },
{
validator(rule, value, callback) {
try {
z.string().url().parse(value);
callback();
} catch (err) {
callback('Not valid http url');
}
},
},
]}
> >
<Input placeholder="https://example.com" /> <Input placeholder="https://example.com" />
</Form.Item> </Form.Item>

View File

@ -1,6 +1,8 @@
import { Form, Input } from 'antd'; import { Form, Input } from 'antd';
import React from 'react'; import React from 'react';
import { MonitorProvider } from './types'; import { MonitorProvider } from './types';
import { z } from 'zod';
import { hostnameRegex } from '../../../../shared';
export const MonitorPing: React.FC = React.memo(() => { export const MonitorPing: React.FC = React.memo(() => {
return ( return (
@ -8,7 +10,22 @@ export const MonitorPing: React.FC = React.memo(() => {
<Form.Item <Form.Item
label="Host" label="Host"
name={['payload', 'hostname']} name={['payload', 'hostname']}
rules={[{ required: true }]} rules={[
{ required: true },
{
validator(rule, value, callback) {
try {
z.union([
z.string().ip(),
z.string().regex(hostnameRegex),
]).parse(value);
callback();
} catch (err) {
callback('Not valid host, it should be ip or hostname');
}
},
},
]}
> >
<Input placeholder="example.com or 1.2.3.4" /> <Input placeholder="example.com or 1.2.3.4" />
</Form.Item> </Form.Item>

View File

@ -12,12 +12,12 @@ import {
FILTER_COLUMNS, FILTER_COLUMNS,
OPENAPI_TAG, OPENAPI_TAG,
SESSION_COLUMNS, SESSION_COLUMNS,
hostnameRegex,
} from '../../utils/const'; } from '../../utils/const';
import { parseDateRange } from '../../utils/common'; import { parseDateRange } from '../../utils/common';
import { getSessionMetrics, getPageviewMetrics } from '../../model/website'; import { getSessionMetrics, getPageviewMetrics } from '../../model/website';
import { websiteInfoSchema } from '../../model/_schema'; import { websiteInfoSchema } from '../../model/_schema';
import { OpenApiMeta } from 'trpc-openapi'; import { OpenApiMeta } from 'trpc-openapi';
import { hostnameRegex } from '../../../shared';
export const websiteRouter = router({ export const websiteRouter = router({
onlineCount: workspaceProcedure onlineCount: workspaceProcedure

View File

@ -120,6 +120,3 @@ export enum OPENAPI_TAG {
WEBSITE = 'Website', WEBSITE = 'Website',
MONITOR = 'Monitor', MONITOR = 'Monitor',
} }
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])$/gim;

View File

@ -1,2 +1,3 @@
export * from './date'; export * from './date';
export * from './server'; export * from './server';
export * from './regex';

2
src/shared/regex.ts Normal file
View File

@ -0,0 +1,2 @@
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])$/;