-
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
feat: Convert project_config_schema and json_schema_validator modules to TS #524
Conversation
* @return {boolean} true if the given object is valid | ||
*/ | ||
export var validate = function(jsonObject) { | ||
export function validate(jsonObject: Record<string, unknown>): boolean { |
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 instead of using Record...
you wanna just use any
so we can validate anything against this. An alternative type to use maybe would be JSON
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 have tried any
before , which gives Argument 'jsonObject' should be typed with a non-any type
error. Did not think of JSON
, thanks @mikeng13!
export default schema; | ||
const schema = schemaDefinition as JSONSchema4 | ||
|
||
export { schema } |
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 makes sense to keep the export default schema
here since we will only have main export from this module.
Then down below instead of importing import { schema } ....
you would simply import schema from ...
5d70fda
to
a004a3c
Compare
b2bc6a0
to
a004a3c
Compare
}; | ||
|
||
export default schema; | ||
const schema = schemaDefinition as 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.
I think it's better to define the variable as having JSONSchema4
type in the first place, rather than coercing it here with as
. Any issue doing it that way?
const schemaDefinition: 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.
@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
* @return {boolean} true if the given object is valid | ||
*/ | ||
export var validate = function(jsonObject) { | ||
export function validate(jsonObject: JSON): boolean { |
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.
JSON
isn't the right type for this. This JSON
is the global JSON
object with methods parse
and stringify
. See here.
I think the right type for this is {}
, an empty object with no properties. This is what's expected as the jsonSchemaValidate
function's first argument.
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 Here is the eslint error from using {}
(which is not intuitive at all IMO):
error Don't use `{}` as a type. `{}` actually means "any non-nullish value".
- If you want a type meaning "any object", you probably want `Record<string, unknown>` instead.
- If you want a type meaning "any value", you probably want `unknown` instead @typescript-eslint/ban-types
Looks like Omit<Record<any, never>, keyof any>
can be used for explicitly empty object or something less strict like Record<string, never>
. Any thoughts @mjc1283?
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.
Hmm yea, this is forbidden by our lint rules. Actually I'm not sure why - I'm going to research it. Let's change it to unknown
.
a098778
to
b64381b
Compare
b64381b
to
1bc1d7f
Compare
1bc1d7f
to
fa3e204
Compare
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.
lgtm
… to TS (#524) * Convert project_config_schema and json_schema_validator modules to TS * Address Mike's comments * Update validate function to accept empty object
Summary
Convert
project_config_schema
andjson_schema_validator
modules from JS to TS.Test plan