Skip to content

Commit

Permalink
refactor(schema): rename Rule → rule in validation function
Browse files Browse the repository at this point in the history
  • Loading branch information
mathiazom committed Sep 11, 2024
1 parent dd964f4 commit 78055c1
Show file tree
Hide file tree
Showing 24 changed files with 105 additions and 93 deletions.
2 changes: 1 addition & 1 deletion studio/schemas/builders/pageBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 }),
Expand Down
2 changes: 1 addition & 1 deletion studio/schemas/documents/benefit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
6 changes: 3 additions & 3 deletions studio/schemas/documents/blog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 }),
Expand All @@ -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 }),
Expand All @@ -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 }),
Expand Down
4 changes: 2 additions & 2 deletions studio/schemas/documents/navigationManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
12 changes: 6 additions & 6 deletions studio/schemas/documents/post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.";

Expand Down Expand Up @@ -57,7 +57,7 @@ const posts = defineType({
components: {
input: CategorySelector,
},
validation: (Rule) => Rule.required(),
validation: (rule) => rule.required(),
}),
defineField({
name: "lead",
Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion studio/schemas/fields/categories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 }),
Expand Down
6 changes: 3 additions & 3 deletions studio/schemas/fields/text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down Expand Up @@ -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 }),
},
Expand Down
6 changes: 3 additions & 3 deletions studio/schemas/objects/compensations/benefitsByLocation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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,
);
Expand Down
11 changes: 6 additions & 5 deletions studio/schemas/objects/compensations/bonusesByLocation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,17 @@ 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",
title: "Average Bonus",
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."),
}),
Expand All @@ -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,
);
Expand Down
7 changes: 4 additions & 3 deletions studio/schemas/objects/compensations/pension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 4 additions & 4 deletions studio/schemas/objects/compensations/salariesByLocation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down Expand Up @@ -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,
);
Expand Down
8 changes: 4 additions & 4 deletions studio/schemas/objects/footerSection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) =>
Expand All @@ -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,
},
{
Expand Down
48 changes: 25 additions & 23 deletions studio/schemas/objects/link.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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 &&
Expand All @@ -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 &&
Expand All @@ -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 &&
Expand Down
2 changes: 1 addition & 1 deletion studio/schemas/objects/sections/callToAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export const callToAction = defineField({
preview: callToActionField.preview,
},
],
validation: (Rule) => Rule.required(),
validation: (rule) => rule.required(),
},
],
initialValue: {
Expand Down
5 changes: 3 additions & 2 deletions studio/schemas/objects/sections/form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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."),
},
Expand Down
8 changes: 4 additions & 4 deletions studio/schemas/objects/sections/grid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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: {
Expand All @@ -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: {
Expand Down
6 changes: 3 additions & 3 deletions studio/schemas/objects/sections/hero.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 }),
Expand All @@ -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";
Expand Down
Loading

0 comments on commit 78055c1

Please sign in to comment.