-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(richtext-lexical)!: various validation improvement (#6163)
BREAKING: this will now display errors if you're previously had invalid link or upload fields data - for example if you have a required field added to an uploads node and did not provide a value to it every time you've added an upload node
- Loading branch information
Showing
13 changed files
with
113 additions
and
28 deletions.
There are no files selected for viewing
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
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
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
51 changes: 51 additions & 0 deletions
51
packages/richtext-lexical/src/field/features/link/validate.ts
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,51 @@ | ||
import type { FieldWithRichTextRequiredEditor } from 'payload/types' | ||
|
||
import { buildStateFromSchema } from '@payloadcms/ui/forms/buildStateFromSchema' | ||
|
||
import type { NodeValidation } from '../types.js' | ||
import type { LinkFeatureServerProps } from './feature.server.js' | ||
import type { SerializedAutoLinkNode, SerializedLinkNode } from './nodes/types.js' | ||
|
||
export const linkValidation = ( | ||
props: LinkFeatureServerProps, | ||
// eslint-disable-next-line @typescript-eslint/no-duplicate-type-constituents | ||
): NodeValidation<SerializedAutoLinkNode | SerializedLinkNode> => { | ||
return async ({ | ||
node, | ||
validation: { | ||
options: { id, operation, preferences, req }, | ||
}, | ||
}) => { | ||
/** | ||
* Run buildStateFromSchema as that properly validates link fields and link sub-fields | ||
*/ | ||
|
||
const data = { | ||
...node.fields, | ||
text: 'ignored', | ||
} | ||
|
||
const result = await buildStateFromSchema({ | ||
id, | ||
data, | ||
fieldSchema: props.fields as FieldWithRichTextRequiredEditor[], // Sanitized in feature.server.ts | ||
operation: operation === 'create' || operation === 'update' ? operation : 'update', | ||
preferences, | ||
req, | ||
siblingData: data, | ||
}) | ||
|
||
let errorPaths = [] | ||
for (const fieldKey in result) { | ||
if (result[fieldKey].errorPaths) { | ||
errorPaths = errorPaths.concat(result[fieldKey].errorPaths) | ||
} | ||
} | ||
|
||
if (errorPaths.length) { | ||
return 'Link fields validation failed: ' + errorPaths.join(', ') | ||
} | ||
|
||
return true | ||
} | ||
} |
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
55 changes: 44 additions & 11 deletions
55
packages/richtext-lexical/src/field/features/upload/validate.ts
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
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
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