ソースを参照

added syntax check to proto schema

davitbejanyan 2 年 前
コミット
8d933881b8

+ 1 - 1
kafka-ui-react-app/src/components/Schemas/Edit/Form.tsx

@@ -55,7 +55,7 @@ const Form: React.FC = () => {
     yup.object().shape({
       newSchema:
         schema?.schemaType === SchemaType.PROTOBUF
-          ? yup.string().required().isEnum('Schema syntax is not valid')
+          ? yup.string().required().isSchema('Schema syntax is not valid')
           : yup.string().required().isJsonObject('Schema syntax is not valid'),
     });
   const methods = useForm<NewSchemaSubjectRaw>({

+ 9 - 8
kafka-ui-react-app/src/lib/yupExtended.ts

@@ -10,7 +10,7 @@ declare module 'yup' {
     TFlags extends yup.Flags = ''
   > extends yup.Schema<TType, TContext, TDefault, TFlags> {
     isJsonObject(message?: string): StringSchema<TType, TContext>;
-    isEnum(message?: string): StringSchema<TType, TContext>;
+    isSchema(message?: string): StringSchema<TType, TContext>;
   }
 }
 
@@ -41,12 +41,13 @@ const isJsonObject = (message?: string) => {
   );
 };
 
-export const isValidEnum = (value?: string) => {
+export const isValidSchema = (value?: string) => {
   try {
     if (!value) return false;
     const trimmedValue = value.trim();
     if (
-      trimmedValue.indexOf('enum') === 0 &&
+      (trimmedValue.indexOf('syntax') === 0 ||
+        trimmedValue.indexOf('enum') === 0) &&
       trimmedValue.lastIndexOf('}') === trimmedValue.length - 1
     ) {
       return true;
@@ -57,12 +58,12 @@ export const isValidEnum = (value?: string) => {
   return false;
 };
 
-const isEnum = (message?: string) => {
+const isSchema = (message?: string) => {
   return yup.string().test(
-    'isEnum',
+    'isSchema',
     // eslint-disable-next-line no-template-curly-in-string
-    message || '${path} is not Enum object',
-    isValidEnum
+    message || '${path} is not valid schema',
+    isValidSchema
   );
 };
 
@@ -88,7 +89,7 @@ export function cacheTest(
 }
 
 yup.addMethod(yup.StringSchema, 'isJsonObject', isJsonObject);
-yup.addMethod(yup.StringSchema, 'isEnum', isEnum);
+yup.addMethod(yup.StringSchema, 'isSchema', isSchema);
 
 export const topicFormValidationSchema = yup.object().shape({
   name: yup