drf-react-by-schema 0.20.9 → 0.21.0

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.
Files changed (55) hide show
  1. package/dist/@types/index.d.ts +4 -1
  2. package/dist/@types/index.d.ts.map +1 -1
  3. package/dist/api.d.ts.map +1 -1
  4. package/dist/api.js +1 -0
  5. package/dist/components/DataGridBySchemaEditable/DataGridDesktop.d.ts.map +1 -1
  6. package/dist/components/DataGridBySchemaEditable/DataGridDesktop.js +27 -29
  7. package/dist/components/DataGridBySchemaEditable.d.ts.map +1 -1
  8. package/dist/components/DataGridBySchemaEditable.js +1 -0
  9. package/dist/components/details/DetailBySchema.d.ts.map +1 -1
  10. package/dist/components/details/DetailBySchema.js +2 -2
  11. package/dist/components/details/DetailFieldBySchema.d.ts +1 -1
  12. package/dist/components/details/DetailFieldBySchema.d.ts.map +1 -1
  13. package/dist/components/details/DetailFieldBySchema.js +5 -1
  14. package/dist/components/forms/DialogFormBySchema.d.ts +1 -1
  15. package/dist/components/forms/DialogFormBySchema.d.ts.map +1 -1
  16. package/dist/components/forms/DialogFormBySchema.js +1 -1
  17. package/dist/components/forms/FieldBySchema.d.ts +1 -1
  18. package/dist/components/forms/FieldBySchema.d.ts.map +1 -1
  19. package/dist/components/forms/FieldBySchema.js +0 -3
  20. package/dist/components/forms/FormBySchema.d.ts.map +1 -1
  21. package/dist/components/forms/FormBySchema.js +4 -2
  22. package/dist/components/forms/inputs/AutocompleteFieldBySchema.d.ts +1 -1
  23. package/dist/components/forms/inputs/AutocompleteFieldBySchema.d.ts.map +1 -1
  24. package/dist/components/forms/inputs/AutocompleteFieldBySchema.js +10 -6
  25. package/dist/components/forms/inputs/BooleanFieldBySchema.d.ts +1 -1
  26. package/dist/components/forms/inputs/BooleanFieldBySchema.d.ts.map +1 -1
  27. package/dist/components/forms/inputs/BooleanFieldBySchema.js +8 -3
  28. package/dist/components/forms/inputs/DesktopDatePickerBySchema.d.ts +1 -1
  29. package/dist/components/forms/inputs/DesktopDatePickerBySchema.d.ts.map +1 -1
  30. package/dist/components/forms/inputs/DesktopDatePickerBySchema.js +9 -4
  31. package/dist/components/forms/inputs/DesktopDateTimePickerBySchema.d.ts +1 -1
  32. package/dist/components/forms/inputs/DesktopDateTimePickerBySchema.d.ts.map +1 -1
  33. package/dist/components/forms/inputs/DesktopDateTimePickerBySchema.js +8 -3
  34. package/dist/components/forms/inputs/EditableAutocompleteFieldBySchema.d.ts.map +1 -1
  35. package/dist/components/forms/inputs/EditableAutocompleteFieldBySchema.js +19 -14
  36. package/dist/components/forms/inputs/FileFieldBySchema.d.ts +1 -1
  37. package/dist/components/forms/inputs/FileFieldBySchema.d.ts.map +1 -1
  38. package/dist/components/forms/inputs/FileFieldBySchema.js +14 -10
  39. package/dist/components/forms/inputs/FloatFieldBySchema.d.ts +1 -1
  40. package/dist/components/forms/inputs/FloatFieldBySchema.d.ts.map +1 -1
  41. package/dist/components/forms/inputs/FloatFieldBySchema.js +13 -9
  42. package/dist/components/forms/inputs/PatternFieldBySchema.d.ts +1 -1
  43. package/dist/components/forms/inputs/PatternFieldBySchema.d.ts.map +1 -1
  44. package/dist/components/forms/inputs/PatternFieldBySchema.js +8 -4
  45. package/dist/components/forms/inputs/TextFieldBySchema.d.ts +1 -1
  46. package/dist/components/forms/inputs/TextFieldBySchema.d.ts.map +1 -1
  47. package/dist/components/forms/inputs/TextFieldBySchema.js +8 -4
  48. package/dist/context/APIWrapper.d.ts.map +1 -1
  49. package/dist/context/APIWrapper.js +17 -4
  50. package/dist/context/APIWrapperContext.d.ts +2 -1
  51. package/dist/context/APIWrapperContext.d.ts.map +1 -1
  52. package/dist/utils.d.ts +5 -2
  53. package/dist/utils.d.ts.map +1 -1
  54. package/dist/utils.js +63 -24
  55. package/package.json +1 -1
package/dist/utils.js CHANGED
@@ -53,6 +53,8 @@ exports.mergeFilterItems = mergeFilterItems;
53
53
  exports.initViewColumns = initViewColumns;
54
54
  exports.shouldMemoUpdate = shouldMemoUpdate;
55
55
  exports.formatFileSize = formatFileSize;
56
+ exports.getFieldsFromFieldsLayout = getFieldsFromFieldsLayout;
57
+ exports.isFieldRequired = isFieldRequired;
56
58
  const dayjs_1 = __importDefault(require("dayjs"));
57
59
  const Yup = __importStar(require("yup"));
58
60
  const string_mask_1 = __importDefault(require("string-mask"));
@@ -193,27 +195,28 @@ const getYupValidator = (type) => {
193
195
  }
194
196
  return yupFunc.transform((v) => (v === '' ? null : v)).nullable();
195
197
  };
