-
Notifications
You must be signed in to change notification settings - Fork 83
feat: Convert project_config_schema and json_schema_validator modules to TS #524
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
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,21 +17,21 @@ import { sprintf } from '@optimizely/js-sdk-utils'; | |
import { validate as jsonSchemaValidator } from 'json-schema'; | ||
|
||
import { ERROR_MESSAGES } from '../enums'; | ||
import projectConfigSchema from '../../core/project_config/project_config_schema'; | ||
import schema from '../../core/project_config/project_config_schema'; | ||
|
||
var MODULE_NAME = 'JSON_SCHEMA_VALIDATOR'; | ||
const MODULE_NAME = 'JSON_SCHEMA_VALIDATOR'; | ||
|
||
/** | ||
* Validate the given json object against the specified schema | ||
* @param {Object} jsonObject The object to validate against the schema | ||
* @return {Boolean} True if the given object is valid | ||
* @param {object} jsonObject The object to validate against the schema | ||
* @return {boolean} true if the given object is valid | ||
*/ | ||
export var validate = function(jsonObject) { | ||
export function validate(jsonObject: JSON): boolean { | ||
|
||
if (!jsonObject) { | ||
throw new Error(sprintf(ERROR_MESSAGES.NO_JSON_PROVIDED, MODULE_NAME)); | ||
} | ||
|
||
var result = jsonSchemaValidator(jsonObject, projectConfigSchema); | ||
const result = jsonSchemaValidator(jsonObject, schema); | ||
if (result.valid) { | ||
return true; | ||
} else { | ||
|
@@ -42,8 +42,4 @@ export var validate = function(jsonObject) { | |
} | ||
throw new Error(sprintf(ERROR_MESSAGES.INVALID_JSON, MODULE_NAME)); | ||
} | ||
}; | ||
|
||
export default { | ||
validate: validate, | ||
}; | ||
} |
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.
I think it's better to define the variable as having
JSONSchema4
type in the first place, rather than coercing it here withas
. Any issue doing it that way?Uh oh!
There was an error while loading. Please reload this page.
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.
@mjc1283 I agree, but I have tried it this way, and it complains
Type '{ projectId: { type: "string"; required: true; }; accountId: ... ... }] is not assignable to type '{ [k: string]: JSONSchema4; }'
. 😕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.
As discussed, I will create additional Jira tasks to fix/improve json schema validation and to document all the changes that we should do. I left
const schema = schemaDefinition as JSONSchema4
so that all our unit tests can still pass without having to modify any of json schema files. FYI @mjc1283 @mikeng13