mirror of
https://github.com/strapi/strapi.git
synced 2025-12-24 13:43:41 +00:00
Fix validations
Signed-off-by: soupette <cyril.lpz@gmail.com>
This commit is contained in:
parent
597c4ec374
commit
efa547aa31
52
examples/getstarted/api/baba/config/routes.json
Normal file
52
examples/getstarted/api/baba/config/routes.json
Normal file
@ -0,0 +1,52 @@
|
||||
{
|
||||
"routes": [
|
||||
{
|
||||
"method": "GET",
|
||||
"path": "/babas",
|
||||
"handler": "baba.find",
|
||||
"config": {
|
||||
"policies": []
|
||||
}
|
||||
},
|
||||
{
|
||||
"method": "GET",
|
||||
"path": "/babas/count",
|
||||
"handler": "baba.count",
|
||||
"config": {
|
||||
"policies": []
|
||||
}
|
||||
},
|
||||
{
|
||||
"method": "GET",
|
||||
"path": "/babas/:id",
|
||||
"handler": "baba.findOne",
|
||||
"config": {
|
||||
"policies": []
|
||||
}
|
||||
},
|
||||
{
|
||||
"method": "POST",
|
||||
"path": "/babas",
|
||||
"handler": "baba.create",
|
||||
"config": {
|
||||
"policies": []
|
||||
}
|
||||
},
|
||||
{
|
||||
"method": "PUT",
|
||||
"path": "/babas/:id",
|
||||
"handler": "baba.update",
|
||||
"config": {
|
||||
"policies": []
|
||||
}
|
||||
},
|
||||
{
|
||||
"method": "DELETE",
|
||||
"path": "/babas/:id",
|
||||
"handler": "baba.delete",
|
||||
"config": {
|
||||
"policies": []
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
8
examples/getstarted/api/baba/controllers/baba.js
Normal file
8
examples/getstarted/api/baba/controllers/baba.js
Normal file
@ -0,0 +1,8 @@
|
||||
'use strict';
|
||||
|
||||
/**
|
||||
* Read the documentation (https://strapi.io/documentation/v3.x/concepts/controllers.html#core-controllers)
|
||||
* to customize this controller
|
||||
*/
|
||||
|
||||
module.exports = {};
|
||||
8
examples/getstarted/api/baba/models/baba.js
Normal file
8
examples/getstarted/api/baba/models/baba.js
Normal file
@ -0,0 +1,8 @@
|
||||
'use strict';
|
||||
|
||||
/**
|
||||
* Read the documentation (https://strapi.io/documentation/v3.x/concepts/models.html#lifecycle-hooks)
|
||||
* to customize this model
|
||||
*/
|
||||
|
||||
module.exports = {};
|
||||
43
examples/getstarted/api/baba/models/baba.settings.json
Normal file
43
examples/getstarted/api/baba/models/baba.settings.json
Normal file
@ -0,0 +1,43 @@
|
||||
{
|
||||
"kind": "collectionType",
|
||||
"collectionName": "babas",
|
||||
"info": {
|
||||
"name": "testvalidationspublish",
|
||||
"description": ""
|
||||
},
|
||||
"options": {
|
||||
"increments": true,
|
||||
"timestamps": true,
|
||||
"draftAndPublish": true
|
||||
},
|
||||
"attributes": {
|
||||
"name": {
|
||||
"type": "string",
|
||||
"required": true
|
||||
},
|
||||
"namepasrep": {
|
||||
"type": "string"
|
||||
},
|
||||
"dz": {
|
||||
"type": "dynamiczone",
|
||||
"components": [
|
||||
"compos.comporeqf"
|
||||
],
|
||||
"required": true,
|
||||
"min": 1
|
||||
},
|
||||
"ref": {
|
||||
"type": "component",
|
||||
"repeatable": true,
|
||||
"component": "compos.comporeqf",
|
||||
"required": true,
|
||||
"min": 1
|
||||
},
|
||||
"rzeaer": {
|
||||
"type": "component",
|
||||
"repeatable": false,
|
||||
"component": "compos.comporeqf",
|
||||
"required": true
|
||||
}
|
||||
}
|
||||
}
|
||||
8
examples/getstarted/api/baba/services/baba.js
Normal file
8
examples/getstarted/api/baba/services/baba.js
Normal file
@ -0,0 +1,8 @@
|
||||
'use strict';
|
||||
|
||||
/**
|
||||
* Read the documentation (https://strapi.io/documentation/v3.x/concepts/services.html#core-services)
|
||||
* to customize this service
|
||||
*/
|
||||
|
||||
module.exports = {};
|
||||
52
examples/getstarted/api/tata/config/routes.json
Normal file
52
examples/getstarted/api/tata/config/routes.json
Normal file
@ -0,0 +1,52 @@
|
||||
{
|
||||
"routes": [
|
||||
{
|
||||
"method": "GET",
|
||||
"path": "/tatas",
|
||||
"handler": "tata.find",
|
||||
"config": {
|
||||
"policies": []
|
||||
}
|
||||
},
|
||||
{
|
||||
"method": "GET",
|
||||
"path": "/tatas/count",
|
||||
"handler": "tata.count",
|
||||
"config": {
|
||||
"policies": []
|
||||
}
|
||||
},
|
||||
{
|
||||
"method": "GET",
|
||||
"path": "/tatas/:id",
|
||||
"handler": "tata.findOne",
|
||||
"config": {
|
||||
"policies": []
|
||||
}
|
||||
},
|
||||
{
|
||||
"method": "POST",
|
||||
"path": "/tatas",
|
||||
"handler": "tata.create",
|
||||
"config": {
|
||||
"policies": []
|
||||
}
|
||||
},
|
||||
{
|
||||
"method": "PUT",
|
||||
"path": "/tatas/:id",
|
||||
"handler": "tata.update",
|
||||
"config": {
|
||||
"policies": []
|
||||
}
|
||||
},
|
||||
{
|
||||
"method": "DELETE",
|
||||
"path": "/tatas/:id",
|
||||
"handler": "tata.delete",
|
||||
"config": {
|
||||
"policies": []
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
8
examples/getstarted/api/tata/controllers/tata.js
Normal file
8
examples/getstarted/api/tata/controllers/tata.js
Normal file
@ -0,0 +1,8 @@
|
||||
'use strict';
|
||||
|
||||
/**
|
||||
* Read the documentation (https://strapi.io/documentation/v3.x/concepts/controllers.html#core-controllers)
|
||||
* to customize this controller
|
||||
*/
|
||||
|
||||
module.exports = {};
|
||||
8
examples/getstarted/api/tata/models/tata.js
Normal file
8
examples/getstarted/api/tata/models/tata.js
Normal file
@ -0,0 +1,8 @@
|
||||
'use strict';
|
||||
|
||||
/**
|
||||
* Read the documentation (https://strapi.io/documentation/v3.x/concepts/models.html#lifecycle-hooks)
|
||||
* to customize this model
|
||||
*/
|
||||
|
||||
module.exports = {};
|
||||
43
examples/getstarted/api/tata/models/tata.settings.json
Normal file
43
examples/getstarted/api/tata/models/tata.settings.json
Normal file
@ -0,0 +1,43 @@
|
||||
{
|
||||
"kind": "collectionType",
|
||||
"collectionName": "tatas",
|
||||
"info": {
|
||||
"name": "testvalidationsnormal",
|
||||
"description": ""
|
||||
},
|
||||
"options": {
|
||||
"increments": true,
|
||||
"timestamps": true,
|
||||
"draftAndPublish": false
|
||||
},
|
||||
"attributes": {
|
||||
"name": {
|
||||
"type": "string",
|
||||
"required": true
|
||||
},
|
||||
"namepasrep": {
|
||||
"type": "string"
|
||||
},
|
||||
"dz": {
|
||||
"type": "dynamiczone",
|
||||
"components": [
|
||||
"compos.comporeqf"
|
||||
],
|
||||
"required": true,
|
||||
"min": 1
|
||||
},
|
||||
"ref": {
|
||||
"type": "component",
|
||||
"repeatable": true,
|
||||
"component": "compos.comporeqf",
|
||||
"required": false,
|
||||
"min": 1
|
||||
},
|
||||
"rzeaer": {
|
||||
"type": "component",
|
||||
"repeatable": false,
|
||||
"component": "compos.comporeqf",
|
||||
"required": true
|
||||
}
|
||||
}
|
||||
}
|
||||
8
examples/getstarted/api/tata/services/tata.js
Normal file
8
examples/getstarted/api/tata/services/tata.js
Normal file
@ -0,0 +1,8 @@
|
||||
'use strict';
|
||||
|
||||
/**
|
||||
* Read the documentation (https://strapi.io/documentation/v3.x/concepts/services.html#core-services)
|
||||
* to customize this service
|
||||
*/
|
||||
|
||||
module.exports = {};
|
||||
17
examples/getstarted/components/compos/comporeqf.json
Normal file
17
examples/getstarted/components/compos/comporeqf.json
Normal file
@ -0,0 +1,17 @@
|
||||
{
|
||||
"collectionName": "components_compos_comporeqfs",
|
||||
"info": {
|
||||
"name": "comporeqf",
|
||||
"icon": "ad"
|
||||
},
|
||||
"options": {},
|
||||
"attributes": {
|
||||
"name": {
|
||||
"type": "string",
|
||||
"required": true
|
||||
},
|
||||
"namenot": {
|
||||
"type": "string"
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -289,7 +289,7 @@ const EditViewDataManagerProvider = ({
|
||||
{
|
||||
components: get(allLayoutData, 'components', {}),
|
||||
},
|
||||
{ isCreatingEntry, isDraft: shouldNotRunValidations }
|
||||
{ isCreatingEntry, isDraft: shouldNotRunValidations, isFromComponent: false }
|
||||
);
|
||||
let errors = {};
|
||||
const updatedData = cloneDeep(modifiedData);
|
||||
@ -371,7 +371,7 @@ const EditViewDataManagerProvider = ({
|
||||
{
|
||||
components: get(allLayoutData, 'components', {}),
|
||||
},
|
||||
{ isCreatingEntry, isDraft: shouldNotRunValidations }
|
||||
{ isCreatingEntry, isDraft: shouldNotRunValidations, isFromComponent: false }
|
||||
);
|
||||
|
||||
try {
|
||||
@ -501,7 +501,7 @@ const EditViewDataManagerProvider = ({
|
||||
{
|
||||
components: get(allLayoutData, 'components', {}),
|
||||
},
|
||||
{ isCreatingEntry }
|
||||
{ isCreatingEntry, isDraft: false, isFromComponent: false }
|
||||
);
|
||||
|
||||
try {
|
||||
@ -562,6 +562,7 @@ const EditViewDataManagerProvider = ({
|
||||
} catch (err) {
|
||||
console.error({ err });
|
||||
const errors = getYupInnerErrors(err);
|
||||
console.log({ errors });
|
||||
setStatus('resolved');
|
||||
|
||||
dispatch({
|
||||
|
||||
@ -224,6 +224,7 @@ const reducer = (state, action) => {
|
||||
case 'DELETE_SUCCEEDED':
|
||||
return state
|
||||
.update('isLoading', () => false)
|
||||
.update('formErrors', () => fromJS({}))
|
||||
.update('initialData', () => state.get('modifiedData'));
|
||||
case 'TRIGGER_FORM_VALIDATION':
|
||||
return state.update('shouldCheckErrors', v => {
|
||||
|
||||
@ -60,7 +60,7 @@ const getAttributes = data => get(data, ['schema', 'attributes'], {});
|
||||
const createYupSchema = (
|
||||
model,
|
||||
{ components },
|
||||
options = { isCreatingEntry: true, isDraft: true }
|
||||
options = { isCreatingEntry: true, isDraft: true, isFromComponent: false }
|
||||
) => {
|
||||
const attributes = getAttributes(model);
|
||||
|
||||
@ -90,16 +90,20 @@ const createYupSchema = (
|
||||
}
|
||||
|
||||
if (attribute.type === 'component') {
|
||||
const componentFieldSchema = createYupSchema(components[attribute.component], {
|
||||
components,
|
||||
});
|
||||
const componentFieldSchema = createYupSchema(
|
||||
components[attribute.component],
|
||||
{
|
||||
components,
|
||||
},
|
||||
{ ...options, isFromComponent: true }
|
||||
);
|
||||
|
||||
if (attribute.repeatable === true) {
|
||||
const { min, max, required } = attribute;
|
||||
let componentSchema = yup.lazy(value => {
|
||||
let baseSchema = yup.array().of(componentFieldSchema);
|
||||
|
||||
if (min) {
|
||||
if (min && !options.isDraft) {
|
||||
if (required) {
|
||||
baseSchema = baseSchema.min(min, errorsTrads.min);
|
||||
} else if (required !== true && isEmpty(value)) {
|
||||
@ -122,7 +126,7 @@ const createYupSchema = (
|
||||
}
|
||||
const componentSchema = yup.lazy(obj => {
|
||||
if (obj !== undefined) {
|
||||
return attribute.required === true
|
||||
return attribute.required === true && !options.isDraft
|
||||
? componentFieldSchema.defined()
|
||||
: componentFieldSchema.nullable();
|
||||
}
|
||||
@ -138,14 +142,17 @@ const createYupSchema = (
|
||||
if (attribute.type === 'dynamiczone') {
|
||||
let dynamicZoneSchema = yup.array().of(
|
||||
yup.lazy(({ __component }) => {
|
||||
return createYupSchema(components[__component], { components });
|
||||
return createYupSchema(
|
||||
components[__component],
|
||||
{ components },
|
||||
{ ...options, isFromComponent: true }
|
||||
);
|
||||
})
|
||||
);
|
||||
|
||||
const { max, min } = attribute;
|
||||
|
||||
if (attribute.required) {
|
||||
// dynamicZoneSchema = dynamicZoneSchema.required();
|
||||
if (attribute.required && !options.isDraft) {
|
||||
dynamicZoneSchema = dynamicZoneSchema.test('required', errorsTrads.required, value => {
|
||||
if (options.isCreatingEntry) {
|
||||
return value !== null || value !== undefined;
|
||||
@ -279,7 +286,7 @@ const createYupSchemaAttribute = (type, validations, options) => {
|
||||
} else {
|
||||
schema = schema.test('required', errorsTrads.required, value => {
|
||||
// Field is not touched and the user is editing the entry
|
||||
if (value === undefined) {
|
||||
if (value === undefined && !options.isFromComponent) {
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user