Skip to content

Commit

Permalink
fix validation for Location_Collection and Service_Collection schemas
Browse files Browse the repository at this point in the history
  • Loading branch information
milewskibogumil committed Jan 7, 2025
1 parent daec0fc commit f685cdb
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 37 deletions.
38 changes: 21 additions & 17 deletions apps/sanity/schema/collectionTypes/Location_Collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,25 +30,29 @@ export default defineType({
const language = (context.parent as { language: string })?.language ?? 'pl';
return `/${language}/${slugify(slug)}`
},
validate: Rule => Rule.custom(async (value, context) => {
const servicePageRef = (context.parent as { servicePage: { _ref: string } })?.servicePage?._ref;
if (!servicePageRef) {
return 'The service page is required for Location Page.';
}

const client = context.getClient({ apiVersion: '2024-11-12' })
const servicePageSlug = await client.fetch(`*[_id == $ref][0].slug.current`, { ref: servicePageRef })
if (!value?.current?.startsWith(servicePageSlug)) {
return 'Slug should start with the slug of the service page.';
}
validate: Rule => [
Rule.custom(async (value, context) => {
const servicePageRef = (context.parent as { servicePage: { _ref: string } })?.servicePage?._ref;
if (!servicePageRef) {
return 'The service page is required for Location Page.';
}

const name = (context.parent as { name: string })?.name;
if (!value?.current?.includes(slugify(name))) {
return 'That slug doesn\'t match the name. Verify if it\'s correct.';
}
const client = context.getClient({ apiVersion: '2024-11-12' })
const servicePageSlug = await client.fetch(`*[_id == $ref][0].slug.current`, { ref: servicePageRef })
if (!value?.current?.startsWith(servicePageSlug)) {
return 'Slug should start with the slug of the service page.';
}

return true;
}).required()
return true;
}).required(),
Rule.custom((value, context) => {
const name = (context.parent as { name: string })?.name;
if (!value?.current?.includes(slugify(name))) {
return 'That slug doesn\'t match the name. Verify if it\'s correct.';
}
return true;
}).warning()
],
}),
defineField({
name: 'servicePage',
Expand Down
43 changes: 23 additions & 20 deletions apps/sanity/schema/collectionTypes/Service_Collection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,28 +32,31 @@ export default defineType({
}
return `/${language}/${slugify(slug)}`
},
validate: Rule => Rule.custom(async (value, context) => {
const language = (context.parent as { language: string })?.language ?? 'pl';
const hasMainPage = (context.parent as { hasMainPage: boolean })?.hasMainPage;
const mainPageRef = (context.parent as { mainPage: { _ref: string } })?.mainPage?._ref;
validate: Rule => [
Rule.custom(async (value, context) => {
const language = (context.parent as { language: string })?.language ?? 'pl';
const hasMainPage = (context.parent as { hasMainPage: boolean })?.hasMainPage;
const mainPageRef = (context.parent as { mainPage: { _ref: string } })?.mainPage?._ref;

if (hasMainPage && mainPageRef) {
const client = context.getClient({ apiVersion: '2024-11-12' })
const mainPageSlug = await client.fetch(`*[_id == $ref][0].slug.current`, { ref: mainPageRef })
if (!value?.current?.startsWith(mainPageSlug)) {
return 'Slug should start with the slug of the main page.';
if (hasMainPage && mainPageRef) {
const client = context.getClient({ apiVersion: '2024-11-12' })
const mainPageSlug = await client.fetch(`*[_id == $ref][0].slug.current`, { ref: mainPageRef })
if (!value?.current?.startsWith(mainPageSlug)) {
return 'Slug should start with the slug of the main page.';
}
} else if (!value?.current?.startsWith(`/${language}/`)) {
return `The slug should start with /${language}/`;
}
} else if (!value?.current?.startsWith(`/${language}/`)) {
return `The slug should start with /${language}/`;
}

const name = (context.parent as { name: string })?.name;
if (!value?.current?.includes(slugify(name))) {
return 'That slug doesn\'t match the name. Verify if it\'s correct.';
}

return true;
}).required()
return true;
}).required(),
Rule.custom((value, context) => {
const name = (context.parent as { name: string })?.name;
if (!value?.current?.includes(slugify(name))) {
return 'That slug doesn\'t match the name. Verify if it\'s correct.';
}
return true;
}).warning(),
]
}),
defineField({
name: 'hasMainPage',
Expand Down

0 comments on commit f685cdb

Please sign in to comment.