Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dayjs support #1005

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ jQuery-QueryBuilder is available on [jsDelivr](https://www.jsdelivr.com/package/
* [Bootstrap 5](https://getbootstrap.com/docs/5.3/) CSS and bundle.js which includes `Popper` for tooltips and popovers
* [Bootstrap Icons](https://icons.getbootstrap.com/)
* [jQuery.extendext](https://github.com/mistic100/jQuery.extendext)
* [MomentJS](https://momentjs.com) (optional, for Date/Time validation)
* [SQL Parser](https://github.com/mistic100/sql-parser) (optional, for SQL methods)
* Other Bootstrap/jQuery plugins used by plugins

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"bootstrap-icons": "^1.11.3",
"jquery": "^3.5.1",
"jquery-extendext": "^1.0.0",
"moment": "^2.29.1",
"dayjs": "^1.11.13",
"sql-parser-mistic": "^1.2.3"
},
"devDependencies": {
Expand Down
33 changes: 21 additions & 12 deletions src/data.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,24 +170,28 @@ QueryBuilder.prototype._validateValue = function(rule, value) {

// we need MomentJS
if (validation.format) {
if (!('moment' in window)) {
Utils.error('MissingLibrary', 'MomentJS is required for Date/Time validation. Get it here http://momentjs.com');
}

var datetime = moment(tempValue[j], validation.format);
if (dayjs in window) {
window.momenty = window.dayjs;
} else if ('moment' in window) {
window.momenty = window.moment;
} else {
Utils.error('MissingLibrary', 'DayJS or MomentJS is required for Date/Time validation. Get it here https://day.js.org/ or https://momentjs.com');
}

var datetime = momenty(tempValue[j], validation.format);
if (!datetime.isValid()) {
result = [this.getValidationMessage(validation, 'format', 'datetime_invalid'), validation.format];
break;
}
else {
if (validation.min) {
if (datetime < moment(validation.min, validation.format)) {
if (datetime < momenty(validation.min, validation.format)) {
result = [this.getValidationMessage(validation, 'min', 'datetime_exceed_min'), validation.min];
break;
}
}
if (validation.max) {
if (datetime > moment(validation.max, validation.format)) {
if (datetime > momenty(validation.max, validation.format)) {
result = [this.getValidationMessage(validation, 'max', 'datetime_exceed_max'), validation.max];
break;
}
Expand Down Expand Up @@ -231,12 +235,17 @@ QueryBuilder.prototype._validateValue = function(rule, value) {

case 'datetime':
// we need MomentJS

if (validation.format) {
if (!('moment' in window)) {
Utils.error('MissingLibrary', 'MomentJS is required for Date/Time validation. Get it here http://momentjs.com');
}

if (moment(value[0], validation.format).isAfter(moment(value[1], validation.format))) {
if (dayjs in window) {
window.momenty = window.dayjs;
} else if ('moment' in window) {
window.momenty = window.moment;
} else {
Utils.error('MissingLibrary', 'DayJS or MomentJS is required for Date/Time validation. Get it here https://day.js.org/ or https://momentjs.com');
}

if (momenty(value[0], validation.format).isAfter(momenty(value[1], validation.format))) {
result = ['datetime_between_invalid', value[0], value[1]];
}
}
Expand Down
2 changes: 1 addition & 1 deletion tests/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<script src="../node_modules/bootstrap/dist/js/bootstrap.js"></script>
<script src="../node_modules/chosenjs/chosen.jquery.js"></script>
<script src="../node_modules/bootbox/bootbox.js"></script>
<script src="../node_modules/moment/moment.js"></script>
<script src="../node_modules/dayjs/dayjs.js"></script>
<script src="../node_modules/sql-parser-mistic/browser/sql-parser.js"></script>
<script src="../node_modules/jquery-extendext/jquery-extendext.js"></script>
<script src="../node_modules/dot/doT.js"></script>
Expand Down
6 changes: 0 additions & 6 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -766,7 +766,6 @@ foodoc@^0.0.9:
handlebars-layouts "^3.1.4"
jsdoc "^3.5.5"
lunr "^1.0.0"
moment "^2.22.1"
sanitize-html "^1.18.2"

for-in@^1.0.1:
Expand Down Expand Up @@ -1545,11 +1544,6 @@ mkdirp@^1.0.4:
resolved "https://registry.npmjs.org/mkdirp/-/mkdirp-1.0.4.tgz"
integrity sha512-vVqVZQyf3WLx2Shd0qJ9xuvqgAyKPLAiqITEtqW0oIUjzo3PePDd6fW9iFz30ef7Ysp/oiWqbhszeGWW2T6Gzw==

moment@^2.22.1, moment@^2.29.1:
version "2.29.4"
resolved "https://registry.npmjs.org/moment/-/moment-2.29.4.tgz"
integrity sha512-5LC9SOxjSc2HF6vO2CyuTDNivEdoz2IvyJJGj6X8DJ0eFyfszE0QiEd+iXmBvUP3WHxSjFH/vIsA0EN00cgr8w==

morgan@^1.10.0:
version "1.10.0"
resolved "https://registry.npmjs.org/morgan/-/morgan-1.10.0.tgz"
Expand Down