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
- Pass resource explicitly: useUnique({ resource: 'posts' }) or unique('posts', ...).
- Render the form inside the correct <Resource> so ResourceContext supplies the resource.
- In tests, wrap the form in ResourceContext.Provider value="posts".
- 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
- Pass resource explicitly when validators are shared outside resource pages.
- In tests, wrap forms in ResourceContext.Provider.
- Keep validators co-located with their Resource components when possible.
- Verify resource resolution with useResourceContext in dev builds.
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
- useUpdateMany mutation requires an array of ids
- useUpdateMany mutation requires a non-empty data object
- <InfiniteList> was called outside of a ResourceContext and w
- useListController requires a non-empty resource prop or cont
- useShowController requires a non-empty resource prop or cont
AI-assisted analysis of marmelab/react-admin@051f511bb0 (2026-08-30).
Data as JSON: /api/errors/94fc861ddfe499e0.
Report an issue: GitHub.