-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b87c97c
commit f63815d
Showing
5 changed files
with
156 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
type ValidationError = { | ||
type: string; | ||
loc: string[]; | ||
msg: string; | ||
input: any; | ||
url: string; | ||
ctx?: any; | ||
}; | ||
|
||
type ErrorOutput = { [key: string]: string }; | ||
|
||
export function parseValidationErrors(errors: ValidationError[]): ErrorOutput { | ||
const result: ErrorOutput = {}; | ||
errors.forEach((error) => { | ||
const key = error.loc[error.loc.length - 1]; // Using the last item in 'loc' array as the key | ||
result[key] = error.msg; | ||
}); | ||
return result; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,33 @@ | ||
// hooks/useForm.ts | ||
import { useState, useEffect } from 'react'; | ||
import { JsonSchema } from '../interfaces/models'; | ||
|
||
export const useForm = (jsonSchema: JsonSchema) => { | ||
const [formData, setFormData] = useState<{ [key: string]: any }>({}); | ||
const [formErrors, setFormErrors] = useState<{ [key: string]: string }>({}); | ||
|
||
useEffect(() => { | ||
const initialValues: { [key: string]: any } = {}; | ||
Object.keys(jsonSchema.properties).forEach((key) => { | ||
const property = jsonSchema.properties[key]; | ||
// Check for enum with exactly one value and set it, otherwise use default or fallback to an empty string | ||
if (property.enum && property.enum.length === 1) { | ||
initialValues[key] = property.enum[0]; // Auto-set single enum value | ||
initialValues[key] = property.enum[0]; | ||
} else { | ||
initialValues[key] = property.default ?? ''; // Use default or empty string if no default | ||
initialValues[key] = property.default ?? ''; | ||
} | ||
}); | ||
setFormData(initialValues); | ||
setFormErrors({}); // Reset errors on schema change | ||
}, [jsonSchema]); | ||
|
||
const handleChange = (key: string, value: any) => { | ||
setFormData((prev) => ({ | ||
...prev, | ||
[key]: value, | ||
})); | ||
setFormData((prev) => ({ ...prev, [key]: value })); | ||
setFormErrors((prev) => ({ ...prev, [key]: '' })); // Clear error on change | ||
}; | ||
|
||
return { | ||
formData, | ||
handleChange, | ||
formErrors, | ||
setFormErrors, // Expose this to allow setting errors from the component | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { describe, it, expect } from 'vitest'; | ||
import { parseValidationErrors } from '../app/utils/formHelpers'; | ||
|
||
describe('parseValidationErrors', () => { | ||
it('converts a list of Pydantic validation errors to a simplified error object', () => { | ||
const input = [ | ||
{ | ||
type: 'string_type', | ||
loc: ['body', 'api_key'], | ||
msg: 'Input should be a valid string', | ||
input: 4, | ||
url: 'https://errors.pydantic.dev/2.7/v/string_type', | ||
}, | ||
{ | ||
type: 'url_parsing', | ||
loc: ['body', 'base_url'], | ||
msg: 'Input should be a valid URL, relative URL without a base', | ||
input: '1', | ||
ctx: { | ||
error: 'relative URL without a base', | ||
}, | ||
url: 'https://errors.pydantic.dev/2.7/v/url_parsing', | ||
}, | ||
]; | ||
|
||
const expectedOutput = { | ||
api_key: 'Input should be a valid string', | ||
base_url: 'Input should be a valid URL, relative URL without a base', | ||
}; | ||
|
||
const result = parseValidationErrors(input); | ||
expect(result).toEqual(expectedOutput); | ||
}); | ||
|
||
it('converts a list of Pydantic validation errors to a simplified error object', () => { | ||
const input = [ | ||
{ | ||
type: 'url_parsing', | ||
loc: ['body', 'base_url'], | ||
msg: 'Input should be a valid URL, relative URL without a base', | ||
input: '1', | ||
ctx: { | ||
error: 'relative URL without a base', | ||
}, | ||
url: 'https://errors.pydantic.dev/2.7/v/url_parsing', | ||
}, | ||
]; | ||
|
||
const expectedOutput = { | ||
base_url: 'Input should be a valid URL, relative URL without a base', | ||
}; | ||
|
||
const result = parseValidationErrors(input); | ||
expect(result).toEqual(expectedOutput); | ||
}); | ||
}); |