forked from apigee-127/sway
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Vlad Barosan
committed
May 14, 2018
1 parent
32784ba
commit 5aff220
Showing
3 changed files
with
76 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
|
||
'use strict'; | ||
|
||
function enumValidator (report, schema, json) { | ||
var match = false; | ||
var idx = schema.enum.length; | ||
var caseInsensitiveMatch = false; | ||
|
||
while (idx--) { | ||
if (json === schema.enum[idx]) { | ||
match = true; | ||
return; | ||
} else if ( | ||
typeof json === 'string' && | ||
typeof schema.enum[idx] === 'string' && | ||
json.toUpperCase() === schema.enum[idx].toUpperCase() | ||
) { | ||
caseInsensitiveMatch = true; | ||
} | ||
} | ||
|
||
if (caseInsensitiveMatch === true) { | ||
report.addCustomError( | ||
'ENUM_CASE_MISMATCH', | ||
'Enum doesn not match case for: {0}', | ||
[json], | ||
null, | ||
schema.description | ||
); | ||
} else if (match === false) { | ||
report.addError('ENUM_MISMATCH', [json], null, schema.description); | ||
} | ||
} | ||
|
||
function requiredPropertyValidator (report, schema, json) { | ||
// http://json-schema.org/latest/json-schema-validation.html#rfc.section.5.4.3.2 | ||
|
||
if ( | ||
!(typeof json === 'object' && json === Object(json) && !Array.isArray(json)) | ||
) { | ||
return; | ||
} | ||
var idx = schema.required.length; | ||
var requiredPropertyName; | ||
var xMsMutability; | ||
|
||
while (idx--) { | ||
requiredPropertyName = schema.required[idx]; | ||
xMsMutability = (schema.properties && schema.properties[`${requiredPropertyName}`]) && schema.properties[`${requiredPropertyName}`]['x-ms-mutability']; | ||
|
||
// If a response has x-ms-mutability property and its missing the read we can skip this step | ||
if (this.validateOptions && this.validateOptions.isResponse && xMsMutability && xMsMutability.indexOf('read') === -1) { | ||
continue; | ||
} | ||
if (json[requiredPropertyName] === undefined) { | ||
report.addError( | ||
'OBJECT_MISSING_REQUIRED_PROPERTY', | ||
[requiredPropertyName], | ||
null, | ||
schema.description | ||
); | ||
} | ||
} | ||
} | ||
|
||
module.exports.enumValidator = enumValidator; | ||
module.exports.requiredPropertyValidator = requiredPropertyValidator; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters