Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[form-builder] Show warning on non-object array values #1173

Merged
merged 2 commits into from
Jan 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 39 additions & 3 deletions packages/@sanity/form-builder/src/inputs/ArrayInput/ArrayInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import React from 'react'
import ArrayFunctions from 'part:@sanity/form-builder/input/array/functions'
import {map} from 'rxjs/operators'
import {isPlainObject} from 'lodash'
import type {Uploader} from '../../sanity/uploads/typedefs'
import type {Marker, Type} from '../../typedefs'
import type {Path} from '../../typedefs/path'
Expand Down Expand Up @@ -261,6 +262,15 @@ export default class ArrayInput extends React.Component<Props, State> {
onChange(PatchEvent.from(...patches))
}

handleRemoveNonObjectValues = () => {
const {onChange, value} = this.props
const nonObjects = value
.reduce((acc, val, i) => (isPlainObject(val) ? acc : acc.concat(i)), [])
.reverse()
const patches = nonObjects.map(index => unset([index]))
onChange(PatchEvent.from(...patches))
}

handleUpload = ({file, type, uploader}) => {
const {onChange} = this.props
const item = createProtoValue(type)
Expand All @@ -280,6 +290,34 @@ export default class ArrayInput extends React.Component<Props, State> {

render() {
const {type, level, markers, readOnly, onChange, value} = this.props
const hasNonObjectValues = (value || []).some(item => !isPlainObject(item))
if (hasNonObjectValues) {
return (
<Fieldset
legend={type.title}
description={type.description}
level={level}
tabIndex={0}
onFocus={this.handleFocus}
ref={this.setElement}
markers={markers}
>
<div className={styles.nonObjectsWarning}>
Some items in this list are not objects. We need to remove them before the list can be
edited.
<div className={styles.removeNonObjectsButtonWrapper}>
<Button onClick={this.handleRemoveNonObjectValues}>Remove non-object values</Button>
</div>
<Details title={<b>Why is this happening?</b>}>
This usually happens when items are created through an API client from outside the
Content Studio and sets invalid data, or a custom input component have inserted
incorrect values into the list.
</Details>
</div>
</Fieldset>
)
}

const hasMissingKeys = (value || []).some(item => !item._key)
if (hasMissingKeys) {
return (
Expand All @@ -296,9 +334,7 @@ export default class ArrayInput extends React.Component<Props, State> {
Some items in this list are missing their keys. We need to fix this before the list can
be edited.
<div className={styles.fixMissingKeysButtonWrapper}>
<Button onClick={this.handleFixMissingKeys}>
Fix missing keys
</Button>
<Button onClick={this.handleFixMissingKeys}>Fix missing keys</Button>
</div>
<Details title={<b>Why is this happening?</b>}>
This usually happens when items are created through the API client from outside the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,12 @@
.fixMissingKeysButtonWrapper {
margin: 1em 0;
}

.nonObjectsWarning {
composes: warning;
padding: 1em;
}

.removeNonObjectsButtonWrapper {
composes: fixMissingKeysButtonWrapper;
}
15 changes: 13 additions & 2 deletions packages/@sanity/validation/src/validateDocument.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ function validateArray(items, type, path, options) {
}
// Validate items within array
const itemChecks = items.map((item, i) => {
const pathSegment = item._key ? {_key: item._key} : i
const pathSegment = item && item._key ? {_key: item._key} : i
const itemType = resolveTypeForArrayItem(item, type.of)
const itemPath = appendPath(path, [pathSegment])
return validateItem(item, itemType, itemPath, {
Expand All @@ -104,6 +104,17 @@ function validateArray(items, type, path, options) {
}

function validatePrimitive(item, type, path, options) {
if (!type) {
return [
{
type: 'validation',
level: 'error',
path,
item: new ValidationError('Unable to resolve type for item')
}
]
}

if (!type.validation) {
return []
}
Expand All @@ -118,7 +129,7 @@ function validatePrimitive(item, type, path, options) {
}

function resolveTypeForArrayItem(item, candidates) {
const primitive = !item._type && Type.string(item).toLowerCase()
const primitive = !item || (!item._type && Type.string(item).toLowerCase())
if (primitive) {
return candidates.find(candidate => candidate.jsonType === primitive)
}
Expand Down