Skip to content

Commit

Permalink
Merge pull request #33 from takayukister/dev/1.4
Browse files Browse the repository at this point in the history
Introduce stepnumber rule type
  • Loading branch information
takayukister authored Jul 24, 2024
2 parents 552a134 + f67a4d9 commit a768865
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
15 changes: 15 additions & 0 deletions rules/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,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.
Expand Down Expand Up @@ -174,6 +181,7 @@ This is the meta schema for SWV schemas based on [JSON Schema](https://json-sche
"maxdate",
"minfilesize",
"maxfilesize",
"stepnumber",
"all",
"any"
]
Expand All @@ -191,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"
}
Expand Down
1 change: 1 addition & 0 deletions rules/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,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';
31 changes: 31 additions & 0 deletions rules/stepnumber.js
Original file line number Diff line number Diff line change
@@ -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 );
}
};

0 comments on commit a768865

Please sign in to comment.