Skip to content

Commit a1ebb81

Browse files
committed
Actually make readme example be correct
1 parent e3d8697 commit a1ebb81

File tree

3 files changed

+169
-19
lines changed

3 files changed

+169
-19
lines changed

README.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ var compare = require('json-schema-compare')
1313
var isEqual = compare({
1414
title: 'title 1',
1515
type: ['object'],
16-
uniqueItems: false,
16+
uniqueItems: false, // false is same as undefined
1717
dependencies: {
1818
name: ['age', 'lastName']
1919
},
@@ -37,6 +37,16 @@ var isEqual = compare({
3737
console.log(isEqual) // => true
3838
```
3939

40+
In the example above the name with `minLength: 0`, it is the same as all the all the following:
41+
```json
42+
{ properties: { name : { minLength: undefined } } }
43+
{ properties: { name: {} } }
44+
{ properties: { name: true } }
45+
{ properties: { name: undefined } }
46+
{ properties: {} }
47+
{ }
48+
```
49+
4050
Compare json schemas correctly.
4151

4252
- Ignores sort for arrays where sort does not matter, like required, enum, type, anyOf, oneOf, anyOf, dependencies (if array)

src/index.js

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,11 @@ var keys = obj => isPlainObject(obj) || Array.isArray(obj) ? Object.keys(obj) :
1414
var has = (obj, key) => obj.hasOwnProperty(key)
1515
var stringArray = arr => sortBy(uniq(arr))
1616
var undefEmpty = val => undef(val) || (Array.isArray(val) && val.length === 0)
17-
var keyValEqual = (a, b, key, compare) => b && has(b, key) && a && has(a, key) && compare(a[key], b[key])
1817
var undefAndZero = (a, b) => (undef(a) && b === 0) || (undef(b) && a === 0) || isEqual(a, b)
1918
var falseUndefined = (a, b) => (undef(a) && b === false) || (undef(b) && a === false) || isEqual(a, b)
2019
var emptySchema = schema => undef(schema) || isEqual(schema, {}) || schema === true
21-
var emptyObjUndef = schema => undef(schema) || isEqual(schema, {})
2220
var isSchema = val => undef(val) || isPlainObject(val) || val === true || val === false
21+
var propOrFake = (a, key) => undef(a) ? undefined : a[key]
2322

2423
function undefArrayEqual(a, b) {
2524
if (undefEmpty(a) && undefEmpty(b)) {
@@ -37,25 +36,24 @@ function unsortedNormalizedArray(a, b) {
3736

3837
function schemaGroup(a, b, key, compare) {
3938
var allProps = uniq(keys(a).concat(keys(b)))
40-
if (emptyObjUndef(a) && emptyObjUndef(b)) {
39+
if (emptySchema(a) && emptySchema(b)) {
4140
return true
42-
} else if (emptyObjUndef(a) && keys(b).length) {
43-
return false
44-
} else if (emptyObjUndef(b) && keys(a).length) {
45-
return false
4641
}
4742

4843
return allProps.every(function(key) {
49-
var aVal = a[key]
50-
var bVal = b[key]
44+
var aVal = propOrFake(a, key)
45+
var bVal = propOrFake(b, key)
46+
47+
// TODO, remove when no longer support dependencies
5148
if (Array.isArray(aVal) && Array.isArray(bVal)) {
5249
return isEqual(stringArray(a), stringArray(b))
5350
} else if (Array.isArray(aVal) && !Array.isArray(bVal)) {
5451
return false
5552
} else if (Array.isArray(bVal) && !Array.isArray(aVal)) {
5653
return false
5754
}
58-
return keyValEqual(a, b, key, compare)
55+
56+
return compare(aVal, bVal)
5957
})
6058
}
6159

@@ -127,15 +125,14 @@ function compare(a, b, options) {
127125
return a === b
128126
}
129127

130-
if ((a === undefined && b === false) || (b === undefined && a === false)) {
128+
if ((a !== false && b === false) || (b !== false && a === false)) {
131129
return false
132130
}
133131

134-
if ((undef(a) && !undef(b)) || (!undef(a) && undef(b))) {
135-
return false
136-
}
132+
var aKeys = isPlainObject(a) ? Object.keys(a) : []
133+
var bKeys = isPlainObject(b) ? Object.keys(b) : []
137134

138-
var allKeys = uniq(Object.keys(a).concat(Object.keys(b)))
135+
var allKeys = uniq([...aKeys, ...bKeys])
139136

140137
if (options.ignore.length) {
141138
allKeys = allKeys.filter(k => options.ignore.indexOf(k) === -1)
@@ -150,8 +147,8 @@ function compare(a, b, options) {
150147
}
151148

152149
return allKeys.every(function(key) {
153-
var aValue = a[key]
154-
var bValue = b[key]
150+
var aValue = propOrFake(a, key)
151+
var bValue = propOrFake(b, key)
155152

156153
if (schemaProps.indexOf(key) !== -1) {
157154
return compare(aValue, bValue, options)

test/specs/index.spec.js

Lines changed: 144 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ describe('comparison', function() {
3030
minLength: 0
3131
}
3232
}
33-
}, false, {
33+
}, true, {
3434
ignore: ['title']
3535
})
3636
})
@@ -79,12 +79,155 @@ describe('comparison', function() {
7979
}, true)
8080
})
8181

82+
it('compares schema undefined and true', function() {
83+
compare(true, undefined, true)
84+
})
85+
86+
it('compares schema empty object and true', function() {
87+
compare(true, {}, true)
88+
})
89+
90+
it('compares sub schema with matching sub validators equal to undefined', function() {
91+
compare({
92+
properties: {
93+
name: undefined
94+
}
95+
}, {
96+
properties: {
97+
name: {
98+
minLength: 0
99+
}
100+
}
101+
}, true)
102+
})
103+
104+
it('compares sub schema with matching sub validators equal to missing', function() {
105+
compare({
106+
properties: {
107+
}
108+
}, {
109+
properties: {
110+
name: {
111+
minLength: 0
112+
}
113+
}
114+
}, true)
115+
})
116+
117+
it('compares sub schema with matching sub validators equal to no properties', function() {
118+
compare({}, {
119+
properties: {
120+
name: {
121+
minLength: 0
122+
}
123+
}
124+
}, true)
125+
126+
compare(true, {
127+
properties: {
128+
name: {
129+
minLength: 0
130+
}
131+
}
132+
}, true)
133+
134+
compare(undefined, {
135+
properties: {
136+
name: {
137+
minLength: 0,
138+
minItems: 0
139+
}
140+
}
141+
}, true)
142+
})
143+
144+
it('compares sub schema with matching sub validators equal to no properties', function() {
145+
compare({
146+
properties: {
147+
name: false
148+
}
149+
}, {
150+
properties: {
151+
name: {
152+
minLength: 0
153+
}
154+
}
155+
}, false)
156+
})
157+
82158
it('compares equal patternProperties empty object and undefined', function() {
83159
compare({
84160
patternProperties: {}
85161
}, {}, true)
86162
})
87163

164+
it('compares patternProperties with regular property as false', function() {
165+
compare({
166+
patternProperties: {
167+
'.+': {
168+
minLength: 9
169+
}
170+
}
171+
}, {
172+
properties: {
173+
name: {
174+
minLength: 9
175+
}
176+
}
177+
}, false)
178+
})
179+
180+
it('compares equal patternProperties', function() {
181+
compare({
182+
patternProperties: {
183+
'.+': {
184+
minLength: 9
185+
}
186+
}
187+
}, {
188+
patternProperties: {
189+
'.+': {
190+
minLength: 9
191+
}
192+
}
193+
}, true)
194+
})
195+
196+
it('compares equal patternProperties', function() {
197+
compare({
198+
patternProperties: {
199+
'.+': {
200+
minLength: 0
201+
},
202+
'..+': {
203+
minLength: 0
204+
},
205+
'...+': {
206+
minLength: 0
207+
}
208+
}
209+
}, {
210+
patternProperties: {
211+
'.+': {},
212+
'..+': true
213+
}
214+
}, true)
215+
})
216+
217+
it('compares patternProperties, subschema false', function() {
218+
compare({
219+
patternProperties: {
220+
'.+': false
221+
}
222+
}, {
223+
patternProperties: {
224+
'.+': {
225+
minLength: 0
226+
}
227+
}
228+
}, false)
229+
})
230+
88231
it('compares equal dependencies empty object and undefined', function() {
89232
compare({
90233
dependencies: {}

0 commit comments

Comments
 (0)