From 005f8b52ad37d04037cb2634d2f1f9d59928906d Mon Sep 17 00:00:00 2001 From: Mathias Oterhals Myklebust Date: Wed, 11 Sep 2024 08:11:06 +0200 Subject: [PATCH] =?UTF-8?q?refactor(schema):=20rename=20Rule=20=E2=86=92?= =?UTF-8?q?=20rule=20in=20validation=20function?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- studio/schemas/builders/pageBuilder.ts | 2 +- studio/schemas/documents/benefit.ts | 2 +- studio/schemas/documents/blog.ts | 6 +-- studio/schemas/documents/navigationManager.ts | 4 +- studio/schemas/documents/post.ts | 12 ++--- studio/schemas/fields/categories.ts | 2 +- studio/schemas/fields/text.ts | 6 +-- .../compensations/benefitsByLocation.ts | 6 +-- .../compensations/bonusesByLocation.ts | 11 +++-- .../schemas/objects/compensations/pension.ts | 7 +-- .../compensations/salariesByLocation.ts | 8 ++-- studio/schemas/objects/footerSection.ts | 8 ++-- studio/schemas/objects/link.ts | 48 ++++++++++--------- .../schemas/objects/sections/callToAction.ts | 2 +- studio/schemas/objects/sections/form.ts | 5 +- studio/schemas/objects/sections/grid.ts | 8 ++-- studio/schemas/objects/sections/hero.ts | 6 +-- studio/schemas/objects/sections/image.ts | 2 +- studio/schemas/objects/sections/logoSalad.ts | 11 +++-- .../schemas/objects/sections/testimonials.ts | 5 +- studio/schemas/objects/seo.ts | 25 ++++++---- studio/schemas/objects/socialMedia.ts | 4 +- studio/schemas/schemaTypes/slug.ts | 4 +- studioShared/schemas/documents/blogPosts.ts | 4 +- 24 files changed, 105 insertions(+), 93 deletions(-) diff --git a/studio/schemas/builders/pageBuilder.ts b/studio/schemas/builders/pageBuilder.ts index 8fc82c4d9..d86c0c371 100644 --- a/studio/schemas/builders/pageBuilder.ts +++ b/studio/schemas/builders/pageBuilder.ts @@ -26,7 +26,7 @@ const pageBuilder = defineType({ description: "Enter a distinctive name for the dynamic page to help content editors easily identify and manage it. This name is used internally and is not visible on your website.", type: "string", - validation: (Rule) => Rule.required().max(30), + validation: (rule) => rule.required().max(30), components: { input: (props) => StringInputWithCharacterCount({ ...props, maxCount: 30 }), diff --git a/studio/schemas/documents/benefit.ts b/studio/schemas/documents/benefit.ts index d1794b83f..c9d5cdcda 100644 --- a/studio/schemas/documents/benefit.ts +++ b/studio/schemas/documents/benefit.ts @@ -24,7 +24,7 @@ const benefitType = defineField({ layout: BENEFIT_TYPES.length > 5 ? "dropdown" : "radio", }, initialValue: BENEFIT_TYPE_BASIC_VALUE, - validation: (Rule) => Rule.required(), + validation: (rule) => rule.required(), }); const benefit = defineType({ diff --git a/studio/schemas/documents/blog.ts b/studio/schemas/documents/blog.ts index 4d74eadba..18a5cd2ec 100644 --- a/studio/schemas/documents/blog.ts +++ b/studio/schemas/documents/blog.ts @@ -17,7 +17,7 @@ const blog = defineType({ description: "Enter a distinctive name for the page to help content editors easily identify and manage it. This name is used internally and is not visible on your website.", type: "string", - validation: (Rule) => Rule.required().max(30), + validation: (rule) => rule.required().max(30), components: { input: (props) => StringInputWithCharacterCount({ ...props, maxCount: 30 }), @@ -33,7 +33,7 @@ const blog = defineType({ "Enter the label used to refer to all posts regardless of their category. This label will be displayed in the filter section on the main blog page. Examples include 'news', 'stories', or 'posts'.", type: "string", initialValue: "All posts", - validation: (Rule) => Rule.required().max(20), + validation: (rule) => rule.required().max(20), components: { input: (props) => StringInputWithCharacterCount({ ...props, maxCount: 20 }), @@ -57,7 +57,7 @@ const blog = defineType({ description: "The name of the category. This will be displayed on the website and used for organizing blog posts.", type: "string", - validation: (Rule) => Rule.required().min(1).max(100), + validation: (rule) => rule.required().min(1).max(100), components: { input: (props) => StringInputWithCharacterCount({ ...props, maxCount: 100 }), diff --git a/studio/schemas/documents/navigationManager.ts b/studio/schemas/documents/navigationManager.ts index 24f5c3560..817e2cc83 100644 --- a/studio/schemas/documents/navigationManager.ts +++ b/studio/schemas/documents/navigationManager.ts @@ -36,8 +36,8 @@ const navigationManager = defineType({ "Add links to the main menu. These links will appear at the top of your website and help visitors navigate to important sections. The first Call to Action (CTA) will be styled as a primary link button. Note: The order in which you add the links here is how they will be displayed on the website.", type: "array", of: [{ type: linkID }, { type: callToActionFieldID }], - validation: (Rule) => - Rule.custom((links) => { + validation: (rule) => + rule.custom((links) => { if (!Array.isArray(links)) return true; const ctaCount = links.filter( (link) => link._type === callToActionFieldID, diff --git a/studio/schemas/documents/post.ts b/studio/schemas/documents/post.ts index 78ddf0854..62ad9690f 100644 --- a/studio/schemas/documents/post.ts +++ b/studio/schemas/documents/post.ts @@ -20,8 +20,8 @@ const posts = defineType({ description: "Select the date and time when this post will be published.", type: "datetime", initialValue: () => new Date().toISOString(), - validation: (Rule) => - Rule.required().custom((date, context) => { + validation: (rule) => + rule.required().custom((date, context) => { // Ensure date is not undefined or null if (!date) return "The publish date is required."; @@ -57,7 +57,7 @@ const posts = defineType({ components: { input: CategorySelector, }, - validation: (Rule) => Rule.required(), + validation: (rule) => rule.required(), }), defineField({ name: "lead", @@ -69,15 +69,15 @@ const posts = defineType({ defineField({ ...richText, description: "Enter the introductory text for the post.", - validation: (Rule) => Rule.required(), + validation: (rule) => rule.required(), }), defineField({ ...image, description: "Upload a featured image for the post.", - validation: (Rule) => Rule.required(), + validation: (rule) => rule.required(), }), ], - validation: (Rule) => Rule.required(), + validation: (rule) => rule.required(), }), defineField({ ...richText, diff --git a/studio/schemas/fields/categories.ts b/studio/schemas/fields/categories.ts index 0fc2f6d6e..bb286fbb0 100644 --- a/studio/schemas/fields/categories.ts +++ b/studio/schemas/fields/categories.ts @@ -12,7 +12,7 @@ const categories = defineField({ name: "category", type: "string", title: "Category", - validation: (Rule) => Rule.required().min(1).max(100), + validation: (rule) => rule.required().min(1).max(100), components: { input: (props: StringInputProps) => StringInputWithCharacterCount({ ...props, maxCount: 100 }), diff --git a/studio/schemas/fields/text.ts b/studio/schemas/fields/text.ts index 449387a60..b120b6ddb 100644 --- a/studio/schemas/fields/text.ts +++ b/studio/schemas/fields/text.ts @@ -23,8 +23,8 @@ const createField = ({ isRequired = false, maxLength = 60, }: CreateFieldProps) => { - const validationRules = (Rule: StringRule) => { - let rules = Rule.max(maxLength); + const validationRules = (rule: StringRule) => { + let rules = rule.max(maxLength); if (isRequired) { rules = rules.required(); } @@ -58,7 +58,7 @@ export const optionalSubtitle = defineField({ name: subtitleID.optional, title: "Subtitle", type: "string", - validation: (Rule) => Rule.max(60), + validation: (rule) => rule.max(60), components: { input: (props) => StringInputWithCharacterCount({ ...props, maxCount: 60 }), }, diff --git a/studio/schemas/objects/compensations/benefitsByLocation.ts b/studio/schemas/objects/compensations/benefitsByLocation.ts index e49b6a730..c34a4a7c3 100644 --- a/studio/schemas/objects/compensations/benefitsByLocation.ts +++ b/studio/schemas/objects/compensations/benefitsByLocation.ts @@ -21,7 +21,7 @@ export const benefitsByLocation = defineField({ ...location, description: "Select the office location for which you are entering benefits information. Each location must be unique.", - validation: (Rule) => Rule.required(), + validation: (rule) => rule.required(), }, defineField({ name: "benefitsGroup", @@ -47,8 +47,8 @@ export const benefitsByLocation = defineField({ }, }, ], - validation: (Rule) => - Rule.custom((benefitsByLocation) => { + validation: (rule) => + rule.custom((benefitsByLocation) => { const isNotDuplicate: boolean = checkForDuplicateLocations( benefitsByLocation as DocumentWithLocation[] | undefined, ); diff --git a/studio/schemas/objects/compensations/bonusesByLocation.ts b/studio/schemas/objects/compensations/bonusesByLocation.ts index 7e2a443e5..d0cb16280 100644 --- a/studio/schemas/objects/compensations/bonusesByLocation.ts +++ b/studio/schemas/objects/compensations/bonusesByLocation.ts @@ -24,7 +24,7 @@ export const bonusesByLocation = defineField({ ...location, description: "Select the company location for which you are entering the average bonus information. Each location must be unique.", - validation: (Rule) => Rule.required(), + validation: (rule) => rule.required(), }, defineField({ name: "averageBonus", @@ -32,8 +32,9 @@ export const bonusesByLocation = defineField({ description: "Enter the average bonus amount for this location. Ensure the amount is positive and reflective of the compensation package for this location.", type: "number", - validation: (Rule) => - Rule.required() + validation: (rule) => + rule + .required() .min(0) .error("Please enter a positive bonus amount."), }), @@ -52,8 +53,8 @@ export const bonusesByLocation = defineField({ }, }), ], - validation: (Rule) => - Rule.custom((bonusesByLocation) => { + validation: (rule) => + rule.custom((bonusesByLocation) => { const isNotDuplicate: boolean = checkForDuplicateLocations( bonusesByLocation as DocumentWithLocation[] | undefined, ); diff --git a/studio/schemas/objects/compensations/pension.ts b/studio/schemas/objects/compensations/pension.ts index d7345b407..50c7e139d 100644 --- a/studio/schemas/objects/compensations/pension.ts +++ b/studio/schemas/objects/compensations/pension.ts @@ -6,11 +6,12 @@ export const pension = defineField({ type: "number", initialValue: 7, description: `Specify the percentage of the pension provided by Variant for employees. The value should be a positive number and will be used to calculate the pension amount.`, - validation: (Rule) => [ - Rule.min(0) + validation: (rule) => [ + rule + .min(0) .max(100) .error("The pension percentage must be a number between 0 and 100."), - Rule.custom((value, context) => { + rule.custom((value, context) => { if ( context.document?.showSalaryCalculator && (value === undefined || value === null) diff --git a/studio/schemas/objects/compensations/salariesByLocation.ts b/studio/schemas/objects/compensations/salariesByLocation.ts index 70665b70d..b13c838f8 100644 --- a/studio/schemas/objects/compensations/salariesByLocation.ts +++ b/studio/schemas/objects/compensations/salariesByLocation.ts @@ -24,7 +24,7 @@ export const salariesByLocation = defineField({ ...location, description: "Select the company location for which you are entering the salary information. Each location must be unique.", - validation: (Rule) => Rule.required(), + validation: (rule) => rule.required(), }, defineField({ name: "yearlySalaries", @@ -42,7 +42,7 @@ export const salariesByLocation = defineField({ description: "The calendar year for which these salaries were in effect", type: "number", - validation: (Rule) => Rule.required().min(2018), + validation: (rule) => rule.required().min(2018), }), defineField({ name: "salaries", @@ -87,8 +87,8 @@ export const salariesByLocation = defineField({ }, }, ], - validation: (Rule) => - Rule.custom((salariesByLocation) => { + validation: (rule) => + rule.custom((salariesByLocation) => { const isNotDuplicate: boolean = checkForDuplicateLocations( salariesByLocation as DocumentWithLocation[] | undefined, ); diff --git a/studio/schemas/objects/footerSection.ts b/studio/schemas/objects/footerSection.ts index fd10a9a76..046affb5b 100644 --- a/studio/schemas/objects/footerSection.ts +++ b/studio/schemas/objects/footerSection.ts @@ -33,9 +33,9 @@ export const footerSection = defineType({ type: "string", description: "Enter the title for this footer section. This will help identify the section within the footer.", - validation: (Rule) => [ - Rule.required().error("Section title is required"), - Rule.max(60), + validation: (rule) => [ + rule.required().error("Section title is required"), + rule.max(60), ], components: { input: (props: StringInputProps) => @@ -55,7 +55,7 @@ export const footerSection = defineType({ ], layout: "dropdown", }, - validation: (Rule) => Rule.required().error("Content type is required"), + validation: (rule) => rule.required().error("Content type is required"), initialValue: SectionType.Content, }, { diff --git a/studio/schemas/objects/link.ts b/studio/schemas/objects/link.ts index 32411039e..8f828ccc5 100644 --- a/studio/schemas/objects/link.ts +++ b/studio/schemas/objects/link.ts @@ -50,8 +50,8 @@ export const link = defineField({ components: { input: LinkTypeSelector, }, - validation: (Rule) => - Rule.custom((value, context) => { + validation: (rule) => + rule.custom((value, context) => { const parent = context.parent as Parent; if (parent?.linkTitle && !value) { return "Link type is required"; @@ -69,8 +69,8 @@ export const link = defineField({ { type: lazyBlogID() }, { type: lazyCompensationsID() }, ], - validation: (Rule: any) => - Rule.custom((value: any, context: any) => { + validation: (rule) => + rule.custom((value: any, context: any) => { const parent = context.parent as Parent; if ( parent?.linkTitle && @@ -93,29 +93,31 @@ export const link = defineField({ type: "url", description: "Enter the full URL for the external link, including 'https://'. For example, 'https://www.example.com'.", - validation: (Rule) => - Rule.uri({ - scheme: ["http", "https"], - allowRelative: false, - }).custom((value, context) => { - const parent = context.parent as Parent; - if ( - parent?.linkTitle && - parent?.linkType === LinkType.External && - !value - ) { - return "URL is required for external links"; - } - return true; - }), + validation: (rule) => + rule + .uri({ + scheme: ["http", "https"], + allowRelative: false, + }) + .custom((value, context) => { + const parent = context.parent as Parent; + if ( + parent?.linkTitle && + parent?.linkType === LinkType.External && + !value + ) { + return "URL is required for external links"; + } + return true; + }), hidden: ({ parent }) => parent?.linkType !== LinkType.External, }, { name: "email", title: "Enter the email address", type: "string", - validation: (Rule) => - Rule.custom((value: string, context) => { + validation: (rule) => + rule.custom((value: string, context) => { const parent = context.parent as Parent; if ( parent?.linkTitle && @@ -135,8 +137,8 @@ export const link = defineField({ name: "phone", title: "Enter the phone number", type: "string", - validation: (Rule) => - Rule.custom((value: string, context) => { + validation: (rule) => + rule.custom((value: string, context) => { const parent = context.parent as Parent; if ( parent?.linkTitle && diff --git a/studio/schemas/objects/sections/callToAction.ts b/studio/schemas/objects/sections/callToAction.ts index e9a701cfc..7ae645494 100644 --- a/studio/schemas/objects/sections/callToAction.ts +++ b/studio/schemas/objects/sections/callToAction.ts @@ -29,7 +29,7 @@ export const callToAction = defineField({ preview: callToActionField.preview, }, ], - validation: (Rule) => Rule.required(), + validation: (rule) => rule.required(), }, ], initialValue: { diff --git a/studio/schemas/objects/sections/form.ts b/studio/schemas/objects/sections/form.ts index 78554391c..1b0c5c898 100644 --- a/studio/schemas/objects/sections/form.ts +++ b/studio/schemas/objects/sections/form.ts @@ -21,8 +21,9 @@ export const contactForm = defineField({ styles: [{ title: "Normal", value: "normal" }], }, ], - validation: (Rule) => - Rule.required() + validation: (rule) => + rule + .required() .min(1) .error("Consent Agreement Text is required and cannot be empty."), }, diff --git a/studio/schemas/objects/sections/grid.ts b/studio/schemas/objects/sections/grid.ts index d50038e57..4f8916414 100644 --- a/studio/schemas/objects/sections/grid.ts +++ b/studio/schemas/objects/sections/grid.ts @@ -26,7 +26,7 @@ export const grid = defineField({ title: "Item Title", description: "Title of the grid item, such as the name of an employee.", - validation: (Rule) => Rule.required(), + validation: (rule) => rule.required(), }, { ...richText, @@ -39,7 +39,7 @@ export const grid = defineField({ title: "Item Image", description: "Image of the grid item, such as a photo of an employee.", - validation: (Rule) => Rule.required(), + validation: (rule) => rule.required(), }, ], preview: { @@ -58,8 +58,8 @@ export const grid = defineField({ }, }, ], - validation: (Rule) => - Rule.required().min(1).error("At least one grid item is required."), + validation: (rule) => + rule.required().min(1).error("At least one grid item is required."), }, ], preview: { diff --git a/studio/schemas/objects/sections/hero.ts b/studio/schemas/objects/sections/hero.ts index 449ff919f..e051243b0 100644 --- a/studio/schemas/objects/sections/hero.ts +++ b/studio/schemas/objects/sections/hero.ts @@ -17,7 +17,7 @@ export const hero = defineField({ name: "description", title: "Description", type: "string", - validation: (Rule) => Rule.max(200), + validation: (rule) => rule.max(200), components: { input: (props: StringInputProps) => StringInputWithCharacterCount({ ...props, maxCount: 200 }), @@ -36,8 +36,8 @@ export const hero = defineField({ preview: callToActionField.preview, }, ], - validation: (Rule) => - Rule.custom((callToActions) => { + validation: (rule) => + rule.custom((callToActions) => { if (!Array.isArray(callToActions)) return true; if (callToActions.length > 2) { return "You can only have two Call to Action links"; diff --git a/studio/schemas/objects/sections/image.ts b/studio/schemas/objects/sections/image.ts index bd9158210..dde7bcb15 100644 --- a/studio/schemas/objects/sections/image.ts +++ b/studio/schemas/objects/sections/image.ts @@ -13,7 +13,7 @@ export const imageSection = defineField({ defineField({ ...image, description: "Upload a featured image for the section.", - validation: (Rule) => Rule.required(), + validation: (rule) => rule.required(), }), ], preview: { diff --git a/studio/schemas/objects/sections/logoSalad.ts b/studio/schemas/objects/sections/logoSalad.ts index 83d790b87..7b6a6a900 100644 --- a/studio/schemas/objects/sections/logoSalad.ts +++ b/studio/schemas/objects/sections/logoSalad.ts @@ -22,9 +22,9 @@ export const logoSalad = defineField({ type: "string", description: "Required text displayed in a smaller body text style. Use it to provide additional context or details about the logos.", - validation: (Rule) => [ - Rule.required().error("Logo description is required."), - Rule.max(100), + validation: (rule) => [ + rule.required().error("Logo description is required."), + rule.max(100), ], components: { input: (props: StringInputProps) => @@ -38,8 +38,9 @@ export const logoSalad = defineField({ "Add a list of logos to display. You must include between 6 and 12 logos.", type: "array", of: [image], - validation: (Rule) => - Rule.min(6) + validation: (rule) => + rule + .min(6) .error("At least 6 logos are required.") .max(12) .error("You can add up to 12 logos.") diff --git a/studio/schemas/objects/sections/testimonials.ts b/studio/schemas/objects/sections/testimonials.ts index bbc24b3dd..66cdf48e5 100644 --- a/studio/schemas/objects/sections/testimonials.ts +++ b/studio/schemas/objects/sections/testimonials.ts @@ -15,8 +15,9 @@ export const testimonals = defineField({ title: "List of Testimonials", type: "array", of: [testimony], - validation: (Rule) => - Rule.required() + validation: (rule) => + rule + .required() .min(1) .max(4) .error("You must have between 1 and 4 testimonials."), diff --git a/studio/schemas/objects/seo.ts b/studio/schemas/objects/seo.ts index cf6adaf5b..44f1f9acc 100644 --- a/studio/schemas/objects/seo.ts +++ b/studio/schemas/objects/seo.ts @@ -23,11 +23,12 @@ const seo = defineField({ title: "SEO & Social Media Title", description: "Create an engaging title that attracts users on social media and in search results. Keep the title between 15-70 characters for the best results.", - validation: (Rule) => [ - Rule.required() + validation: (rule) => [ + rule + .required() .min(15) .error("A title of minimum 15 characters is required"), - Rule.max(70).error("A title cannot exceed 70 characters"), + rule.max(70), ], components: { input: (props) => @@ -40,13 +41,17 @@ const seo = defineField({ title: "SEO & Social Media Description", description: "An optional but recommended short description to boost visitor engagement from social media and search engines. Try to keep it between 70-160 characters.", - validation: (Rule) => [ - Rule.min(70).warning( - "A description of at least 70 characters has a higher chance of converting visitors", - ), - Rule.max(160).warning( - "A description of more than 160 characters has a lower chance of converting visitors", - ), + validation: (rule) => [ + rule + .min(70) + .warning( + "A description of at least 70 characters has a higher chance of converting visitors", + ), + rule + .max(160) + .warning( + "A description of more than 160 characters has a lower chance of converting visitors", + ), ], components: { input: (props) => diff --git a/studio/schemas/objects/socialMedia.ts b/studio/schemas/objects/socialMedia.ts index 5d9943b58..70cbe1a1e 100644 --- a/studio/schemas/objects/socialMedia.ts +++ b/studio/schemas/objects/socialMedia.ts @@ -19,8 +19,8 @@ export const socialMedia = defineType({ name: SocialMediaID.url, type: "url", title: "URL", - validation: (Rule) => - Rule.uri({ + validation: (rule) => + rule.uri({ allowRelative: false, scheme: ["http", "https"], }), diff --git a/studio/schemas/schemaTypes/slug.ts b/studio/schemas/schemaTypes/slug.ts index c18745250..602211222 100644 --- a/studio/schemas/schemaTypes/slug.ts +++ b/studio/schemas/schemaTypes/slug.ts @@ -47,8 +47,8 @@ function createSlugField(source: string) { .slice(0, SLUG_MAX_LENGTH), isUnique: isSlugUniqueAcrossAllDocuments, }, - validation: (Rule) => - Rule.required().custom((value) => { + validation: (rule) => + rule.required().custom((value) => { if (value?.current === undefined) return true; return ( encodeURIComponent(value.current) === value.current || diff --git a/studioShared/schemas/documents/blogPosts.ts b/studioShared/schemas/documents/blogPosts.ts index 10208b062..0a31ead1b 100644 --- a/studioShared/schemas/documents/blogPosts.ts +++ b/studioShared/schemas/documents/blogPosts.ts @@ -18,8 +18,8 @@ const blogPosts = defineType({ description: "Select the date and time when this post will be published.", type: "datetime", initialValue: () => new Date().toISOString(), - validation: (Rule) => - Rule.required().custom((date, context) => { + validation: (rule) => + rule.required().custom((date, context) => { // Ensure date is not undefined or null if (!date) return "The publish date is required.";