-
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.
Merge branch 'master' into eanders-ms/pxt-bedrock
- Loading branch information
Showing
14 changed files
with
184 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
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
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 }; | ||
} |