Skip to content

Commit

Permalink
+ Mock.valid() 的返回值中增加 path
Browse files Browse the repository at this point in the history
  • Loading branch information
nuysoft committed Aug 20, 2015
1 parent ed5174d commit a2a646d
Show file tree
Hide file tree
Showing 5 changed files with 136 additions and 84 deletions.
100 changes: 58 additions & 42 deletions dist/mock.js
Original file line number Diff line number Diff line change
Expand Up @@ -1124,7 +1124,7 @@ define('mock/random/color',['./color_convert', './color_dict'], function(Convert
parseInt(rgb[1], 10) + ', ' +
parseInt(rgb[2], 10) + ')'
},
// rgb(128,255,255,0.3)
// rgba(128,255,255,0.3)
rgba: function() {
var hsv = this._goldenRatioColor()
var rgb = Convert.hsv2rgb(hsv)
Expand Down Expand Up @@ -5647,7 +5647,7 @@ define('mock/random/address',['./address_dict.js'], function(DICT) {
var province = this.pick(DICT)
var city = this.pick(province.children)
var county = this.pick(city.children) || {
name: ''
name: '-'
}
return prefix ? [province.name, city.name, county.name].join(' ') : county.name
},
Expand Down Expand Up @@ -7830,29 +7830,32 @@ define(
function(
Constant, Util, Parser
) {
function toJSONSchema(template, name) {
function toJSONSchema(template, name, path /* Internal Use Only */ ) {
// type rule properties items
path = path || []
var result = {
name: typeof name === 'string' ? name.replace(Constant.RE_KEY, '$1') : name,
template: template,
type: Util.type(template), // 可能不准确,例如 { 'name|1': [{}, {} ...] }
rule: Parser.parse(name)
}
result.path = path.slice(0)
result.path.push(name === undefined ? 'ROOT' : result.name)

switch (result.type) {
case 'array':
result.items = []
Util.each(template, function(value, index) {
result.items.push(
toJSONSchema(value, index)
toJSONSchema(value, index, result.path)
)
})
break
case 'object':
result.properties = []
Util.each(template, function(value, name) {
result.properties.push(
toJSONSchema(value, name)
toJSONSchema(value, name, result.path)
)
})
break
Expand Down Expand Up @@ -7889,7 +7892,7 @@ define(
[Regexp Demo](http://demos.forbeslindesay.co.uk/regexp/)
*/
define('mock/valid/valid',['mock/util', 'mock/schema/schema'], function(Util, toJSONSchema) {

function valid(template, data) {
var schema = toJSONSchema(template)
var result = Diff.diff(schema, data)
Expand Down Expand Up @@ -7955,15 +7958,15 @@ define('mock/valid/valid',['mock/util', 'mock/schema/schema'], function(Util, to
name: function(schema, data, name, result) {
var length = result.length

Assert.equal('name', name, name + '', schema.name + '', result)
Assert.equal('name', schema.path, name + '', schema.name + '', result)

if (result.length !== length) return false
return true
},
type: function(schema, data, name, result) {
var length = result.length

Assert.equal('type', name, Util.type(data), schema.type, result)
Assert.equal('type', schema.path, Util.type(data), schema.type, result)

if (result.length !== length) return false
return true
Expand All @@ -7977,7 +7980,7 @@ define('mock/valid/valid',['mock/util', 'mock/schema/schema'], function(Util, to

// 无生成规则
if (!schema.rule.parameters) {
Assert.equal('value', name, data, schema.template, result)
Assert.equal('value', schema.path, data, schema.template, result)
return
}

Expand All @@ -7990,25 +7993,25 @@ define('mock/valid/valid',['mock/util', 'mock/schema/schema'], function(Util, to
// 整数部分
// |min-max
if (rule.min !== undefined && rule.max !== undefined) {
Assert.greaterThanOrEqualTo('value', name, parts[0], rule.min, result,
'numeric instance is lower than the required minimum (minimum: {expected}, found: {actual})')
Assert.lessThanOrEqualTo('value', name, parts[0], rule.max, result)
Assert.greaterThanOrEqualTo('value', schema.path, parts[0], rule.min, result)
// , 'numeric instance is lower than the required minimum (minimum: {expected}, found: {actual})')
Assert.lessThanOrEqualTo('value', schema.path, parts[0], rule.max, result)
}
// |count
if (rule.min !== undefined && rule.max === undefined) {
Assert.equal('value', name, parts[0], rule.min, result, '[value] ' + name)
Assert.equal('value', schema.path, parts[0], rule.min, result, '[value] ' + name)
}

// 小数部分
if (rule.decimal) {
// |dmin-dmax
if (rule.dmin !== undefined && rule.dmax !== undefined) {
Assert.greaterThanOrEqualTo('value', name, parts[1].length, rule.dmin, result)
Assert.lessThanOrEqualTo('value', name, parts[1].length, rule.dmax, result)
Assert.greaterThanOrEqualTo('value', schema.path, parts[1].length, rule.dmin, result)
Assert.lessThanOrEqualTo('value', schema.path, parts[1].length, rule.dmax, result)
}
// |dcount
if (rule.dmin !== undefined && rule.dmax === undefined) {
Assert.equal('value', name, parts[1].length, rule.dmin, result)
Assert.equal('value', schema.path, parts[1].length, rule.dmin, result)
}
}

Expand All @@ -8023,12 +8026,12 @@ define('mock/valid/valid',['mock/util', 'mock/schema/schema'], function(Util, to

// |min-max
if (rule.min !== undefined && rule.max !== undefined) {
Assert.greaterThanOrEqualTo('value', name, actualRepeatCount, rule.min, result)
Assert.lessThanOrEqualTo('value', name, actualRepeatCount, rule.max, result)
Assert.greaterThanOrEqualTo('value', schema.path, actualRepeatCount, rule.min, result)
Assert.lessThanOrEqualTo('value', schema.path, actualRepeatCount, rule.max, result)
}
// |count
if (rule.min !== undefined && rule.max === undefined) {
Assert.equal('value', name, actualRepeatCount, rule.min, result)
Assert.equal('value', schema.path, actualRepeatCount, rule.min, result)
}
break
}
Expand All @@ -8045,17 +8048,17 @@ define('mock/valid/valid',['mock/util', 'mock/schema/schema'], function(Util, to

// 无生成规则
if (!schema.rule.parameters) {
Assert.equal('properties length', name, keys.length, schema.properties.length, result)
Assert.equal('properties length', schema.path, keys.length, schema.properties.length, result)
} else {
// 有生成规则
// |min-max
if (rule.min !== undefined && rule.max !== undefined) {
Assert.greaterThanOrEqualTo('properties length', name, keys.length, rule.min, result)
Assert.lessThanOrEqualTo('properties length', name, keys.length, rule.max, result)
Assert.greaterThanOrEqualTo('properties length', schema.path, keys.length, rule.min, result)
Assert.lessThanOrEqualTo('properties length', schema.path, keys.length, rule.max, result)
}
// |count
if (rule.min !== undefined && rule.max === undefined) {
Assert.equal('properties length', name, keys.length, rule.min, result)
Assert.equal('properties length', schema.path, keys.length, rule.min, result)
}
}

Expand Down Expand Up @@ -8084,19 +8087,19 @@ define('mock/valid/valid',['mock/util', 'mock/schema/schema'], function(Util, to

// 无生成规则
if (!schema.rule.parameters) {
Assert.equal('items length', name, data.length, schema.items.length, result)
Assert.equal('items length', schema.path, data.length, schema.items.length, result)
} else {
// 有生成规则
// |min-max
if (rule.min !== undefined && rule.max !== undefined) {
Assert.greaterThanOrEqualTo('items', name, data.length, (rule.min * schema.items.length), result,
Assert.greaterThanOrEqualTo('items', schema.path, data.length, (rule.min * schema.items.length), result,
'[{utype}] array is too short: {path} must have at least {expected} elements but instance has {actual} elements')
Assert.lessThanOrEqualTo('items', name, data.length, (rule.max * schema.items.length), result,
Assert.lessThanOrEqualTo('items', schema.path, data.length, (rule.max * schema.items.length), result,
'[{utype}] array is too long: {path} must have at most {expected} elements but instance has {actual} elements')
}
// |count
if (rule.min !== undefined && rule.max === undefined) {
Assert.equal('items length', name, data.length, (rule.min * schema.items.length), result)
Assert.equal('items length', schema.path, data.length, (rule.min * schema.items.length), result)
}
}

Expand Down Expand Up @@ -8132,84 +8135,97 @@ define('mock/valid/valid',['mock/util', 'mock/schema/schema'], function(Util, to
*/
var Assert = {
message: function(item) {
return ( /*item.message ||*/
return (item.message ||
'[{utype}] Expect {path}\'{ltype} is {action} {expected}, but is {actual}')
.replace('{utype}', item.type.toUpperCase())
.replace('{ltype}', item.type.toLowerCase())
.replace('{path}', item.path)
.replace('{path}', Util.isArray(item.path) && item.path.join('.') || item.path)
.replace('{action}', item.action)
.replace('{expected}', item.expected)
.replace('{actual}', item.actual)
},
equal: function(type, path, actual, expected, result, message) {
if (actual === expected) return true
result.push({
var item = {
path: path,
type: type,
actual: actual,
expected: expected,
action: 'equal to'
})
action: 'equal to',
message: message
}
item.message = Assert.message(item)
result.push(item)
return false
},
notEqual: function(type, path, actual, expected, result, message) {
if (actual !== expected) return true
result.push({
var item = {
path: path,
type: type,
actual: actual,
expected: expected,
action: 'not equal to',
message: message
})
}
item.message = Assert.message(item)
result.push(item)
return false
},
greaterThan: function(type, path, actual, expected, result, message) {
if (actual > expected) return true
result.push({
var item = {
path: path,
type: type,
actual: actual,
expected: expected,
action: 'greater than',
message: message
})
}
item.message = Assert.message(item)
result.push(item)
return false
},
lessThan: function(type, path, actual, expected, result, message) {
if (actual < expected) return true
result.push({
var item = {
path: path,
type: type,
actual: actual,
expected: expected,
action: 'less to',
message: message
})
}
item.message = Assert.message(item)
result.push(item)
return false
},
greaterThanOrEqualTo: function(type, path, actual, expected, result, message) {
if (actual >= expected) return true
result.push({
var item = {
path: path,
type: type,
actual: actual,
expected: expected,
action: 'greater than or equal to',
message: message
})
}
item.message = Assert.message(item)
result.push(item)
return false
},
lessThanOrEqualTo: function(type, path, actual, expected, result, message) {
if (actual <= expected) return true
result.push({
var item = {
path: path,
type: type,
actual: actual,
expected: expected,
action: 'less than or equal to',
message: message
})
}
item.message = Assert.message(item)
result.push(item)
return false
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/mock/random/address.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ define(['./address_dict.js'], function(DICT) {
var province = this.pick(DICT)
var city = this.pick(province.children)
var county = this.pick(city.children) || {
name: ''
name: '-'
}
return prefix ? [province.name, city.name, county.name].join(' ') : county.name
},
Expand Down
9 changes: 6 additions & 3 deletions src/mock/schema/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,32 @@ define(
function(
Constant, Util, Parser
) {
function toJSONSchema(template, name) {
function toJSONSchema(template, name, path /* Internal Use Only */ ) {
// type rule properties items
path = path || []
var result = {
name: typeof name === 'string' ? name.replace(Constant.RE_KEY, '$1') : name,
template: template,
type: Util.type(template), // 可能不准确,例如 { 'name|1': [{}, {} ...] }
rule: Parser.parse(name)
}
result.path = path.slice(0)
result.path.push(name === undefined ? 'ROOT' : result.name)

switch (result.type) {
case 'array':
result.items = []
Util.each(template, function(value, index) {
result.items.push(
toJSONSchema(value, index)
toJSONSchema(value, index, result.path)
)
})
break
case 'object':
result.properties = []
Util.each(template, function(value, name) {
result.properties.push(
toJSONSchema(value, name)
toJSONSchema(value, name, result.path)
)
})
break
Expand Down
Loading

0 comments on commit a2a646d

Please sign in to comment.