Skip to content

Commit

Permalink
Attribute validation defaults to minimum and maximum defined by type …
Browse files Browse the repository at this point in the history
…if not defined by attribute.
  • Loading branch information
dhchandw committed Nov 11, 2024
1 parent bf5c7cc commit 2eb6a3d
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 55 deletions.
30 changes: 2 additions & 28 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -18925,7 +18925,6 @@ things were successful or not.
* [~validateSpecificAttribute(endpointAttribute, attribute, db, zapSessionId)](#module_Validation API_ Validation APIs..validateSpecificAttribute) ⇒
* [~validateSpecificEndpoint(endpoint)](#module_Validation API_ Validation APIs..validateSpecificEndpoint) ⇒
* [~isValidNumberString(value)](#module_Validation API_ Validation APIs..isValidNumberString) ⇒
* [~isValidSignedNumberString(value)](#module_Validation API_ Validation APIs..isValidSignedNumberString) ⇒
* [~isValidHexString(value)](#module_Validation API_ Validation APIs..isValidHexString) ⇒
* [~isValidDecimalString(value)](#module_Validation API_ Validation APIs..isValidDecimalString) ⇒
* [~isValidFloat(value)](#module_Validation API_ Validation APIs..isValidFloat) ⇒
Expand Down Expand Up @@ -18976,7 +18975,7 @@ Enforce zigbee specific common cluster initialization.

### Validation API: Validation APIs~validateAttribute(db, endpointTypeId, attributeRef, clusterRef, zapSessionId) ⇒
Main attribute validation function.
Returns a promise of an object which stores a list of validational issues.
Returns a promise of an object which stores a list of validation issues.
Such issues as "Invalid type" or "Out of Range".

**Kind**: inner method of [<code>Validation API: Validation APIs</code>](#module_Validation API_ Validation APIs)
Expand Down Expand Up @@ -19057,18 +19056,6 @@ This applies to both actual numbers as well as octet strings.
| --- | --- |
| value | <code>\*</code> |

<a name="module_Validation API_ Validation APIs..isValidSignedNumberString"></a>

### Validation API: Validation APIs~isValidSignedNumberString(value) ⇒
Check if value is a valid signed number in string form.

**Kind**: inner method of [<code>Validation API: Validation APIs</code>](#module_Validation API_ Validation APIs)
**Returns**: boolean

| Param | Type |
| --- | --- |
| value | <code>\*</code> |

<a name="module_Validation API_ Validation APIs..isValidHexString"></a>

### Validation API: Validation APIs~isValidHexString(value) ⇒
Expand Down Expand Up @@ -19300,7 +19287,6 @@ things were successful or not.
* [~validateSpecificAttribute(endpointAttribute, attribute, db, zapSessionId)](#module_Validation API_ Validation APIs..validateSpecificAttribute) ⇒
* [~validateSpecificEndpoint(endpoint)](#module_Validation API_ Validation APIs..validateSpecificEndpoint) ⇒
* [~isValidNumberString(value)](#module_Validation API_ Validation APIs..isValidNumberString) ⇒
* [~isValidSignedNumberString(value)](#module_Validation API_ Validation APIs..isValidSignedNumberString) ⇒
* [~isValidHexString(value)](#module_Validation API_ Validation APIs..isValidHexString) ⇒
* [~isValidDecimalString(value)](#module_Validation API_ Validation APIs..isValidDecimalString) ⇒
* [~isValidFloat(value)](#module_Validation API_ Validation APIs..isValidFloat) ⇒
Expand Down Expand Up @@ -19351,7 +19337,7 @@ Enforce zigbee specific common cluster initialization.

### Validation API: Validation APIs~validateAttribute(db, endpointTypeId, attributeRef, clusterRef, zapSessionId) ⇒
Main attribute validation function.
Returns a promise of an object which stores a list of validational issues.
Returns a promise of an object which stores a list of validation issues.
Such issues as "Invalid type" or "Out of Range".

**Kind**: inner method of [<code>Validation API: Validation APIs</code>](#module_Validation API_ Validation APIs)
Expand Down Expand Up @@ -19432,18 +19418,6 @@ This applies to both actual numbers as well as octet strings.
| --- | --- |
| value | <code>\*</code> |

<a name="module_Validation API_ Validation APIs..isValidSignedNumberString"></a>

### Validation API: Validation APIs~isValidSignedNumberString(value) ⇒
Check if value is a valid signed number in string form.

**Kind**: inner method of [<code>Validation API: Validation APIs</code>](#module_Validation API_ Validation APIs)
**Returns**: boolean

| Param | Type |
| --- | --- |
| value | <code>\*</code> |

<a name="module_Validation API_ Validation APIs..isValidHexString"></a>

### Validation API: Validation APIs~isValidHexString(value) ⇒
Expand Down
34 changes: 7 additions & 27 deletions src-electron/validation/validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const queryPackage = require('../db/query-package.js')

/**
* Main attribute validation function.
* Returns a promise of an object which stores a list of validational issues.
* Returns a promise of an object which stores a list of validation issues.
* Such issues as "Invalid type" or "Out of Range".
* @param {*} db db reference
* @param {*} endpointTypeId endpoint reference
Expand Down Expand Up @@ -128,20 +128,6 @@ async function validateSpecificAttribute(
//Interpreting float values
if (!checkAttributeBoundsFloat(attribute, endpointAttribute))
defaultAttributeIssues.push('Out of range')
} else if (await types.isSignedInteger(db, zapSessionId, attribute.type)) {
// we shouldn't check boundaries for an invalid number string
if (!isValidSignedNumberString(endpointAttribute.defaultValue)) {
defaultAttributeIssues.push('Invalid Integer')
} else if (
!(await checkAttributeBoundsInteger(
attribute,
endpointAttribute,
db,
zapSessionId
))
) {
defaultAttributeIssues.push('Out of range')
}
} else {
// we shouldn't check boundaries for an invalid number string
if (!isValidNumberString(endpointAttribute.defaultValue)) {
Expand Down Expand Up @@ -210,16 +196,6 @@ function isValidNumberString(value) {
return /^(0x)?[\dA-F]+$/i.test(value) || Number.isInteger(Number(value))
}

/**
* Check if value is a valid signed number in string form.
*
* @param {*} value
* @returns boolean
*/
function isValidSignedNumberString(value) {
return /^(0x)?[\dA-F]+$/i.test(value) || Number.isInteger(Number(value))
}

/**
* Check if value is a valid hex string.
*
Expand Down Expand Up @@ -312,13 +288,17 @@ function isBigInteger(bits) {
* @returns object
*/
async function getBoundsInteger(attribute, typeSize, isSigned) {
let typeMin = isSigned ? -Math.pow(2, typeSize - 1) : 0
let typeMax = isSigned
? Math.pow(2, typeSize - 1) - 1
: Math.pow(2, typeSize) - 1
return {
min: attribute.min
? await getIntegerFromAttribute(attribute.min, typeSize, isSigned)
: null,
: typeMin,
max: attribute.max
? await getIntegerFromAttribute(attribute.max, typeSize, isSigned)
: null
: typeMax
}
}

Expand Down

0 comments on commit 2eb6a3d

Please sign in to comment.