feat: uid validate (#681)

This commit is contained in:
chenos 2022-07-26 10:07:35 +08:00 committed by GitHub
parent d9b2bf8af1
commit 827c324be3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 38 additions and 8 deletions

View File

@ -1,4 +1,9 @@
import { registerValidateFormats } from '@formily/core';
export * from './AddFieldAction'; export * from './AddFieldAction';
export * from './ConfigurationTable'; export * from './ConfigurationTable';
export * from './EditFieldAction'; export * from './EditFieldAction';
registerValidateFormats({
uid: /^[A-Za-z0-9][A-Za-z0-9_-]*$/,
});

View File

@ -1,8 +1,5 @@
import { ISchema, Schema } from '@formily/react'; import { ISchema, Schema } from '@formily/react';
import { useTranslation } from 'react-i18next';
import { i18n } from '../../../i18n'; import { i18n } from '../../../i18n';
import { useRecord } from '../../../record-provider';
import { useCompile } from '../../../schema-component';
import { CollectionOptions } from '../../types'; import { CollectionOptions } from '../../types';
import { collectionFieldSchema } from './collectionFields'; import { collectionFieldSchema } from './collectionFields';
@ -121,6 +118,7 @@ export const collectionSchema: ISchema = {
name: { name: {
'x-component': 'CollectionField', 'x-component': 'CollectionField',
'x-decorator': 'FormItem', 'x-decorator': 'FormItem',
'x-validator': 'uid',
}, },
footer: { footer: {
type: 'void', type: 'void',

View File

@ -175,6 +175,7 @@ export const m2m: IField = {
"{{t('Randomly generated and can be modified. Support letters, numbers and underscores, must start with an letter.')}}", "{{t('Randomly generated and can be modified. Support letters, numbers and underscores, must start with an letter.')}}",
'x-decorator': 'FormItem', 'x-decorator': 'FormItem',
'x-component': 'Input', 'x-component': 'Input',
'x-validator': 'uid',
'x-disabled': '{{ !createOnly }}', 'x-disabled': '{{ !createOnly }}',
}, },
}, },
@ -208,6 +209,7 @@ export const m2m: IField = {
"{{t('Randomly generated and can be modified. Support letters, numbers and underscores, must start with an letter.')}}", "{{t('Randomly generated and can be modified. Support letters, numbers and underscores, must start with an letter.')}}",
'x-decorator': 'FormItem', 'x-decorator': 'FormItem',
'x-component': 'Input', 'x-component': 'Input',
'x-validator': 'uid',
'x-disabled': '{{ !createOnly }}', 'x-disabled': '{{ !createOnly }}',
}, },
}, },

View File

@ -181,6 +181,7 @@ export const m2o: IField = {
"{{t('Randomly generated and can be modified. Support letters, numbers and underscores, must start with an letter.')}}", "{{t('Randomly generated and can be modified. Support letters, numbers and underscores, must start with an letter.')}}",
'x-decorator': 'FormItem', 'x-decorator': 'FormItem',
'x-component': 'Input', 'x-component': 'Input',
'x-validator': 'uid',
'x-disabled': '{{ !createOnly }}', 'x-disabled': '{{ !createOnly }}',
}, },
}, },

View File

@ -207,6 +207,7 @@ export const o2m: IField = {
"{{t('Randomly generated and can be modified. Support letters, numbers and underscores, must start with an letter.')}}", "{{t('Randomly generated and can be modified. Support letters, numbers and underscores, must start with an letter.')}}",
'x-decorator': 'FormItem', 'x-decorator': 'FormItem',
'x-component': 'Input', 'x-component': 'Input',
'x-validator': 'uid',
'x-disabled': '{{ !createOnly }}', 'x-disabled': '{{ !createOnly }}',
}, },
}, },

View File

@ -198,6 +198,7 @@ export const o2o: IField = {
"{{t('Randomly generated and can be modified. Support letters, numbers and underscores, must start with an letter.')}}", "{{t('Randomly generated and can be modified. Support letters, numbers and underscores, must start with an letter.')}}",
'x-decorator': 'FormItem', 'x-decorator': 'FormItem',
'x-component': 'Input', 'x-component': 'Input',
'x-validator': 'uid',
'x-disabled': '{{ !createOnly }}', 'x-disabled': '{{ !createOnly }}',
}, },
}, },
@ -362,6 +363,7 @@ export const oho: IField = {
"{{t('Randomly generated and can be modified. Support letters, numbers and underscores, must start with an letter.')}}", "{{t('Randomly generated and can be modified. Support letters, numbers and underscores, must start with an letter.')}}",
'x-decorator': 'FormItem', 'x-decorator': 'FormItem',
'x-component': 'Input', 'x-component': 'Input',
'x-validator': 'uid',
'x-disabled': '{{ !createOnly }}', 'x-disabled': '{{ !createOnly }}',
}, },
}, },
@ -513,6 +515,7 @@ export const obo: IField = {
"{{t('Randomly generated and can be modified. Support letters, numbers and underscores, must start with an letter.')}}", "{{t('Randomly generated and can be modified. Support letters, numbers and underscores, must start with an letter.')}}",
'x-decorator': 'FormItem', 'x-decorator': 'FormItem',
'x-component': 'Input', 'x-component': 'Input',
'x-validator': 'uid',
'x-disabled': '{{ !createOnly }}', 'x-disabled': '{{ !createOnly }}',
}, },
}, },

View File

@ -218,6 +218,7 @@ export const defaultProps = {
'x-disabled': '{{ !createOnly }}', 'x-disabled': '{{ !createOnly }}',
'x-decorator': 'FormItem', 'x-decorator': 'FormItem',
'x-component': 'Input', 'x-component': 'Input',
'x-validator': 'uid',
description: description:
"{{t('Randomly generated and can be modified. Support letters, numbers and underscores, must start with an letter.')}}", "{{t('Randomly generated and can be modified. Support letters, numbers and underscores, must start with an letter.')}}",
}, },

View File

@ -8,17 +8,35 @@ export class UidField extends Field {
} }
init() { init() {
const { name, prefix = '' } = this.options; const { name, prefix = '', pattern } = this.options;
const { model } = this.context.collection; const re = new RegExp(pattern || '^[A-Za-z0-9][A-Za-z0-9_-]*$');
model.beforeCreate(async (instance) => { this.listener = async (instance) => {
if (!instance.get(name)) { const value = instance.get(name);
if (!value) {
instance.set(name, `${prefix}${uid()}`); instance.set(name, `${prefix}${uid()}`);
} else if (re.test(value)) {
instance.set(name, value);
} else {
throw new Error(`uid '${value}' is invalid`);
} }
}); };
}
bind() {
super.bind();
this.on('beforeCreate', this.listener);
this.on('beforeUpdate', this.listener);
}
unbind() {
super.unbind();
this.off('beforeCreate', this.listener);
this.off('beforeUpdate', this.listener);
} }
} }
export interface UidFieldOptions extends BaseColumnFieldOptions { export interface UidFieldOptions extends BaseColumnFieldOptions {
type: 'uid'; type: 'uid';
prefix?: string; prefix?: string;
pattern?: string;
} }

View File

@ -37,6 +37,7 @@ export default {
type: 'string', type: 'string',
title: '{{t("Email")}}', title: '{{t("Email")}}',
'x-component': 'Input', 'x-component': 'Input',
'x-validator': 'email',
require: true, require: true,
}, },
}, },