Last active
February 9, 2024 23:16
-
-
Save deviationist/f42ecce164e106863809ef2f35873437 to your computer and use it in GitHub Desktop.
Sanity unique e-mail field
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
defineField({ | |
name: 'email', | |
title: 'Email', | |
type: 'string', | |
readOnly: ({document}) => document?.origin === 'Checkin', | |
description: 'Can only be changed in Checkin if the origin is "Checkin".', | |
validation: Rule => Rule.required().email().custom(async (email: any, context: any) => { | |
const response = await client.fetch(`*[_type == "user" && email == $email && !(_id in [$draftId, $id])]`, {email, draftId: context.document._id, id: context.document._id.replace('drafts.', '')}); | |
if (response?.length > 0) { | |
return 'Email already in use'; | |
} | |
return true; | |
}), | |
group: 'userInformation', | |
}), |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment