File size: 1,330 Bytes
de7129f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36

import { z } from "zod";

const DataPointSchema = z.object({
  x: z.string().refine(val => val.trim() !== '' && !isNaN(parseFloat(val)), {
    message: "X must be a number.",
  }),
  y: z.string().refine(val => val.trim() !== '' && !isNaN(parseFloat(val)), {
    message: "Y must be a number.",
  }),
});

export const LagrangeFormSchema = z.object({
  dataPoints: z.array(DataPointSchema)
    .min(1, "At least one data point is required.") // Changed from 2 to 1 to allow single point (constant polynomial)
    .refine(points => {
      if (points.length <= 1) return true; // No need to check for uniqueness if 0 or 1 point
      const xValues = points.map(p => {
        const numX = parseFloat(p.x);
        return isNaN(numX) ? NaN : numX; // Ensure parsing before Set
      });
      if (xValues.some(isNaN)) return false; 
      return new Set(xValues).size === xValues.length;
    }, {
      message: "All X values in data points must be unique.",
      path: ["root"], // Apply error to the root of dataPoints array for better display
    }),
  interpolationX: z.string().refine(val => val.trim() !== '' && !isNaN(parseFloat(val)), {
    message: "Interpolation X must be a number.",
  }),
});

export type LagrangeFormValues = z.infer<typeof LagrangeFormSchema>;
export type DataPoint = { x: number; y: number };