196
- function buildGenericYupValidationSchema({ data, schema, many = false, skipFields = [], extraValidators, }) {
198
+ function buildGenericYupValidationSchema({ data, schema, many = false, skipFields = [], extraValidators, uiFields, }) {
197
199
  const yupValidator = {};
198
200
  for (const entry of Object.entries(schema)) {
199
- const [key, field] = entry;
200
- if (!data || !(key in data) || key === 'id' || skipFields.includes(key)) {
201
+ const [field, fieldSchema] = entry;
202
+ if (!data || !(field in data) || field === 'id' || skipFields.includes(field)) {
201
203
  continue;
202
204
  }
203
205
  // OneToMany or ManyToMany:
204
- if (field.type === 'field' && field.child) {
205
- yupValidator[key] =
206
- extraValidators && key in extraValidators
207
- ? extraValidators[key]
206
+ if (fieldSchema.type === 'field' && fieldSchema.child) {
207
+ yupValidator[field] =
208
+ extraValidators && field in extraValidators
209
+ ? extraValidators[field]
208
210
  : buildGenericYupValidationSchema({
209
- schema: field.child.children,
211
+ schema: fieldSchema.child.children,
210
212
  many: true,
211
- data: data[key],
213
+ data: data[field],
214
+ uiFields,
212
215
  });
213
216
  continue;
214
217
  }
215
218
  // Nested Object:
216
- if (field.type === 'nested object' && field.children) {
219
+ if (fieldSchema.type === 'nested object' && fieldSchema.children) {
217
220
  // yupValidator[key] = buildGenericYupValidationSchema({
218
221
  // schema: field.children,
219
222
  // many: false,
@@ -225,28 +228,28 @@ function buildGenericYupValidationSchema({ data, schema, many = false, skipField
225
228
  // }
226
229
  // continue;
227
230
  }
228
- yupValidator[key] =
229
- extraValidators && key in extraValidators
230
- ? extraValidators[key]
231
- : getYupValidator(field.type);
232
- if (field.required || field.model_required) {
233
- yupValidator[key] = yupValidator[key].required('Este campo é obrigatório');
231
+ yupValidator[field] =
232
+ extraValidators && field in extraValidators
233
+ ? extraValidators[field]
234
+ : getYupValidator(fieldSchema.type);
235
+ if (isFieldRequired(fieldSchema, (uiFields || []).includes(field))) {
236
+ yupValidator[field] = yupValidator[field].required('Este campo é obrigatório');
234
237
  }
235
- if (field.max_length && field.type === 'string') {
236
- yupValidator[key] = yupValidator[key].max(parseInt(field.max_length), `Este campo só pode ter no máximo ${field.max_length} caracteres`);
238
+ if (fieldSchema.max_length && fieldSchema.type === 'string') {
239
+ yupValidator[field] = yupValidator[field].max(parseInt(fieldSchema.max_length), `Este campo só pode ter no máximo ${fieldSchema.max_length} caracteres`);
237
240
  }
238
- if (field.max_digits && field.type === 'decimal') {
239
- const maxDigits = field.max_digits;
240
- yupValidator[key] = yupValidator[key].test('len', `Este número pode ter no máximo ${maxDigits} dígitos`, (val) => {
241
+ if (fieldSchema.max_digits && fieldSchema.type === 'decimal') {
242
+ const maxDigits = fieldSchema.max_digits;
243
+ yupValidator[field] = yupValidator[field].test('len', `Este número pode ter no máximo ${maxDigits} dígitos`, (val) => {
241
244
  if (!val) {
242
245
  return true;
243
246
  }
244
247
  return val.toString().length <= maxDigits;
245
248
  });
246
249
  }
247
- if (!field.read_only && field.validators_regex) {
248
- for (const validator of field.validators_regex) {
249
- yupValidator[key] = yupValidator[key].matches(validator.regex, validator.message);
250
+ if (!fieldSchema.read_only && fieldSchema.validators_regex) {
251
+ for (const validator of fieldSchema.validators_regex) {
252
+ yupValidator[field] = yupValidator[field].matches(validator.regex, validator.message);
250
253
  }
251
254
  }
252
255
  }
@@ -595,3 +598,39 @@ function formatFileSize(bytes) {
595
598
  });
596
599
  return `${formattedValue} ${units[i]}`;
597
600
  }
601
+ function getFieldsFromFieldsLayout(fieldsLayout) {
602
+ const fields = [];
603
+ for (const section of fieldsLayout) {
604
+ if (!section.rows) {
605
+ continue;
606
+ }
607
+ for (const row of section.rows) {
608
+ if (typeof row === 'string') {
609
+ fields.push(row);
610
+ continue;
611
+ }
612
+ if (Array.isArray(row)) {
613
+ for (const field of row) {
614
+ if (typeof field === 'string') {
615
+ fields.push(field);
616
+ continue;
617
+ }
618
+ }
619
+ continue;
620
+ }
621
+ }
622
+ }
623
+ return fields;
624
+ }
625
+ function isFieldRequired(fieldSchema, isInForm) {
626
+ if (fieldSchema === null || fieldSchema === void 0 ? void 0 : fieldSchema.required) {
627
+ return true;
628
+ }
629
+ if (fieldSchema === null || fieldSchema === void 0 ? void 0 : fieldSchema.model_required) {
630
+ return true;
631
+ }
632
+ if ((fieldSchema === null || fieldSchema === void 0 ? void 0 : fieldSchema.ui_required) && (typeof isInForm === 'undefined' || isInForm === true)) {
633
+ return true;
634
+ }
635
+ return false;
636
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "drf-react-by-schema",
3
- "version": "0.20.9",
3
+ "version": "0.21.0",
4
4
  "description": "Components and Tools for building a React App having Django Rest Framework (DRF) as server",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",