-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
274 additions
and
30 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
/** | ||
* return the value of the object for the given index | ||
* @param {any} object | ||
* @param {Number} index | ||
* @returns {boolean} | ||
*/ | ||
|
||
// obj,'1.2.3' -> multiIndex(obj,['1','2','3']) | ||
function pathIndex(obj,is) { | ||
return multiIndex(obj,is.split('.')); | ||
} | ||
|
||
// obj,['1','2','3'] -> ((obj['1'])['2'])['3'] | ||
function multiIndex(obj,is){ | ||
return is.length ? multiIndex(obj[is[0]],is.slice(1)) : obj | ||
} | ||
module.exports = {pathIndex,multiIndex} |
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,42 @@ | ||
const empty = require('../lib/empty'); | ||
const {pathIndex} = require('../lib/ObjectIndex'); | ||
|
||
module.exports = async function acceptedIf(field, value, args) { | ||
|
||
if (!args || args.length < 2) { | ||
|
||
throw new Error('Invalid arguments supplied for field ' + field + ' in requiredIf rule.'); | ||
return false; | ||
} | ||
|
||
if (args.length % 2 !== 0) { | ||
|
||
throw new Error('Invalid arguments supplied for field ' + field + ' in requiredIf rule.'); | ||
return false; | ||
} | ||
let acceptedValues = [true, 'true', 1, '1', 'yes', 'on']; | ||
|
||
let canbetrue = false; | ||
for (let start = 0; start < args.length; start += 2) { | ||
let requiredField = args[start]; | ||
let requiredValue = args[start + 1]; | ||
|
||
if (requiredField == field) { | ||
return false; | ||
} | ||
|
||
// field can be true if all values are presented | ||
if (!empty(pathIndex(this.inputs,requiredField)) | ||
&& pathIndex(this.inputs,requiredField).toString() == requiredValue) { | ||
canbetrue = true; | ||
} else { | ||
canbetrue = false; | ||
break | ||
} | ||
} | ||
if (canbetrue && !(acceptedValues.indexOf(value)>= 0)) { | ||
return false; | ||
} | ||
|
||
return true; | ||
} |
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,42 @@ | ||
const empty = require('../lib/empty'); | ||
const {pathIndex} = require('../lib/ObjectIndex'); | ||
|
||
module.exports = async function acceptedIf(field, value, args) { | ||
|
||
if (!args || args.length < 2) { | ||
|
||
throw new Error('Invalid arguments supplied for field ' + field + ' in requiredIf rule.'); | ||
return false; | ||
} | ||
|
||
if (args.length % 2 !== 0) { | ||
|
||
throw new Error('Invalid arguments supplied for field ' + field + ' in requiredIf rule.'); | ||
return false; | ||
} | ||
let acceptedValues = [true, 'true', 1, '1', 'yes', 'on']; | ||
|
||
let canbetrue = false; | ||
for (let start = 0; start < args.length; start += 2) { | ||
let requiredField = args[start]; | ||
let requiredValue = args[start + 1]; | ||
|
||
if (requiredField == field) { | ||
return false; | ||
} | ||
|
||
// field can be true if all values are presented | ||
if (!empty(pathIndex(this.inputs,requiredField)) | ||
&& pathIndex(this.inputs,requiredField).toString() == requiredValue) { | ||
canbetrue = true; | ||
} else { | ||
canbetrue = false; | ||
break | ||
} | ||
} | ||
if (canbetrue && acceptedValues.indexOf(value)>= 0) { | ||
return false; | ||
} | ||
|
||
return true; | ||
} |
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,18 @@ | ||
const moment = require('moment'), | ||
{ dateFormats } = require('../lib/date'); | ||
|
||
module.exports = async function dateDaysAfterToday(field, value, days) { | ||
|
||
|
||
let mAfterDate, mDate; | ||
|
||
mAfterDate = moment().add(days, 'years'); | ||
mDate = moment(value, dateFormats); | ||
|
||
/* istanbul ignore next */ | ||
if (!mAfterDate.isValid() || !mDate.isValid() || mAfterDate.valueOf() > mDate.valueOf()) { | ||
return false; | ||
} | ||
|
||
return true; | ||
} |
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,18 @@ | ||
const moment = require('moment'), | ||
{ dateFormats } = require('../lib/date'); | ||
|
||
module.exports = async function dateDaysBeforeToday(field, value, days) { | ||
|
||
|
||
let mAfterDate, mDate; | ||
|
||
mAfterDate = moment().add(days, 'years'); | ||
mDate = moment(value, dateFormats); | ||
|
||
/* istanbul ignore next */ | ||
if (!mAfterDate.isValid() || !mDate.isValid() || mAfterDate.valueOf() < mDate.valueOf()) { | ||
return false; | ||
} | ||
|
||
return true; | ||
} |
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,18 @@ | ||
const moment = require('moment'), | ||
{ dateFormats } = require('../lib/date'); | ||
|
||
module.exports = async function dateYearsAfterToday(field, value, days) { | ||
|
||
|
||
let mAfterDate, mDate; | ||
|
||
mAfterDate = moment().add(days, 'years'); | ||
mDate = moment(value, dateFormats); | ||
|
||
/* istanbul ignore next */ | ||
if (!mAfterDate.isValid() || !mDate.isValid() || mAfterDate.valueOf() > mDate.valueOf()) { | ||
return false; | ||
} | ||
|
||
return true; | ||
} |
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,18 @@ | ||
const moment = require('moment'), | ||
{ dateFormats } = require('../lib/date'); | ||
|
||
module.exports = async function dateYearsBeforeToday(field, value, days) { | ||
|
||
|
||
let mAfterDate, mDate; | ||
|
||
mAfterDate = moment().add(days, 'years'); | ||
mDate = moment(value, dateFormats); | ||
|
||
/* istanbul ignore next */ | ||
if (!mAfterDate.isValid() || !mDate.isValid() || mAfterDate.valueOf() < mDate.valueOf()) { | ||
return false; | ||
} | ||
|
||
return true; | ||
} |
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
Oops, something went wrong.