Skip to content

Commit

Permalink
#375 Provision to add custom validation rules for field renderers
Browse files Browse the repository at this point in the history
  • Loading branch information
rajeev-k-tomy committed Nov 8, 2024
1 parent 07c92aa commit 8b94a08
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
8 changes: 8 additions & 0 deletions src/reactapp/src/customValidationRules.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/**
* Here you can define your own custom validation rules which will be applied
* to fields using field renderers.
*/
export default {
min_text_length: (schema, minLength) => schema.min(minLength),
max_text_length: (schema, maxLength) => schema.max(maxLength),
};
8 changes: 5 additions & 3 deletions src/reactapp/src/utils/validation.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { get as _get } from 'lodash-es';
import {
bool as YupBool,
array as YupArray,
string as YupString,
number as YupNumber,
} from 'yup';
import { get as _get } from 'lodash-es';

import { __ } from '../i18n';
import customValidationRules from '../customValidationRules';

const requiredMessage = __('%1 is required');

Expand All @@ -19,7 +20,9 @@ export const yupSchemaByFieldType = {

/**
* Bult-in support of rule mapping.
*
* It maps magento validation rules with corresponding yup rule here.
*
*/
export const yupValidationRules = {
required: (schema) => schema.required(requiredMessage),
Expand All @@ -31,9 +34,8 @@ export const yupValidationRules = {
(value, context) =>
!!_get(context, `parent.${ruleConfig.field}.${ruleConfig.index}`)
),
min_text_length: (schema, minLength) => schema.min(minLength),
max_text_length: (schema, maxLength) => schema.max(maxLength),
email: (schema) => schema.email(),
url: (schema) => schema.url(),
date: (schema) => schema.date(),
...customValidationRules,
};

0 comments on commit 8b94a08

Please sign in to comment.