-
Notifications
You must be signed in to change notification settings - Fork 45
feat: redesign validations #105
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
Conversation
marcelltoth
left a comment
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.
Looking good, great work Jakub!
The hard blockers are "match pattern value" because it's a functional non-compliance (unless @philsturgeon says it's OK), and the potential key duplication bug, but that's trivial to fix.
| maxLength: value => `<= ${value} characters`, | ||
| }; | ||
|
|
||
| const validationsFormatter = (name: string) => (value: unknown[] | unknown): ValidationFormat | null => { |
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.
An unknown[] | unknown is still unknown, although I can see your point that it increases clarity somewhat.
We shouldn't be using this many unknowns to begin with, but that's not your fault, that's how validations are defined unfortunately.
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.
Also I'd consider calling the function createValidationsFormatter as it's a HOF. It isn't the formatter itself, rather it creates the function that will be the formatter.
| maxItems: value => `<= ${value} items`, | ||
| maxLength: value => `<= ${value} characters`, |
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.
Nice catch, my bad!
| ); | ||
| } | ||
| const validation = Array.isArray(value) ? value : [value]; | ||
| const KeyValueValidation = ({ className, name, values }: { className?: string; name: string; values: string[] }) => { |
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.
This function is much clearer now, great job!
| {values.map(value => ( | ||
| <Text key={value} ml={2} px={1} fontFamily="mono" border rounded="lg" className={className}> | ||
| {value} | ||
| </Text> | ||
| ))} |
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.
Are you sure values are unique? I know that's how things generally are, but I like to be very cautious with keys, because they cause hard-to-debug undefined behavior when non-unique. Please either run it through uniq or use another key, such as the index. Whichever you think fits the business requirements better.
| expect(wrapper).toIncludeText('>= 10<= 40'); | ||
| expect(wrapper).toIncludeText('Allowed values:10203040'); | ||
| expect(wrapper).toIncludeText('Default value:20'); | ||
| expect(wrapper).toIncludeText('Multiple of value:10'); | ||
| expect(wrapper).toIncludeText('Example value:20'); |
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 see you're attempting to use the good approach even with the not-so-good framework. We can likely discuss introduction of RTL in here later. Looks good for now.
| multipleOf: validationsFormatter('Multiple of'), | ||
| pattern: validationsFormatter('Match pattern'), |
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.
Your abstraction falls a bit short here. These two shouldn't say values.
I'm willing to let it slide for now though if @philsturgeon is, too.
cc @philsturgeon Is it acceptable that it currently says "Match pattern value" and "Multiple of value"?
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.
Oh yeah, right, it shouldn't. I'll think of better way
| const validations = getValidationsFromSchema(node); | ||
| const wrapper = mount(<Validations validations={validations} />); | ||
|
|
||
| expect(wrapper).toIncludeText('>= 10<= 40'); |
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.
You could probably just do
| expect(wrapper).toIncludeText('>= 10<= 40'); | |
| expect(wrapper.text()).toMatchInlineSnapshot( |
and verify the output
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.
Do we like snaphosts? 😉
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.
😭
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.
Well, I don't like them, BUT inline snapshots are okay.
| } | ||
|
|
||
| function stringifyValue(value: unknown) { | ||
| return typeof value === 'string' ? `"${value}"` : String(value); |
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.
Any reason not to use JSON.stringify?
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.
Right, might be better
| enum: validationsFormatter('Allowed'), | ||
| examples: validationsFormatter('Example'), | ||
| example: validationsFormatter('Example'), | ||
| ['x-example']: validationsFormatter('Example'), |
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'm not sure if json-schema-tree supports this. You might want to verify it.
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.
It might not. Same with const so I took it from fragment to not update JST but we might want to do that. Though not sure if x-example is worth it.
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.
so I took it from fragment to not update JST
We shouldn't be using fragment. 😬
b774989 to
a59d0b9
Compare
|
@philsturgeon do you have any remarks from your point of view? |
| ...(schemaNode.annotations.examples ? { examples: schemaNode.annotations.examples } : null), | ||
| } | ||
| : null), | ||
| ...('fragment' in schemaNode |
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.
We shouldn't be using fragment.
You can leave a todo note here (or even better create an issue in jst) and I'll get it done soon.
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.
Alright, I'll put a note here and create issue for JST
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.
JST bumped and fragment usage removed
philsturgeon
left a comment
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.
Not sure if these comments matter but I noticed some invalid OAS. I’ll review on computer soon but I’m sick and dying today so still try and look again when I can brain.
28db2b4 to
6a57473
Compare
|
🎉 This PR is included in version 4.0.0 🎉 The release is available on: Your semantic-release bot 📦🚀 |
Resolves stoplightio/elements#668
Makes validations being displayed in more human friendly way.