-
Notifications
You must be signed in to change notification settings - Fork 389
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[form-builder] Show warning for array items without _type (#2020)
- Loading branch information
Showing
5 changed files
with
153 additions
and
45 deletions.
There are no files selected for viewing
49 changes: 20 additions & 29 deletions
49
packages/@sanity/form-builder/src/inputs/ArrayInput/InvalidItem.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,24 @@ | ||
import React from 'react' | ||
import {resolveTypeName} from '../../utils/resolveTypeName' | ||
import InvalidValue from '../InvalidValueInput' | ||
import PatchEvent from '../../PatchEvent' | ||
type Type = { | ||
name: string | ||
of: Array<Type> | ||
import {resolveTypeName} from '../../utils/resolveTypeName' | ||
import InvalidValueInput from '../InvalidValueInput' | ||
import {ArrayType, ItemValue} from './typedefs' | ||
|
||
interface Props { | ||
type: ArrayType | ||
value: unknown | ||
onChange: (event: PatchEvent, valueOverride?: ItemValue) => void | ||
} | ||
export default class Item extends React.PureComponent<{}, {}> { | ||
props: { | ||
// note: type here is the *array* type | ||
type: Type | ||
value: any | ||
onChange: (arg0: PatchEvent, value: any) => void | ||
} | ||
handleChange = (event: PatchEvent) => { | ||
const {onChange, value} = this.props | ||
onChange(event, value) | ||
} | ||
render() { | ||
const {type, value} = this.props | ||
const actualType = resolveTypeName(value) | ||
const validTypes = type.of.map(ofType => ofType.name) | ||
return ( | ||
<InvalidValue | ||
actualType={actualType} | ||
validTypes={validTypes} | ||
onChange={this.handleChange} | ||
value={value} | ||
/> | ||
) | ||
} | ||
|
||
export default function InvalidItem({value, type, onChange}: Props) { | ||
const actualType = resolveTypeName(value) | ||
const validTypes = type.of.map(ofType => ofType.name) | ||
return ( | ||
<InvalidValueInput | ||
actualType={actualType} | ||
validTypes={validTypes} | ||
onChange={onChange} | ||
value={value} | ||
/> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
packages/@sanity/form-builder/src/inputs/InvalidValueInput/UntypedValueInput.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import React from 'react' | ||
import schema from 'part:@sanity/base/schema' | ||
import DefaultButton from 'part:@sanity/components/buttons/default' | ||
import PatchEvent, {setIfMissing, unset} from '../../PatchEvent' | ||
import styles from '../ObjectInput/styles/UnknownFields.css' | ||
import Warning from '../Warning' | ||
|
||
type Props = { | ||
validTypes?: string[] | ||
value?: Record<string, unknown> | ||
onChange?: (event: PatchEvent, value?: Record<string, unknown>) => void | ||
} | ||
|
||
function SetMissingTypeButton({ | ||
value, | ||
targetType, | ||
onChange | ||
}: { | ||
value: Record<string, unknown> | ||
targetType: string | ||
onChange: Props['onChange'] | ||
}) { | ||
const itemValue = {...value, _type: targetType} | ||
return ( | ||
<DefaultButton | ||
onClick={() => onChange(PatchEvent.from(setIfMissing(targetType, ['_type'])), itemValue)} | ||
color="primary" | ||
> | ||
Set <code>_type</code> to <code>{targetType}</code> | ||
</DefaultButton> | ||
) | ||
} | ||
|
||
function UnsetItemButton({ | ||
value, | ||
onChange, | ||
validTypes | ||
}: { | ||
value: Record<string, unknown> | ||
validTypes: string[] | ||
onChange: Props['onChange'] | ||
}) { | ||
// Doesn't matter which `_type` we use as long as it's allowed by the array | ||
const itemValue = {...value, _type: validTypes[0]} | ||
return ( | ||
<DefaultButton onClick={() => onChange(PatchEvent.from(unset()), itemValue)} color="danger"> | ||
Remove value | ||
</DefaultButton> | ||
) | ||
} | ||
|
||
/** | ||
* When the value does not have an `_type` property, | ||
* but the schema has a named type | ||
*/ | ||
export function UntypedValueInput({validTypes, value, onChange}: Props) { | ||
const isSingleValidType = validTypes.length === 1 | ||
const isHoistedType = schema.has(validTypes[0]) | ||
const fix = isSingleValidType ? ( | ||
<SetMissingTypeButton onChange={onChange} targetType={validTypes[0]} value={value} /> | ||
) : null | ||
|
||
const message = ( | ||
<> | ||
Encountered an object value without a <code>_type</code> property. | ||
{isSingleValidType && !isHoistedType && ( | ||
<div> | ||
Either remove the <code>name</code> property of the object declaration, or set{' '} | ||
<code>_type</code> property on items. | ||
</div> | ||
)} | ||
{!isSingleValidType && ( | ||
<div> | ||
The following types are valid here according to schema:{' '} | ||
<ul> | ||
{validTypes.map(validType => ( | ||
<li key={validType}> | ||
<code>{validType}</code> | ||
</li> | ||
))} | ||
</ul> | ||
</div> | ||
)} | ||
<h4>object</h4> | ||
<pre className={styles.inspectValue}>{JSON.stringify(value, null, 2)}</pre> | ||
{fix} | ||
{fix && ' '} | ||
<UnsetItemButton onChange={onChange} validTypes={validTypes} value={value} /> | ||
</> | ||
) | ||
|
||
return <Warning heading="Content is missing _type" message={message} /> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,11 @@ | ||
import {resolveJSType} from './resolveJSType' | ||
|
||
export function resolveTypeName(value) { | ||
export function resolveTypeName(value: unknown): string { | ||
const jsType = resolveJSType(value) | ||
return (jsType === 'object' && '_type' in value && value._type) || jsType | ||
if (jsType !== 'object') { | ||
return jsType | ||
} | ||
|
||
const obj = value as Record<string, unknown> & {_type?: string} | ||
return ('_type' in obj && obj._type) || jsType | ||
} |
78d88e8
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs: