Skip to content

Commit

Permalink
fix: Remove old validation schema in names in favour of a standard na…
Browse files Browse the repository at this point in the history
…ming pattern
  • Loading branch information
DafyddLlyr committed Jan 22, 2025
1 parent b77f302 commit 93edced
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 17 deletions.
10 changes: 5 additions & 5 deletions editor.planx.uk/src/@planx/components/DateInput/model.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {
DateInput,
dateRangeSchema,
dateSchema,
dateValidationSchema,
paddedDate,
parseDate,
} from "./model";
Expand Down Expand Up @@ -104,22 +104,22 @@ describe("dateSchema", () => {
});
});

describe("dateRangeSchema", () => {
describe("dateValidationSchema", () => {
test("basic validation", async () => {
expect(
await dateRangeSchema({
await dateValidationSchema({
min: "1990-01-01",
max: "1999-12-31",
} as DateInput).isValid("1995-06-15"),
).toBe(true);
expect(
await dateRangeSchema({
await dateValidationSchema({
min: "1990-01-01",
max: "1999-12-31",
} as DateInput).isValid("2021-06-15"),
).toBe(false);
expect(
await dateRangeSchema({
await dateValidationSchema({
min: "1990-01-01",
max: "1999-12-31",
} as DateInput).isValid("1980-06-15"),
Expand Down
8 changes: 7 additions & 1 deletion editor.planx.uk/src/@planx/components/DateInput/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ export const parseDate = (date?: string) => {
return { year, month, day };
};

/**
* Validates that a given string is a date in the correct format
*/
export const dateSchema = () => {
return string()
.test("missing day", "Date must include a day", (date?: string) => {
Expand Down Expand Up @@ -97,7 +100,10 @@ export const dateSchema = () => {
);
};

export const dateRangeSchema: (input: DateInput) => SchemaOf<string> = (
/**
* Validates that date is both valid and fits within the provided min/max
*/
export const dateValidationSchema: (input: DateInput) => SchemaOf<string> = (
params,
) =>
dateSchema()
Expand Down
4 changes: 2 additions & 2 deletions editor.planx.uk/src/@planx/components/TextInput/Public.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { object } from "yup";
import { DESCRIPTION_TEXT, ERROR_MESSAGE } from "../shared/constants";
import { getPreviouslySubmittedData, makeData } from "../shared/utils";
import type { TextInput } from "./model";
import { TextInputType, userDataSchema } from "./model";
import { TextInputType, textInputValidationSchema } from "./model";

export type Props = PublicProps<TextInput>;

Expand All @@ -28,7 +28,7 @@ const TextInputComponent: React.FC<Props> = (props) => {
validateOnBlur: false,
validateOnChange: false,
validationSchema: object({
text: userDataSchema(props),
text: textInputValidationSchema(props),
}),
});

Expand Down
4 changes: 3 additions & 1 deletion editor.planx.uk/src/@planx/components/TextInput/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ export const emailRegex =
// eslint-disable-next-line
/^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;

export const userDataSchema = ({ type }: TextInput): SchemaOf<UserData> =>
export const textInputValidationSchema = ({
type,
}: TextInput): SchemaOf<UserData> =>
string()
.required("Enter your answer before continuing")
.test({
Expand Down
10 changes: 2 additions & 8 deletions editor.planx.uk/src/@planx/components/shared/Schema/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,12 @@ import { exhaustiveCheck } from "utils";
import { array, BaseSchema, object, ObjectSchema, string } from "yup";

import { checklistValidationSchema } from "../../Checklist/model";
import {
DateInput,
dateRangeSchema as dateValidationSchema,
} from "../../DateInput/model";
import { DateInput, dateValidationSchema } from "../../DateInput/model";
import {
NumberInput,
numberInputValidationSchema,
} from "../../NumberInput/model";
import {
TextInput,
userDataSchema as textInputValidationSchema,
} from "../../TextInput/model";
import { TextInput, textInputValidationSchema } from "../../TextInput/model";
import { Option } from "..";

/**
Expand Down

0 comments on commit 93edced

Please sign in to comment.