marmelab/react-admin · error · Error

useUnique: missing resource prop or context

Error message

useUnique: missing resource prop or context

What it means

The useUnique validation hook needs to know which resource (dataProvider endpoint) to query for duplicate values. It reads the resource from its options or from the surrounding ResourceContext; if neither exists it throws, since uniqueness cannot be checked without a target resource.

Source

Thrown at packages/ra-core/src/form/validation/useUnique.ts:59

 *             <ReferenceInput source="organization_id" reference="organizations" />
 *             <FormDataConsumer>
 *                 {({ formData }) => (
 *                     <TextInput
 *                         source="username"
 *                         validate={unique({ filter: { organization_id: formData.organization_id })}
 *                     />
 *                 )}
 *             </FormDataConsumer>
 *         </SimpleForm>
 *     );
 * }
 */
export const useUnique = (options?: UseUniqueOptions) => {
    const dataProvider = useDataProvider();
    const translateLabel = useTranslateLabel();
    const resource = useResourceContext(options);
    if (!resource) {
        throw new Error('useUnique: missing resource prop or context');
    }
    const translate = useTranslate();
    const record = useRecordContext();

    const debouncedGetList = useRef(
        // The initial value is here to set the correct type on useRef
        asyncDebounce(
            dataProvider.getList,
            options?.debounce ?? DEFAULT_DEBOUNCE
        )
    );

    const validateUnique = useCallback(
        (callTimeOptions?: UseUniqueOptions) => {
            const {
                message,
                filter,
                debounce: interval,

View on GitHub (pinned to 051f511bb0)

Solutions

  1. Pass resource explicitly: useUnique({ resource: 'posts' }) or unique('posts', ...).
  2. Render the form inside the correct <Resource> so ResourceContext supplies the resource.
  3. In tests, wrap the form in ResourceContext.Provider value="posts".
  4. Verify the validator is not invoked from a context that strips the resource (e.g. a generic modal at app root).

Example fix

// before
const validate = [required(), unique()]; // throws outside resource context
// after
const validate = [required(), unique('posts')];
Defensive patterns

Strategy: validation

Validate before calling

if (!resource) throw new Error('unique() requires a resource: pass unique("posts") or use inside a <Resource>');

Try / catch

try {
    const validate = [required(), unique()];
} catch (e) {
    if (e.message.includes('missing resource prop or context')) {
        console.error('pass resource in options or render inside a Resource context');
    }
}

Prevention

When it happens

Trigger: Using unique() / useUnique in a standalone validator outside a <Resource>/List/Show/Edit context and without passing resource in options; calling it in a storybook/demo page that renders the form outside any ResourceContext.Provider.

Common situations: Custom validation functions extracted into shared files and used outside resource context; tests rendering the form without ResourceContext; forms rendered at app root for creation flows without a matching Resource declaration.

Related errors


AI-assisted analysis of marmelab/react-admin@051f511bb0 (2026-08-30). Data as JSON: /api/errors/94fc861ddfe499e0. Report an issue: GitHub.