Skip to content

Commit c4d7cba

Browse files
committed
fix #108
1 parent 9aa7e25 commit c4d7cba

File tree

6 files changed

+138
-38
lines changed

6 files changed

+138
-38
lines changed

dist/mock-min.js

+4-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/mock-min.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/mock.js

+42-16
Original file line numberDiff line numberDiff line change
@@ -882,8 +882,8 @@ return /******/ (function(modules) { // webpackBootstrap
882882
var count = range ? !range[2] ? parseInt(range[1], 10) : Random.integer(min, max) : undefined
883883

884884
var decimal = parameters && parameters[4] && parameters[4].match(Constant.RE_RANGE)
885-
var dmin = decimal && parseInt(decimal[1], 10) // || 0,
886-
var dmax = decimal && parseInt(decimal[2], 10) // || 0,
885+
var dmin = decimal && decimal[1] && parseInt(decimal[1], 10) // || 0,
886+
var dmax = decimal && decimal[2] && parseInt(decimal[2], 10) // || 0,
887887
// int || dmin-dmax || 0
888888
var dcount = decimal ? !decimal[2] && parseInt(decimal[1], 10) || Random.integer(dmin, dmax) : undefined
889889

@@ -7664,7 +7664,8 @@ return /******/ (function(modules) { // webpackBootstrap
76647664
var schema = toJSONSchema(template)
76657665
var result = Diff.diff(schema, data)
76667666
for (var i = 0; i < result.length; i++) {
7667-
// console.log(Assert.message(result[i]))
7667+
// console.log(template, data)
7668+
// console.warn(Assert.message(result[i]))
76687669
}
76697670
return result
76707671
}
@@ -7733,10 +7734,24 @@ return /******/ (function(modules) { // webpackBootstrap
77337734
var length = result.length
77347735

77357736
switch (schema.type) {
7736-
// 跳过含有『占位符』的属性值,因为『占位符』返回值的类型可能和模板不一致,例如 '@int' 会返回一个整形值
77377737
case 'string':
7738+
// 跳过含有『占位符』的属性值,因为『占位符』返回值的类型可能和模板不一致,例如 '@int' 会返回一个整形值
77387739
if (schema.template.match(Constant.RE_PLACEHOLDER)) return true
77397740
break
7741+
case 'array':
7742+
if (schema.rule.parameters) {
7743+
// name|count: array
7744+
if (schema.rule.min !== undefined && schema.rule.max === undefined) {
7745+
// 跳过 name|1: array,因为最终值的类型(很可能)不是数组,也不一定与 `array` 中的类型一致
7746+
if (schema.rule.count === 1) return true
7747+
}
7748+
// 跳过 name|+inc: array
7749+
if (schema.rule.parameters[2]) return true
7750+
}
7751+
break
7752+
case 'function':
7753+
// 跳过 `'name': function`,因为函数可以返回任何类型的值。
7754+
return true
77407755
}
77417756

77427757
Assert.equal('type', schema.path, Util.type(data), schema.type, result)
@@ -7748,7 +7763,7 @@ return /******/ (function(modules) { // webpackBootstrap
77487763

77497764
var rule = schema.rule
77507765
var templateType = schema.type
7751-
if (templateType === 'object' || templateType === 'array') return
7766+
if (templateType === 'object' || templateType === 'array' || templateType === 'function') return true
77527767

77537768
// 无生成规则
77547769
if (!rule.parameters) {
@@ -7775,9 +7790,9 @@ return /******/ (function(modules) { // webpackBootstrap
77757790
// 整数部分
77767791
// |min-max
77777792
if (rule.min !== undefined && rule.max !== undefined) {
7778-
Assert.greaterThanOrEqualTo('value', schema.path, parts[0], rule.min, result)
7793+
Assert.greaterThanOrEqualTo('value', schema.path, parts[0], Math.min(rule.min, rule.max), result)
77797794
// , 'numeric instance is lower than the required minimum (minimum: {expected}, found: {actual})')
7780-
Assert.lessThanOrEqualTo('value', schema.path, parts[0], rule.max, result)
7795+
Assert.lessThanOrEqualTo('value', schema.path, parts[0], Math.max(rule.min, rule.max), result)
77817796
}
77827797
// |count
77837798
if (rule.min !== undefined && rule.max === undefined) {
@@ -7805,7 +7820,7 @@ return /******/ (function(modules) { // webpackBootstrap
78057820
case 'string':
78067821
// 'aaa'.match(/a/g)
78077822
actualRepeatCount = data.match(new RegExp(schema.template, 'g'))
7808-
actualRepeatCount = actualRepeatCount ? actualRepeatCount.length : actualRepeatCount
7823+
actualRepeatCount = actualRepeatCount ? actualRepeatCount.length : 0
78097824

78107825
// |min-max
78117826
if (rule.min !== undefined && rule.max !== undefined) {
@@ -7821,7 +7836,7 @@ return /******/ (function(modules) { // webpackBootstrap
78217836

78227837
case 'regexp':
78237838
actualRepeatCount = data.match(new RegExp(schema.template.source.replace(/^\^|\$$/g, ''), 'g'))
7824-
actualRepeatCount = actualRepeatCount ? actualRepeatCount.length : actualRepeatCount
7839+
actualRepeatCount = actualRepeatCount ? actualRepeatCount.length : 0
78257840

78267841
// |min-max
78277842
if (rule.min !== undefined && rule.max !== undefined) {
@@ -7851,12 +7866,13 @@ return /******/ (function(modules) { // webpackBootstrap
78517866
// 有生成规则
78527867
// |min-max
78537868
if (rule.min !== undefined && rule.max !== undefined) {
7854-
Assert.greaterThanOrEqualTo('properties length', schema.path, keys.length, rule.min, result)
7855-
Assert.lessThanOrEqualTo('properties length', schema.path, keys.length, rule.max, result)
7869+
Assert.greaterThanOrEqualTo('properties length', schema.path, keys.length, Math.min(rule.min, rule.max), result)
7870+
Assert.lessThanOrEqualTo('properties length', schema.path, keys.length, Math.max(rule.min, rule.max), result)
78567871
}
78577872
// |count
78587873
if (rule.min !== undefined && rule.max === undefined) {
7859-
Assert.equal('properties length', schema.path, keys.length, rule.min, result)
7874+
// |1, |>1
7875+
if (rule.count !== 1) Assert.equal('properties length', schema.path, keys.length, rule.min, result)
78607876
}
78617877
}
78627878

@@ -7866,7 +7882,13 @@ return /******/ (function(modules) { // webpackBootstrap
78667882
result.push.apply(
78677883
result,
78687884
this.diff(
7869-
schema.properties[i],
7885+
function() {
7886+
var property
7887+
Util.each(schema.properties, function(item /*, index*/ ) {
7888+
if (item.name === keys[i]) property = item
7889+
})
7890+
return property || schema.properties[i]
7891+
}(),
78707892
data[keys[i]],
78717893
keys[i]
78727894
)
@@ -7889,15 +7911,19 @@ return /******/ (function(modules) { // webpackBootstrap
78897911
// 有生成规则
78907912
// |min-max
78917913
if (rule.min !== undefined && rule.max !== undefined) {
7892-
Assert.greaterThanOrEqualTo('items', schema.path, data.length, (rule.min * schema.items.length), result,
7914+
Assert.greaterThanOrEqualTo('items', schema.path, data.length, (Math.min(rule.min, rule.max) * schema.items.length), result,
78937915
'[{utype}] array is too short: {path} must have at least {expected} elements but instance has {actual} elements')
7894-
Assert.lessThanOrEqualTo('items', schema.path, data.length, (rule.max * schema.items.length), result,
7916+
Assert.lessThanOrEqualTo('items', schema.path, data.length, (Math.max(rule.min, rule.max) * schema.items.length), result,
78957917
'[{utype}] array is too long: {path} must have at most {expected} elements but instance has {actual} elements')
78967918
}
78977919
// |count
78987920
if (rule.min !== undefined && rule.max === undefined) {
7899-
Assert.equal('items length', schema.path, data.length, (rule.min * schema.items.length), result)
7921+
// |1, |>1
7922+
if (rule.count === 1) return result.length === length
7923+
else Assert.equal('items length', schema.path, data.length, (rule.min * schema.items.length), result)
79007924
}
7925+
// |+inc
7926+
if (rule.parameters[2]) return result.length === length
79017927
}
79027928

79037929
if (result.length !== length) return false

src/mock/parser.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ module.exports = {
4040
var count = range ? !range[2] ? parseInt(range[1], 10) : Random.integer(min, max) : undefined
4141

4242
var decimal = parameters && parameters[4] && parameters[4].match(Constant.RE_RANGE)
43-
var dmin = decimal && parseInt(decimal[1], 10) // || 0,
44-
var dmax = decimal && parseInt(decimal[2], 10) // || 0,
43+
var dmin = decimal && decimal[1] && parseInt(decimal[1], 10) // || 0,
44+
var dmax = decimal && decimal[2] && parseInt(decimal[2], 10) // || 0,
4545
// int || dmin-dmax || 0
4646
var dcount = decimal ? !decimal[2] && parseInt(decimal[1], 10) || Random.integer(dmin, dmax) : undefined
4747

src/mock/valid/valid.js

+40-14
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ function valid(template, data) {
2828
var schema = toJSONSchema(template)
2929
var result = Diff.diff(schema, data)
3030
for (var i = 0; i < result.length; i++) {
31-
// console.log(Assert.message(result[i]))
31+
// console.log(template, data)
32+
// console.warn(Assert.message(result[i]))
3233
}
3334
return result
3435
}
@@ -97,10 +98,24 @@ var Diff = {
9798
var length = result.length
9899

99100
switch (schema.type) {
100-
// 跳过含有『占位符』的属性值,因为『占位符』返回值的类型可能和模板不一致,例如 '@int' 会返回一个整形值
101101
case 'string':
102+
// 跳过含有『占位符』的属性值,因为『占位符』返回值的类型可能和模板不一致,例如 '@int' 会返回一个整形值
102103
if (schema.template.match(Constant.RE_PLACEHOLDER)) return true
103104
break
105+
case 'array':
106+
if (schema.rule.parameters) {
107+
// name|count: array
108+
if (schema.rule.min !== undefined && schema.rule.max === undefined) {
109+
// 跳过 name|1: array,因为最终值的类型(很可能)不是数组,也不一定与 `array` 中的类型一致
110+
if (schema.rule.count === 1) return true
111+
}
112+
// 跳过 name|+inc: array
113+
if (schema.rule.parameters[2]) return true
114+
}
115+
break
116+
case 'function':
117+
// 跳过 `'name': function`,因为函数可以返回任何类型的值。
118+
return true
104119
}
105120

106121
Assert.equal('type', schema.path, Util.type(data), schema.type, result)
@@ -112,7 +127,7 @@ var Diff = {
112127

113128
var rule = schema.rule
114129
var templateType = schema.type
115-
if (templateType === 'object' || templateType === 'array') return
130+
if (templateType === 'object' || templateType === 'array' || templateType === 'function') return true
116131

117132
// 无生成规则
118133
if (!rule.parameters) {
@@ -139,9 +154,9 @@ var Diff = {
139154
// 整数部分
140155
// |min-max
141156
if (rule.min !== undefined && rule.max !== undefined) {
142-
Assert.greaterThanOrEqualTo('value', schema.path, parts[0], rule.min, result)
157+
Assert.greaterThanOrEqualTo('value', schema.path, parts[0], Math.min(rule.min, rule.max), result)
143158
// , 'numeric instance is lower than the required minimum (minimum: {expected}, found: {actual})')
144-
Assert.lessThanOrEqualTo('value', schema.path, parts[0], rule.max, result)
159+
Assert.lessThanOrEqualTo('value', schema.path, parts[0], Math.max(rule.min, rule.max), result)
145160
}
146161
// |count
147162
if (rule.min !== undefined && rule.max === undefined) {
@@ -169,7 +184,7 @@ var Diff = {
169184
case 'string':
170185
// 'aaa'.match(/a/g)
171186
actualRepeatCount = data.match(new RegExp(schema.template, 'g'))
172-
actualRepeatCount = actualRepeatCount ? actualRepeatCount.length : actualRepeatCount
187+
actualRepeatCount = actualRepeatCount ? actualRepeatCount.length : 0
173188

174189
// |min-max
175190
if (rule.min !== undefined && rule.max !== undefined) {
@@ -185,7 +200,7 @@ var Diff = {
185200

186201
case 'regexp':
187202
actualRepeatCount = data.match(new RegExp(schema.template.source.replace(/^\^|\$$/g, ''), 'g'))
188-
actualRepeatCount = actualRepeatCount ? actualRepeatCount.length : actualRepeatCount
203+
actualRepeatCount = actualRepeatCount ? actualRepeatCount.length : 0
189204

190205
// |min-max
191206
if (rule.min !== undefined && rule.max !== undefined) {
@@ -215,12 +230,13 @@ var Diff = {
215230
// 有生成规则
216231
// |min-max
217232
if (rule.min !== undefined && rule.max !== undefined) {
218-
Assert.greaterThanOrEqualTo('properties length', schema.path, keys.length, rule.min, result)
219-
Assert.lessThanOrEqualTo('properties length', schema.path, keys.length, rule.max, result)
233+
Assert.greaterThanOrEqualTo('properties length', schema.path, keys.length, Math.min(rule.min, rule.max), result)
234+
Assert.lessThanOrEqualTo('properties length', schema.path, keys.length, Math.max(rule.min, rule.max), result)
220235
}
221236
// |count
222237
if (rule.min !== undefined && rule.max === undefined) {
223-
Assert.equal('properties length', schema.path, keys.length, rule.min, result)
238+
// |1, |>1
239+
if (rule.count !== 1) Assert.equal('properties length', schema.path, keys.length, rule.min, result)
224240
}
225241
}
226242

@@ -230,7 +246,13 @@ var Diff = {
230246
result.push.apply(
231247
result,
232248
this.diff(
233-
schema.properties[i],
249+
function() {
250+
var property
251+
Util.each(schema.properties, function(item /*, index*/ ) {
252+
if (item.name === keys[i]) property = item
253+
})
254+
return property || schema.properties[i]
255+
}(),
234256
data[keys[i]],
235257
keys[i]
236258
)
@@ -253,15 +275,19 @@ var Diff = {
253275
// 有生成规则
254276
// |min-max
255277
if (rule.min !== undefined && rule.max !== undefined) {
256-
Assert.greaterThanOrEqualTo('items', schema.path, data.length, (rule.min * schema.items.length), result,
278+
Assert.greaterThanOrEqualTo('items', schema.path, data.length, (Math.min(rule.min, rule.max) * schema.items.length), result,
257279
'[{utype}] array is too short: {path} must have at least {expected} elements but instance has {actual} elements')
258-
Assert.lessThanOrEqualTo('items', schema.path, data.length, (rule.max * schema.items.length), result,
280+
Assert.lessThanOrEqualTo('items', schema.path, data.length, (Math.max(rule.min, rule.max) * schema.items.length), result,
259281
'[{utype}] array is too long: {path} must have at most {expected} elements but instance has {actual} elements')
260282
}
261283
// |count
262284
if (rule.min !== undefined && rule.max === undefined) {
263-
Assert.equal('items length', schema.path, data.length, (rule.min * schema.items.length), result)
285+
// |1, |>1
286+
if (rule.count === 1) return result.length === length
287+
else Assert.equal('items length', schema.path, data.length, (rule.min * schema.items.length), result)
264288
}
289+
// |+inc
290+
if (rule.parameters[2]) return result.length === length
265291
}
266292

267293
if (result.length !== length) return false

test/test.mock.valid.js

+49-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ describe('Mock.valid', function() {
2323

2424
function title(tpl, data, result, test) {
2525
test.title = stringify(tpl) + ' VS ' + stringify(data) + '\n\tresult: ' + stringify(result)
26+
2627
// if (result.length) test.title += '\n\tresult: '
2728
// for (var i = 0; i < result.length; i++) {
2829
// test.title += '\n\t' + result[i].message // stringify(result)
@@ -32,8 +33,8 @@ describe('Mock.valid', function() {
3233
function doit(tpl, data, len) {
3334
it('', function() {
3435
var result = Mock.valid(tpl, data)
35-
expect(result).to.be.an('array').with.length(len)
3636
title(tpl, data, result, this.test)
37+
expect(result).to.be.an('array').with.length(len)
3738
})
3839
}
3940

@@ -209,6 +210,51 @@ describe('Mock.valid', function() {
209210

210211
doit([1, 2, 3], [1, 2, 3, 4], 1)
211212

213+
// 'name|1': array
214+
doit({
215+
'name|1': [1, 2, 3]
216+
}, {
217+
'name': 1
218+
}, 0)
219+
doit({
220+
'name|1': [1, 2, 3]
221+
}, {
222+
'name': 2
223+
}, 0)
224+
doit({
225+
'name|1': [1, 2, 3]
226+
}, {
227+
'name': 3
228+
}, 0)
229+
doit({ // 不检测
230+
'name|1': [1, 2, 3]
231+
}, {
232+
'name': 4
233+
}, 0)
234+
235+
// 'name|+1': array
236+
doit({
237+
'name|+1': [1, 2, 3]
238+
}, {
239+
'name': 1
240+
}, 0)
241+
doit({
242+
'name|+1': [1, 2, 3]
243+
}, {
244+
'name': 2
245+
}, 0)
246+
doit({
247+
'name|+1': [1, 2, 3]
248+
}, {
249+
'name': 3
250+
}, 0)
251+
doit({
252+
'name|+1': [1, 2, 3]
253+
}, {
254+
'name': 4
255+
}, 0)
256+
257+
// 'name|min-max': array
212258
doit({
213259
'name|2-3': [1]
214260
}, {
@@ -244,6 +290,8 @@ describe('Mock.valid', function() {
244290
}, {
245291
'name': [1, 2, 3]
246292
}, 2)
293+
294+
// 'name|count': array
247295
})
248296
describe('Value - Placeholder', function() {
249297
doit({

0 commit comments

Comments
 (0)