Skip to content

Commit

Permalink
Do not validate value ranges for min/max/offset/scale
Browse files Browse the repository at this point in the history
  • Loading branch information
javagl committed Oct 3, 2023
1 parent 2f4d63c commit 7daf2ba
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 43 deletions.
2 changes: 2 additions & 0 deletions src/validation/metadata/ClassPropertyValidator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,7 @@ export class ClassPropertyValidator {
property,
"default",
theDefault,
true,
schema,
context
)
Expand All @@ -552,6 +553,7 @@ export class ClassPropertyValidator {
property,
"noData",
noData,
true,
schema,
context
)
Expand Down
2 changes: 2 additions & 0 deletions src/validation/metadata/ClassPropertyValueValidator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ export class ClassPropertyValueValidator {
path,
maxOrMin,
value,
false,
context
)
) {
Expand Down Expand Up @@ -161,6 +162,7 @@ export class ClassPropertyValueValidator {
path,
offsetOrScale,
value,
false,
context
)
) {
Expand Down
1 change: 1 addition & 0 deletions src/validation/metadata/MetadataEntityValidator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ export class MetadataEntityValidator {
classProperty,
propertyName,
propertyValue,
true,
schema,
context
)
Expand Down
115 changes: 72 additions & 43 deletions src/validation/metadata/MetadataValueValidator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ export class MetadataValueValidator {
* validation issue message. For example, this may be "noData"
* or "default".
* @param value - The value
* @param validateValueRange - Whether the function should also
* validate whether the value is in the range that is defined by
* the class property component type (if it is a numeric value).
* For example, whether the components are in [0,255] when the
* component type is `UINT8`.
* @param schem - The metadata schema
* @param context - The `ValidationContext`
* @returns Whether the object was valid
*/
Expand All @@ -51,6 +57,7 @@ export class MetadataValueValidator {
property: ClassProperty,
valueName: string,
value: any,
validateValueRange: boolean,
schema: Schema,
context: ValidationContext
): boolean {
Expand Down Expand Up @@ -94,6 +101,7 @@ export class MetadataValueValidator {
path,
valueName,
value,
validateValueRange,
context
);
}
Expand Down Expand Up @@ -343,13 +351,24 @@ export class MetadataValueValidator {
* Validates that a value that appears in a property is a proper
* numeric value that matches the type of the property.
*
* This is intended for the values that can be given as `offset`,
* `scale`, `max`, and `min`, and performs the checks as defined in
* `definitions.schema.json#/definitions/numericValue`.
*
* When `validateValueRange` is `false`, then this will only
* check the structure of the values (e.g. whether a `VEC2`
* property is represented with 2 numeric values). It will
* not check whether these values are in the range that is
* defined by the component type. This is intended for the
* values that can be given as `offset`, `scale`, `max`,
* and `min`. For example, for a `normalized` `SCALAR` `UINT8`
* property, the value may be `1000.0` (which is out of
* the range of `UINT8`), because such a value can be used
* in the `offset`, `scale`, `max`, and `min` properties)
*
* @param property - The ClassProperty
* @param valueName - The name of the value (e.g. 'min' or 'offset')
* @param value - The actual value
* @param validateValueRange - Whether the function should also
* validate whether the value is in the range that is defined by
* the class property component type - for example, whether the
* given value is in [0,255] when the component type is `UINT8`.
* @param context - The `ValidationContext`
* @returns Whether the value was valid
*/
Expand All @@ -358,6 +377,7 @@ export class MetadataValueValidator {
valuePath: string,
valueName: string,
value: any,
validateValueRange: boolean,
context: ValidationContext
): boolean {
// This assumes that the validity of the given property has
Expand All @@ -378,18 +398,21 @@ export class MetadataValueValidator {
) {
return false;
}
// The value MUST be in the range that is
// determined by the componentType
if (
!NumberValidator.validateRange(
valuePath,
"value",
value,
componentType,
context
)
) {
return false;

if (validateValueRange) {
// The value MUST be in the range that is
// determined by the componentType
if (
!NumberValidator.validateRange(
valuePath,
"value",
value,
componentType,
context
)
) {
return false;
}
}
return true;
}
Expand All @@ -411,14 +434,16 @@ export class MetadataValueValidator {
) {
return false;
}
// Each element MUST be in the range that is
// determined by the componentType
return NumberValidator.validateRanges(
valuePath,
value,
componentType,
context
);
if (validateValueRange) {
// Each element MUST be in the range that is
// determined by the componentType
return NumberValidator.validateRanges(
valuePath,
value,
componentType,
context
);
}
}

// Here, the value must be an array.
Expand All @@ -444,14 +469,16 @@ export class MetadataValueValidator {
return false;
}

// Each element MUST be in the range that is
// determined by the componentType
return NumberValidator.validateRanges(
valuePath,
value,
componentType,
context
);
if (validateValueRange) {
// Each element MUST be in the range that is
// determined by the componentType
return NumberValidator.validateRanges(
valuePath,
value,
componentType,
context
);
}
}

// For non-SCALAR arrays, the value MUST be an array of objects
Expand Down Expand Up @@ -489,17 +516,19 @@ export class MetadataValueValidator {
) {
allElementsValid = false;
} else {
// Each element of the array MUST be in the range that is
// determined by the componentType
if (
!NumberValidator.validateRanges(
valueElementPath,
valueElement,
componentType,
context
)
) {
allElementsValid = false;
if (validateValueRange) {
// Each element of the array MUST be in the range that is
// determined by the componentType
if (
!NumberValidator.validateRanges(
valueElementPath,
valueElement,
componentType,
context
)
) {
allElementsValid = false;
}
}
}
}
Expand Down

0 comments on commit 7daf2ba

Please sign in to comment.