{"record":{"id":"94fc861ddfe499e0","repo":"marmelab/react-admin","slug":"useunique-missing-resource-prop-or-context","errorCode":null,"errorMessage":"useUnique: missing resource prop or context","messagePattern":"useUnique: missing resource prop or context","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/ra-core/src/form/validation/useUnique.ts","lineNumber":59,"sourceCode":" *             <ReferenceInput source=\"organization_id\" reference=\"organizations\" />\n *             <FormDataConsumer>\n *                 {({ formData }) => (\n *                     <TextInput\n *                         source=\"username\"\n *                         validate={unique({ filter: { organization_id: formData.organization_id })}\n *                     />\n *                 )}\n *             </FormDataConsumer>\n *         </SimpleForm>\n *     );\n * }\n */\nexport const useUnique = (options?: UseUniqueOptions) => {\n    const dataProvider = useDataProvider();\n    const translateLabel = useTranslateLabel();\n    const resource = useResourceContext(options);\n    if (!resource) {\n        throw new Error('useUnique: missing resource prop or context');\n    }\n    const translate = useTranslate();\n    const record = useRecordContext();\n\n    const debouncedGetList = useRef(\n        // The initial value is here to set the correct type on useRef\n        asyncDebounce(\n            dataProvider.getList,\n            options?.debounce ?? DEFAULT_DEBOUNCE\n        )\n    );\n\n    const validateUnique = useCallback(\n        (callTimeOptions?: UseUniqueOptions) => {\n            const {\n                message,\n                filter,\n                debounce: interval,","sourceCodeStart":41,"sourceCodeEnd":77,"githubUrl":"https://github.com/marmelab/react-admin/blob/051f511bb0afb5ea565c2d3728bf4dab0a6fa5e0/packages/ra-core/src/form/validation/useUnique.ts#L41-L77","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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)."],"exampleFix":"// before\nconst validate = [required(), unique()]; // throws outside resource context\n// after\nconst validate = [required(), unique('posts')];","handlingStrategy":"validation","validationCode":"if (!resource) throw new Error('unique() requires a resource: pass unique(\"posts\") or use inside a <Resource>');","typeGuard":null,"tryCatchPattern":"try {\n    const validate = [required(), unique()];\n} catch (e) {\n    if (e.message.includes('missing resource prop or context')) {\n        console.error('pass resource in options or render inside a Resource context');\n    }\n}","preventionTips":["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."],"tags":["react-admin","validation","context","developer-error"],"backgroundTag":"missing-context-provider","analyzedSha":"051f511bb0afb5ea565c2d3728bf4dab0a6fa5e0","analyzedAt":"2026-08-30T02:28:14.926Z","schemaVersion":2},"datasetVersion":"2026-08-30T03:17:51.788Z"}