Skip to content

Conversation

@mallachari
Copy link

@mallachari mallachari commented Feb 15, 2021

Resolves stoplightio/elements#668

Makes validations being displayed in more human friendly way.

image

@mallachari mallachari requested review from a team and philsturgeon February 15, 2021 02:50
Copy link
Contributor

@marcelltoth marcelltoth left a 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 => {
Copy link
Contributor

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.

Copy link
Contributor

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.

Comment on lines +36 to +37
maxItems: value => `<= ${value} items`,
maxLength: value => `<= ${value} characters`,
Copy link
Contributor

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[] }) => {
Copy link
Contributor

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!

Comment on lines 128 to 132
{values.map(value => (
<Text key={value} ml={2} px={1} fontFamily="mono" border rounded="lg" className={className}>
{value}
</Text>
))}
Copy link
Contributor

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.

Comment on lines 25 to 29
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');
Copy link
Contributor

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.

Comment on lines 57 to 58
multipleOf: validationsFormatter('Multiple of'),
pattern: validationsFormatter('Match pattern'),
Copy link
Contributor

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"?

Copy link
Author

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');
Copy link
Contributor

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

Suggested change
expect(wrapper).toIncludeText('>= 10<= 40');
expect(wrapper.text()).toMatchInlineSnapshot(

and verify the output

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we like snaphosts? 😉

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😭

Copy link
Contributor

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);
Copy link
Contributor

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?

Copy link
Author

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'),
Copy link
Contributor

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.

Copy link
Author

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.

Copy link
Contributor

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. 😬

@mallachari mallachari requested a review from P0lip February 15, 2021 15:04
@mallachari
Copy link
Author

@philsturgeon do you have any remarks from your point of view?

...(schemaNode.annotations.examples ? { examples: schemaNode.annotations.examples } : null),
}
: null),
...('fragment' in schemaNode
Copy link
Contributor

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.

Copy link
Author

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

Copy link
Author

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

Copy link

@philsturgeon philsturgeon left a 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.

@mallachari mallachari requested a review from P0lip February 16, 2021 09:28
@mallachari mallachari merged commit 42ff7a4 into beta Feb 16, 2021
@mallachari mallachari deleted the feat/validations branch February 16, 2021 10:23
@stoplight-bot
Copy link
Collaborator

🎉 This PR is included in version 4.0.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

JSV: Display all property attributes below description

6 participants