-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feat: Upgrade AJV from 6 to 8 #31
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
|
@@ -6,5 +6,5 @@ | |
"../test/helpers/**/*.ts" | ||
], | ||
"stopSpecOnExpectationFailure": false, | ||
"random": true | ||
"random": false | ||
} |
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
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
131 changes: 12 additions & 119 deletions
131
lib/swagger-mock-validator/validate-spec-and-mock/validate-json.ts
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 |
---|---|---|
@@ -1,128 +1,21 @@ | ||
import Ajv from 'ajv'; | ||
import _ from 'lodash'; | ||
import {traverseJsonSchema} from '../common/traverse-json-schema'; | ||
import {ParsedSpecJsonSchema, ParsedSpecJsonSchemaCore} from '../spec-parser/parsed-spec'; | ||
import {isBinary} from './validate-json/binary'; | ||
import {isByte} from './validate-json/byte'; | ||
import {doubleAjvKeyword, formatForDoubleNumbers, isDouble} from './validate-json/double'; | ||
import {floatAjvKeyword, formatForFloatNumbers, isFloat} from './validate-json/float'; | ||
import {formatForInt32Numbers, int32AjvKeyword, isInt32} from './validate-json/int32'; | ||
import {formatForInt64Numbers, int64AjvKeyword, isInt64} from './validate-json/int64'; | ||
import { formatForString, isString, stringAjvKeyword } from './validate-json/string'; | ||
import {isPassword} from './validate-json/password'; | ||
import draft4MetaSchema from 'ajv/lib/refs/json-schema-draft-04.json'; | ||
|
||
const removeLeadingDotIfPresent = (dataPath: string): string => | ||
dataPath.replace(/^\./, ''); | ||
|
||
const getRawValueFromJson = (rawJson: any, dataPath?: string): any => | ||
dataPath ? _.get(rawJson, removeLeadingDotIfPresent(dataPath)) : rawJson; | ||
|
||
const addSwaggerFormatsAndKeywords = (ajv: Ajv.Ajv, rawJson: any) => { | ||
ajv.addFormat('binary', isBinary); | ||
ajv.addFormat('byte', isByte); | ||
ajv.addFormat('password', isPassword); | ||
ajv.addKeyword(doubleAjvKeyword, { | ||
type: 'number', | ||
validate: (_schema: any, _data: number, _parentSchema: any, dataPath?: string) => { | ||
const rawValue = getRawValueFromJson(rawJson, dataPath); | ||
return isDouble(rawValue); | ||
} | ||
}); | ||
ajv.addKeyword(floatAjvKeyword, { | ||
type: 'number', | ||
validate: (_schema: any, _data: number, _parentSchema: any, dataPath?: string) => { | ||
const rawValue = getRawValueFromJson(rawJson, dataPath); | ||
return isFloat(rawValue); | ||
} | ||
}); | ||
ajv.addKeyword(int32AjvKeyword, { | ||
type: 'integer', | ||
validate: (_schema: any, _data: number, _parentSchema: any, dataPath?: string) => { | ||
const rawValue = getRawValueFromJson(rawJson, dataPath); | ||
return isInt32(rawValue); | ||
} | ||
}); | ||
ajv.addKeyword(int64AjvKeyword, { | ||
type: 'integer', | ||
validate: (_schema: any, _data: number, _parentSchema: any, dataPath?: string) => { | ||
const rawValue = getRawValueFromJson(rawJson, dataPath); | ||
return isInt64(rawValue); | ||
} | ||
}); | ||
ajv.addKeyword(stringAjvKeyword, { | ||
type: 'string', | ||
validate: (_schema: any, _data: string, _parentSchema: any, dataPath?: string) => { | ||
const rawValue = getRawValueFromJson(rawJson, dataPath); | ||
return isString(rawValue); | ||
} | ||
}); | ||
}; | ||
|
||
const nonSwaggerAjvFormats = [ | ||
'email', | ||
'hostname', | ||
'ipv4', | ||
'ipv6', | ||
'json-pointer', | ||
'regex', | ||
'relative-json-pointer', | ||
'time', | ||
'uri', | ||
'uuid', | ||
'url', | ||
'uri-template', | ||
'uri-reference' | ||
]; | ||
|
||
const alwaysTrue = () => true; | ||
|
||
const removeNonSwaggerAjvFormats = (ajv: Ajv.Ajv) => { | ||
nonSwaggerAjvFormats.forEach((formatName) => { | ||
ajv.addFormat(formatName, alwaysTrue); | ||
}); | ||
}; | ||
|
||
const updateSchemaPropertyToDraft4 = (schema: ParsedSpecJsonSchema) => { | ||
(schema as any).$schema = 'http://json-schema.org/draft-04/schema'; | ||
}; | ||
|
||
const changeTypeToKeywordForCustomFormats = (schema?: ParsedSpecJsonSchema) => { | ||
traverseJsonSchema(schema, (mutableSchema: ParsedSpecJsonSchemaCore) => { | ||
formatForDoubleNumbers(mutableSchema); | ||
formatForFloatNumbers(mutableSchema); | ||
formatForInt32Numbers(mutableSchema); | ||
formatForInt64Numbers(mutableSchema); | ||
formatForString(mutableSchema); | ||
}); | ||
}; | ||
const createAjvForDraft4 = (userOptions: Ajv.Options) => { | ||
const optionsRequiredForDraft4 = { | ||
logger: false, | ||
schemaId: 'id' | ||
}; | ||
|
||
const options: Ajv.Options = _.defaultsDeep({}, userOptions, optionsRequiredForDraft4); | ||
|
||
const ajv = new Ajv(options); | ||
ajv.addMetaSchema(draft4MetaSchema); | ||
|
||
return ajv; | ||
}; | ||
import Ajv from 'ajv'; | ||
import addFormats from "ajv-formats" | ||
import {ParsedSpecJsonSchema} from '../spec-parser/parsed-spec'; | ||
|
||
export const validateJson = (jsonSchema: ParsedSpecJsonSchema, json: any, numbersSentAsStrings?: boolean) => { | ||
const ajv = createAjvForDraft4({ | ||
const ajv = new Ajv({ | ||
allErrors: true, | ||
coerceTypes: numbersSentAsStrings || false, | ||
verbose: true | ||
logger: false, | ||
strictSchema: false, | ||
}); | ||
addFormats(ajv); | ||
ajv.addKeyword({ | ||
keyword: 'collectionFormat', | ||
type: 'array', | ||
}); | ||
|
||
addSwaggerFormatsAndKeywords(ajv, json); | ||
removeNonSwaggerAjvFormats(ajv); | ||
ajv.validate(jsonSchema, json); | ||
|
||
const ajvCompatibleJsonSchema = _.cloneDeep(jsonSchema); | ||
changeTypeToKeywordForCustomFormats(ajvCompatibleJsonSchema); | ||
updateSchemaPropertyToDraft4(ajvCompatibleJsonSchema); | ||
ajv.validate(ajvCompatibleJsonSchema, _.cloneDeep(json)); | ||
return ajv.errors || []; | ||
}; |
1 change: 0 additions & 1 deletion
1
lib/swagger-mock-validator/validate-spec-and-mock/validate-json/binary.ts
This file was deleted.
Oops, something went wrong.
5 changes: 0 additions & 5 deletions
5
lib/swagger-mock-validator/validate-spec-and-mock/validate-json/byte.ts
This file was deleted.
Oops, something went wrong.
22 changes: 0 additions & 22 deletions
22
lib/swagger-mock-validator/validate-spec-and-mock/validate-json/double.ts
This file was deleted.
Oops, something went wrong.
22 changes: 0 additions & 22 deletions
22
lib/swagger-mock-validator/validate-spec-and-mock/validate-json/float.ts
This file was deleted.
Oops, something went wrong.
24 changes: 0 additions & 24 deletions
24
lib/swagger-mock-validator/validate-spec-and-mock/validate-json/int32.ts
This file was deleted.
Oops, something went wrong.
24 changes: 0 additions & 24 deletions
24
lib/swagger-mock-validator/validate-spec-and-mock/validate-json/int64.ts
This file was deleted.
Oops, something went wrong.
9 changes: 0 additions & 9 deletions
9
lib/swagger-mock-validator/validate-spec-and-mock/validate-json/is-type-supported.ts
This file was deleted.
Oops, something went wrong.
1 change: 0 additions & 1 deletion
1
lib/swagger-mock-validator/validate-spec-and-mock/validate-json/password.ts
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OpenAPI defines these as booleans, as did AJV@6.
But since https://json-schema.org/draft-06/json-schema-release-notes.html, it has been converted to a number. AJV followed suite.