@flowgram.ai/form 0.2.30 → 0.2.32

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.mts CHANGED
@@ -266,6 +266,7 @@ declare class FormModel<TValues = any> implements Disposable {
266
266
  setInitValueIn<TValue = any>(name: FieldName, value: TValue): void;
267
267
  clearValueIn(name: FieldName): void;
268
268
  validateIn(name: FieldName): Promise<(string | FormErrorOptions | FormWarningOptions | undefined)[] | undefined>;
269
+ protected getValidateOptions(): Record<string, Validate> | undefined;
269
270
  validate(): Promise<FormValidateReturn>;
270
271
  alignStateWithFieldMap(): void;
271
272
  dispose(): void;
@@ -318,7 +319,7 @@ interface FormOptions<TValues = any> {
318
319
  /**
319
320
  * Form data's validation rules. It's a key value map, where the key is a pattern of data's path (or field name), the value is a validate function.
320
321
  */
321
- validate?: Record<string, Validate>;
322
+ validate?: Record<string, Validate> | ((value: TValues, ctx: Context) => Record<string, Validate>);
322
323
  /**
323
324
  * Custom context. It will be accessible via form instance or in validate function.
324
325
  */
package/dist/index.d.ts CHANGED
@@ -266,6 +266,7 @@ declare class FormModel<TValues = any> implements Disposable {
266
266
  setInitValueIn<TValue = any>(name: FieldName, value: TValue): void;
267
267
  clearValueIn(name: FieldName): void;
268
268
  validateIn(name: FieldName): Promise<(string | FormErrorOptions | FormWarningOptions | undefined)[] | undefined>;
269
+ protected getValidateOptions(): Record<string, Validate> | undefined;
269
270
  validate(): Promise<FormValidateReturn>;
270
271
  alignStateWithFieldMap(): void;
271
272
  dispose(): void;
@@ -318,7 +319,7 @@ interface FormOptions<TValues = any> {
318
319
  /**
319
320
  * Form data's validation rules. It's a key value map, where the key is a pattern of data's path (or field name), the value is a validate function.
320
321
  */
321
- validate?: Record<string, Validate>;
322
+ validate?: Record<string, Validate> | ((value: TValues, ctx: Context) => Record<string, Validate>);
322
323
  /**
323
324
  * Custom context. It will be accessible via form instance or in validate function.
324
325
  */
package/dist/index.js CHANGED
@@ -1424,14 +1424,15 @@ var FormModel = class {
1424
1424
  this.setValueIn(name, void 0);
1425
1425
  }
1426
1426
  async validateIn(name) {
1427
- if (!this._options.validate) {
1427
+ const validateOptions = this.getValidateOptions();
1428
+ if (!validateOptions) {
1428
1429
  return;
1429
1430
  }
1430
- const validateKeys = Object.keys(this._options.validate).filter(
1431
+ const validateKeys = Object.keys(validateOptions).filter(
1431
1432
  (pattern) => Glob.isMatch(pattern, name)
1432
1433
  );
1433
1434
  const validatePromises = validateKeys.map(async (validateKey) => {
1434
- const validate = this._options.validate[validateKey];
1435
+ const validate = validateOptions[validateKey];
1435
1436
  return validate({
1436
1437
  value: this.getValueIn(name),
1437
1438
  formValues: this.values,
@@ -1441,18 +1442,27 @@ var FormModel = class {
1441
1442
  });
1442
1443
  return Promise.all(validatePromises);
1443
1444
  }
1445
+ getValidateOptions() {
1446
+ const validate = this._options.validate;
1447
+ if (typeof validate === "function") {
1448
+ return validate(this.values, this.context);
1449
+ }
1450
+ return validate;
1451
+ }
1444
1452
  async validate() {
1445
- if (!this._options.validate) {
1453
+ const validateOptions = this.getValidateOptions();
1454
+ if (!validateOptions) {
1446
1455
  return [];
1447
1456
  }
1448
- const feedbacksArrPromises = Object.keys(this._options.validate).map(async (nameRule) => {
1449
- const validate = this._options.validate[nameRule];
1450
- const paths = Glob.findMatchPathsWithEmptyValue(this.values, nameRule);
1457
+ const feedbacksArrPromises = Object.keys(validateOptions).map(async (nameRule) => {
1458
+ const validate = validateOptions[nameRule];
1459
+ const values = this.values;
1460
+ const paths = Glob.findMatchPathsWithEmptyValue(values, nameRule);
1451
1461
  return Promise.all(
1452
1462
  paths.map(async (path) => {
1453
1463
  const result = await validate({
1454
- value: (0, import_lodash8.get)(this.values, path),
1455
- formValues: this.values,
1464
+ value: (0, import_lodash8.get)(values, path),
1465
+ formValues: values,
1456
1466
  context: this.context,
1457
1467
  name: path
1458
1468
  });