Skip to content

Commit 604612b

Browse files
author
Adam Bloomston
committed
proposed fix for jdorn#388 using math.js
* bower and grunt-bower-concat are now required for building json-editor * math.js is now required for running json-editor, but is not checked by jshint * grunt no longer runs jshint on the output dist file, as it may contain external libaries * float comparisons for multipleOf, divisibleBy, maximum, minimum use math.js
1 parent cfd3419 commit 604612b

File tree

6 files changed

+36
-20
lines changed

6 files changed

+36
-20
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
/node_modules/
22
/.idea/
3+
/bower_components/
4+
/src/bower.js

Gruntfile.js

+14-10
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@
22

33
module.exports = function(grunt) {
44
grunt.initConfig({
5+
bower_concat: {
6+
all: {
7+
dest: 'src/bower.js',
8+
mainFiles: {
9+
'mathjs': ['dist/math.js']
10+
}
11+
}
12+
},
513
concat_sourcemap: {
614
options: {
715
sourcesContent: true
@@ -12,6 +20,9 @@ module.exports = function(grunt) {
1220

1321
// License & version info, start the containing closure
1422
'src/intro.js',
23+
24+
// bower dependencies
25+
'src/bower.js',
1526

1627
// Simple inheritance
1728
'src/class.js',
@@ -119,25 +130,18 @@ module.exports = function(grunt) {
119130

120131
// Wrapper for $.fn style initialization
121132
'src/jquery.js'
122-
],
123-
afterconcat: {
124-
options: {
125-
undef: true
126-
},
127-
files: {
128-
src: ['dist/jsoneditor.js']
129-
}
130-
}
133+
]
131134
}
132135
});
133136

134137
// These plugins provide necessary tasks.
135138
grunt.loadNpmTasks('grunt-contrib-uglify');
139+
grunt.loadNpmTasks('grunt-bower-concat');
136140
grunt.loadNpmTasks('grunt-contrib-watch');
137141
grunt.loadNpmTasks('grunt-contrib-jshint');
138142
grunt.loadNpmTasks('grunt-concat-sourcemap');
139143

140144
// Default task.
141-
grunt.registerTask('default', ['jshint:beforeconcat','concat_sourcemap','jshint:afterconcat','uglify']);
145+
grunt.registerTask('default', ['jshint:beforeconcat','bower_concat','concat_sourcemap','uglify']);
142146

143147
};

README.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@ Download the [production version][min] (22K when gzipped) or the [development ve
1616
Requirements
1717
-----------------
1818

19-
JSON Editor has no required dependencies. It only needs a modern browser (tested in Chrome and Firefox).
19+
JSON Schema has the following dependencies:
20+
21+
* [math.js](http://mathjs.org/) for floating point math
22+
23+
It needs a modern browser (tested in Chrome and Firefox).
2024

2125
### Optional Requirements
2226

bower.json

+4-1
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,8 @@
2323
"package.json",
2424
"Gruntfile.js",
2525
"src"
26-
]
26+
],
27+
"dependencies": {
28+
"mathjs": "~1.7.0"
29+
}
2730
}

package.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,16 @@
2727
"node": ">= 0.8.0"
2828
},
2929
"devDependencies": {
30+
"bower": "~1.4.1",
3031
"grunt": "~0.4.2",
32+
"grunt-bower-concat": "~0.5.0",
3133
"grunt-concat-sourcemap": "^0.4.3",
3234
"grunt-contrib-jshint": "^0.10.0",
3335
"grunt-contrib-uglify": "~0.2.0",
3436
"grunt-contrib-watch": "~0.5.3"
3537
},
3638
"scripts": {
37-
"build": "npm install && grunt",
39+
"build": "npm install && node_modules/.bin/bower install --config.interactive=false && grunt",
3840
"start": "grunt watch",
3941
"test": "grunt"
4042
}

src/validator.js

+8-7
Original file line numberDiff line numberDiff line change
@@ -208,26 +208,27 @@ JSONEditor.Validator = Class.extend({
208208
if(typeof value === "number") {
209209
// `multipleOf` and `divisibleBy`
210210
if(schema.multipleOf || schema.divisibleBy) {
211-
valid = value / (schema.multipleOf || schema.divisibleBy);
212-
if(valid !== Math.floor(valid)) {
211+
var divisor = schema.multipleOf || schema.divisibleBy;
212+
valid = math.mod(value, divisor);
213+
if(!math.equal(valid, 0.0) && !math.equal(valid, divisor)) {
213214
errors.push({
214215
path: path,
215216
property: schema.multipleOf? 'multipleOf' : 'divisibleBy',
216-
message: this.translate('error_multipleOf', [schema.multipleOf || schema.divisibleBy])
217+
message: this.translate('error_multipleOf', [divisor])
217218
});
218219
}
219220
}
220221

221222
// `maximum`
222223
if(schema.hasOwnProperty('maximum')) {
223-
if(schema.exclusiveMaximum && value >= schema.maximum) {
224+
if(schema.exclusiveMaximum && math.largerEq(value, schema.maximum)) {
224225
errors.push({
225226
path: path,
226227
property: 'maximum',
227228
message: this.translate('error_maximum_excl', [schema.maximum])
228229
});
229230
}
230-
else if(!schema.exclusiveMaximum && value > schema.maximum) {
231+
else if(!schema.exclusiveMaximum && math.larger(value, schema.maximum)) {
231232
errors.push({
232233
path: path,
233234
property: 'maximum',
@@ -238,14 +239,14 @@ JSONEditor.Validator = Class.extend({
238239

239240
// `minimum`
240241
if(schema.hasOwnProperty('minimum')) {
241-
if(schema.exclusiveMinimum && value <= schema.minimum) {
242+
if(schema.exclusiveMinimum && math.smallerEq(value, schema.minimum)) {
242243
errors.push({
243244
path: path,
244245
property: 'minimum',
245246
message: this.translate('error_minimum_excl', [schema.minimum])
246247
});
247248
}
248-
else if(!schema.exclusiveMinimum && value < schema.minimum) {
249+
else if(!schema.exclusiveMinimum && math.smaller(value, schema.minimum)) {
249250
errors.push({
250251
path: path,
251252
property: 'minimum',

0 commit comments

Comments
 (0)