From 01aa3ea176064da9accbbecef39896a4df4ce5f3 Mon Sep 17 00:00:00 2001 From: Takayuki Miyoshi Date: Tue, 23 Jul 2024 18:21:50 +0900 Subject: [PATCH 1/4] Add stepnumber rule #30 --- rules/index.js | 1 + rules/stepnumber.js | 31 +++++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+) create mode 100644 rules/stepnumber.js diff --git a/rules/index.js b/rules/index.js index 7bf5a2e..1897207 100644 --- a/rules/index.js +++ b/rules/index.js @@ -7,6 +7,7 @@ export { number } from './number'; export { date } from './date'; export { time } from './time'; export { file } from './file'; +export { stepnumber } from './stepnumber'; export { enumeration as "enum" } from './enum'; export { dayofweek } from './dayofweek'; export { minitems } from './minitems'; diff --git a/rules/stepnumber.js b/rules/stepnumber.js new file mode 100644 index 0000000..a9fcaa1 --- /dev/null +++ b/rules/stepnumber.js @@ -0,0 +1,31 @@ +import { ValidationError } from '../error'; + +export const stepnumber = function ( formDataTree ) { + const values = formDataTree.getAll( this.field ); + + const base = parseFloat( this.base ); + const interval = parseFloat( this.interval ); + + if ( ! ( 0 < interval ) ) { + return true; + } + + const matchesStep = text => { + text = text.trim(); + + const remainder = ( parseFloat( text ) - base ) % interval; + + if ( + '0.000000' === Math.abs( remainder ).toFixed( 6 ) || + '0.000000' === Math.abs( remainder - interval ).toFixed( 6 ) + ) { + return true; + } + + return false; + }; + + if ( ! values.every( matchesStep ) ) { + throw new ValidationError( this ); + } +}; From 4a47475e7c8f86ce3b37b8803088343800b959b6 Mon Sep 17 00:00:00 2001 From: Takayuki Miyoshi Date: Wed, 24 Jul 2024 18:06:03 +0900 Subject: [PATCH 2/4] Add stepnumber doc #30 --- rules/README.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/rules/README.md b/rules/README.md index 8cba029..3f9aacb 100644 --- a/rules/README.md +++ b/rules/README.md @@ -54,6 +54,13 @@ A `file` rule verifies that the field specified by the `field` property is empty If an `accept` property is specified, the file type must match one of file types that the property defines. +## stepnumber + +A `stepnumber` rule verifies that the field specified by the `field` property is empty or has a numerical value that matches one of the allowed values calculated based on the `base` and `interval` properties. Specifically, when the field value is equal to the base value plus an integral multiple of the interval value, the rule is validated. + +Both the `base` and `interval` properties must have an integer or a floating-point number value. The use of an `any` keyword, which HTML supports as the `step` attribute value, is not supported. + + ## enum An `enum` rule verifies that the field specified by the `field` property is empty or that it has a value exactly the same as one of the array items specified by the `accept` property. From 7c65b0db3b6d02f2734eed912c6fa8ebd5a217b4 Mon Sep 17 00:00:00 2001 From: Takayuki Miyoshi Date: Wed, 24 Jul 2024 18:08:37 +0900 Subject: [PATCH 3/4] Move stepnumber after min/max rules #30 --- rules/README.md | 14 +++++++------- rules/index.js | 2 +- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/rules/README.md b/rules/README.md index 3f9aacb..06faaf1 100644 --- a/rules/README.md +++ b/rules/README.md @@ -54,13 +54,6 @@ A `file` rule verifies that the field specified by the `field` property is empty If an `accept` property is specified, the file type must match one of file types that the property defines. -## stepnumber - -A `stepnumber` rule verifies that the field specified by the `field` property is empty or has a numerical value that matches one of the allowed values calculated based on the `base` and `interval` properties. Specifically, when the field value is equal to the base value plus an integral multiple of the interval value, the rule is validated. - -Both the `base` and `interval` properties must have an integer or a floating-point number value. The use of an `any` keyword, which HTML supports as the `step` attribute value, is not supported. - - ## enum An `enum` rule verifies that the field specified by the `field` property is empty or that it has a value exactly the same as one of the array items specified by the `accept` property. @@ -123,6 +116,13 @@ A `minfilesize` rule verifies that the size of the file object value of the fiel A `maxfilesize` rule verifies that the size of the file object value of the field specified by the `field` property is not larger than the size specified by the `threshold` property. +## stepnumber + +A `stepnumber` rule verifies that the field specified by the `field` property is empty or has a numerical value that matches one of the allowed values calculated based on the `base` and `interval` properties. Specifically, when the field value is equal to the base value plus an integral multiple of the interval value, the rule is validated. + +Both the `base` and `interval` properties must have an integer or a floating-point number value. The use of an `any` keyword, which HTML supports as the `step` attribute value, is not supported. + + ## all An `all` rule verifies that all of the child rules in the `rules` property are verified. Child rules are evaluated in order from the top, and if one of the rules fails, the iteration will terminate there. diff --git a/rules/index.js b/rules/index.js index 1897207..c6acf96 100644 --- a/rules/index.js +++ b/rules/index.js @@ -7,7 +7,6 @@ export { number } from './number'; export { date } from './date'; export { time } from './time'; export { file } from './file'; -export { stepnumber } from './stepnumber'; export { enumeration as "enum" } from './enum'; export { dayofweek } from './dayofweek'; export { minitems } from './minitems'; @@ -20,5 +19,6 @@ export { mindate } from './mindate'; export { maxdate } from './maxdate'; export { minfilesize } from './minfilesize'; export { maxfilesize } from './maxfilesize'; +export { stepnumber } from './stepnumber'; export { all } from './all'; export { any } from './any'; From f67a4d96456a74ddea7c265e9426a41c944b0f6a Mon Sep 17 00:00:00 2001 From: Takayuki Miyoshi Date: Wed, 24 Jul 2024 18:27:30 +0900 Subject: [PATCH 4/4] Update JSON Schema #30 --- rules/README.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/rules/README.md b/rules/README.md index 06faaf1..0e9cff3 100644 --- a/rules/README.md +++ b/rules/README.md @@ -181,6 +181,7 @@ This is the meta schema for SWV schemas based on [JSON Schema](https://json-sche "maxdate", "minfilesize", "maxfilesize", + "stepnumber", "all", "any" ] @@ -198,6 +199,13 @@ This is the meta schema for SWV schemas based on [JSON Schema](https://json-sche "type": "string" } }, + "base": { + "type": "string" + }, + "interval": { + "type": "number", + "minimum": 0 + }, "threshold": { "type": "string" }