From 3c65c116275a58cdfa7b14e0a6dc6f4f03796dff Mon Sep 17 00:00:00 2001 From: Per-Kristian Nordnes Date: Sat, 19 Jan 2019 15:13:41 +0100 Subject: [PATCH] [schema] Don't allow object types without a name when used with a block type --- .../schema/src/sanity/validation/types/array.js | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/packages/@sanity/schema/src/sanity/validation/types/array.js b/packages/@sanity/schema/src/sanity/validation/types/array.js index 648526b8219..4b8398b6e88 100644 --- a/packages/@sanity/schema/src/sanity/validation/types/array.js +++ b/packages/@sanity/schema/src/sanity/validation/types/array.js @@ -20,10 +20,25 @@ export default (typeDef, visitorContext) => { HELP_IDS.ARRAY_OF_INVALID ) ]) + const of = ofIsArray ? typeDef.of : [] + + // Don't allow object types without a name in block arrays + const hasObjectTypesWithoutName = of.some( + type => type.type === 'object' && typeof type.name === 'undefined' + ) + const hasBlockType = of.some(ofType => ofType.type === 'block') + if (hasBlockType && hasObjectTypesWithoutName) { + problems.push( + error( + "The array type's 'of' attribute can't have an object type without a .name when used in together with a block type", + HELP_IDS.ARRAY_OF_INVALID + ) + ) + } return { ...typeDef, - of: (ofIsArray ? typeDef.of : []).map(visitorContext.visit), + of: of.map(visitorContext.visit), _problems: problems } }