added syntax check to proto schema
This commit is contained in:
parent
da3932e342
commit
8d933881b8
2 changed files with 10 additions and 9 deletions
|
@ -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>({
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Add table
Reference in a new issue