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.
- package/dist/@types/index.d.ts +4 -1
- package/dist/@types/index.d.ts.map +1 -1
- package/dist/api.d.ts.map +1 -1
- package/dist/api.js +1 -0
- package/dist/components/DataGridBySchemaEditable/DataGridDesktop.d.ts.map +1 -1
- package/dist/components/DataGridBySchemaEditable/DataGridDesktop.js +27 -29
- package/dist/components/DataGridBySchemaEditable.d.ts.map +1 -1
- package/dist/components/DataGridBySchemaEditable.js +1 -0
- package/dist/components/details/DetailBySchema.d.ts.map +1 -1
- package/dist/components/details/DetailBySchema.js +2 -2
- package/dist/components/details/DetailFieldBySchema.d.ts +1 -1
- package/dist/components/details/DetailFieldBySchema.d.ts.map +1 -1
- package/dist/components/details/DetailFieldBySchema.js +5 -1
- package/dist/components/forms/DialogFormBySchema.d.ts +1 -1
- package/dist/components/forms/DialogFormBySchema.d.ts.map +1 -1
- package/dist/components/forms/DialogFormBySchema.js +1 -1
- package/dist/components/forms/FieldBySchema.d.ts +1 -1
- package/dist/components/forms/FieldBySchema.d.ts.map +1 -1
- package/dist/components/forms/FieldBySchema.js +0 -3
- package/dist/components/forms/FormBySchema.d.ts.map +1 -1
- package/dist/components/forms/FormBySchema.js +4 -2
- package/dist/components/forms/inputs/AutocompleteFieldBySchema.d.ts +1 -1
- package/dist/components/forms/inputs/AutocompleteFieldBySchema.d.ts.map +1 -1
- package/dist/components/forms/inputs/AutocompleteFieldBySchema.js +10 -6
- package/dist/components/forms/inputs/BooleanFieldBySchema.d.ts +1 -1
- package/dist/components/forms/inputs/BooleanFieldBySchema.d.ts.map +1 -1
- package/dist/components/forms/inputs/BooleanFieldBySchema.js +8 -3
- package/dist/components/forms/inputs/DesktopDatePickerBySchema.d.ts +1 -1
- package/dist/components/forms/inputs/DesktopDatePickerBySchema.d.ts.map +1 -1
- package/dist/components/forms/inputs/DesktopDatePickerBySchema.js +9 -4
- package/dist/components/forms/inputs/DesktopDateTimePickerBySchema.d.ts +1 -1
- package/dist/components/forms/inputs/DesktopDateTimePickerBySchema.d.ts.map +1 -1
- package/dist/components/forms/inputs/DesktopDateTimePickerBySchema.js +8 -3
- package/dist/components/forms/inputs/EditableAutocompleteFieldBySchema.d.ts.map +1 -1
- package/dist/components/forms/inputs/EditableAutocompleteFieldBySchema.js +19 -14
- package/dist/components/forms/inputs/FileFieldBySchema.d.ts +1 -1
- package/dist/components/forms/inputs/FileFieldBySchema.d.ts.map +1 -1
- package/dist/components/forms/inputs/FileFieldBySchema.js +14 -10
- package/dist/components/forms/inputs/FloatFieldBySchema.d.ts +1 -1
- package/dist/components/forms/inputs/FloatFieldBySchema.d.ts.map +1 -1
- package/dist/components/forms/inputs/FloatFieldBySchema.js +13 -9
- package/dist/components/forms/inputs/PatternFieldBySchema.d.ts +1 -1
- package/dist/components/forms/inputs/PatternFieldBySchema.d.ts.map +1 -1
- package/dist/components/forms/inputs/PatternFieldBySchema.js +8 -4
- package/dist/components/forms/inputs/TextFieldBySchema.d.ts +1 -1
- package/dist/components/forms/inputs/TextFieldBySchema.d.ts.map +1 -1
- package/dist/components/forms/inputs/TextFieldBySchema.js +8 -4
- package/dist/context/APIWrapper.d.ts.map +1 -1
- package/dist/context/APIWrapper.js +17 -4
- package/dist/context/APIWrapperContext.d.ts +2 -1
- package/dist/context/APIWrapperContext.d.ts.map +1 -1
- package/dist/utils.d.ts +5 -2
- package/dist/utils.d.ts.map +1 -1
- package/dist/utils.js +63 -24
- 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 [
|
|
200
|
-
if (!data || !(
|
|
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 (
|
|
205
|
-
yupValidator[
|
|
206
|
-
extraValidators &&
|
|
207
|
-
? extraValidators[
|
|
206
|
+
if (fieldSchema.type === 'field' && fieldSchema.child) {
|
|
207
|
+
yupValidator[field] =
|
|
208
|
+
extraValidators && field in extraValidators
|
|
209
|
+
? extraValidators[field]
|
|
208
210
|
: buildGenericYupValidationSchema({
|
|
209
|
-
schema:
|
|
211
|
+
schema: fieldSchema.child.children,
|
|
210
212
|
many: true,
|
|
211
|
-
data: data[
|
|
213
|
+
data: data[field],
|
|
214
|
+
uiFields,
|
|
212
215
|
});
|
|
213
216
|
continue;
|
|
214
217
|
}
|
|
215
218
|
// Nested Object:
|
|
216
|
-
if (
|
|
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[
|
|
229
|
-
extraValidators &&
|
|
230
|
-
? extraValidators[
|
|
231
|
-
: getYupValidator(
|
|
232
|
-
if (
|
|
233
|
-
yupValidator[
|
|
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 (
|
|
236
|
-
yupValidator[
|
|
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 (
|
|
239
|
-
const maxDigits =
|
|
240
|
-
yupValidator[
|
|
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 (!
|
|
248
|
-
for (const validator of
|
|
249
|
-
yupValidator[
|
|
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