-
Notifications
You must be signed in to change notification settings - Fork 580
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
More Parameter Validation and Limitations (Length Limit for AI Prompt…
…s) (#10170) This change started with just adding a limit to the AI prompt length, but as I was adding yet another field to the CriteriaParameter type definition (maxCharacters) which only really applies to a subset of parameter types, I decided it was time to tidy that up. Hence, a larger change. Now, each parameter type has a corresponding type which can have its own fields, and a validate function switches on that type to perform different validations as applicable. Currently, I've added maxCharacters for strings (used for the AI prompt), and min and max for numbers (tested manually but not used; I mostly wanted a proof of concept around 2 different classes with 2 different types of validation).
- Loading branch information
Showing
10 changed files
with
178 additions
and
20 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
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
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
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
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,47 @@ | ||
export type CriteriaParameterType = "string" | "longString" | "number" | "block" | "system"; | ||
|
||
// Represents a parameter definition in a catalog criteria. | ||
export type CriteriaParameterBase = { | ||
name: string; | ||
type: CriteriaParameterType; | ||
default: string | undefined; | ||
paths: string[]; // The json path(s) to update with the parameter value in the catalog criteria. | ||
} | ||
|
||
export type StringParameterBase = CriteriaParameterBase & { | ||
maxCharacters?: number; | ||
} | ||
|
||
export type StringParameter = StringParameterBase & { | ||
type: "string"; | ||
} | ||
|
||
export type LongStringParameter = StringParameterBase & { | ||
type: "longString"; | ||
} | ||
|
||
export type NumberParameter = CriteriaParameterBase & { | ||
type: "number"; | ||
min?: number; | ||
max?: number; | ||
} | ||
|
||
export type BlockParameter = CriteriaParameterBase & { | ||
type: "block"; | ||
} | ||
|
||
/** | ||
* System parameters are fields that can change for a criteria but which are not set directly by the user. | ||
* For example, the project id could be a parameter, but we fill it automatically at eval-time based on the loaded project. | ||
*/ | ||
export type SystemParameter = CriteriaParameterBase & { | ||
type: "system"; | ||
key?: string; | ||
} | ||
|
||
export type CriteriaParameter = StringParameter | LongStringParameter | NumberParameter | BlockParameter | SystemParameter; | ||
|
||
export interface CriteriaParameterValidationResult { | ||
valid: boolean; | ||
message?: string; | ||
} |
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,67 @@ | ||
import { Strings } from "../constants"; | ||
import { | ||
CriteriaParameter, | ||
CriteriaParameterValidationResult, | ||
LongStringParameter, | ||
NumberParameter, | ||
StringParameter, | ||
StringParameterBase, | ||
} from "../types/criteriaParameters"; | ||
|
||
export function validateParameterValue(param: CriteriaParameter, value: string): CriteriaParameterValidationResult { | ||
switch (param.type) { | ||
case "string": | ||
return validateStringParameter(param, value); | ||
case "longString": | ||
return validateLongStringParameter(param, value); | ||
case "number": | ||
return validateNumberParameter(param, value); | ||
case "block": | ||
// Fall through to default case. | ||
case "system": | ||
// Fall through to default case. | ||
default: | ||
return { valid: true } as CriteriaParameterValidationResult; | ||
} | ||
} | ||
|
||
function validateStringParameterBase(param: StringParameterBase, value: string): CriteriaParameterValidationResult { | ||
if (!value) return { valid: true }; // Unset is okay for initial value | ||
|
||
if (param.maxCharacters && value.length > param.maxCharacters) { | ||
return { valid: false, message: Strings.ExceedsMaxLength }; | ||
} | ||
return { valid: true }; | ||
} | ||
|
||
function validateStringParameter(param: StringParameter, value: string): CriteriaParameterValidationResult { | ||
return validateStringParameterBase(param, value); | ||
} | ||
|
||
function validateLongStringParameter(param: LongStringParameter, value: string): CriteriaParameterValidationResult { | ||
return validateStringParameterBase(param, value); | ||
} | ||
|
||
function validateNumberParameter(param: NumberParameter, value: string): CriteriaParameterValidationResult { | ||
// Ensure the value is numeric and within the specified range. | ||
const num = Number(value); | ||
if (isNaN(num)) { | ||
return { | ||
valid: false, | ||
message: Strings.MustBeANumber, | ||
}; | ||
} | ||
if (param.min !== undefined && num < param.min) { | ||
return { | ||
valid: false, | ||
message: Strings.BelowMin, | ||
}; | ||
} | ||
if (param.max !== undefined && num > param.max) { | ||
return { | ||
valid: false, | ||
message: Strings.ExceedsMax, | ||
}; | ||
} | ||
return { valid: true }; | ||
} |