Skip to content
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

Add configurable rule for strict string definitions #34

Merged
merged 3 commits into from
Apr 26, 2024

Conversation

adamaltman
Copy link
Member

@adamaltman adamaltman commented Apr 22, 2024

Strict string length definitions

Make sure that there is a min and max length defined on every string except for enums.

Resolved

See the history of this rule I need help with this rule because it doesn't work as expected.

To execute the rule I need the same object in two where clauses, and our documentation is unclear if this is allowed or not.

    where: 
      - subject: 
          type: Schema
          property: type
        assertions:  
          const: string
      - subject: 
          type: Schema
          property: enum
        assertions: 
          defined: false        

The where object is part of a where list which must be defined in order from the root node. Nodes may be skipped in between the subject node types of the where list and those defined in the root subject type.

https://redocly.com/docs/cli/rules/configurable-rules/#where-object

It doesn't state if you can use the same node twice. If I can't, how do I accomplish this rule?

@tatomyr
Copy link
Contributor

tatomyr commented Apr 22, 2024

@adamaltman a node cannot be used twice in the where object. This is a restriction of current implementation.

how do I accomplish this rule?

There are several options to this, e.g. you can define a custom function (in a plugin) which will combine the two assertions into one, or you can try 'moving' one of the checks into the actual assertion (depends on the behaviour you're trying to achieve).
Writing a custom rule is also always an option. And lastly, we can try rewriting the core logic that doesn't allow to reuse the nodes (but I suspect that the restriction was implemented intentionally).

What the rule should do?

@adamaltman
Copy link
Member Author

It should make sure all strings have minLength and maxLength defined unless they have an enum defined.

@tatomyr
Copy link
Contributor

tatomyr commented Apr 22, 2024

Could you try if something like this works for you?

  rule/string-schemas:
    where: 
      - subject: 
          type: Schema
          property: type
        assertions:  
          const: string
    subject: 
      type: Schema
    assertions: 
      requireAny: 
        - minLength
        - maxLength
        - enum
      mutuallyRequired: 
        - minLength
        - maxLength

@adamaltman
Copy link
Member Author

adamaltman commented Apr 22, 2024

This does work. (Maybe I'll some more test cases first before we declare victory. 😆 )

@adamaltman adamaltman marked this pull request as ready for review April 22, 2024 20:38
@adamaltman adamaltman requested review from lornajane and tatomyr April 23, 2024 13:25
Copy link
Contributor

@tatomyr tatomyr left a comment

Choose a reason for hiding this comment

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

Left a minor comment.

Comment on lines +18 to +33
subject:
type: Schema
where:
- subject:
type: Schema
property: type
assertions:
const: string
assertions:
requireAny:
- minLength
- maxLength
- enum
mutuallyRequired:
- minLength
- maxLength
Copy link
Contributor

Choose a reason for hiding this comment

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

Albeit it's correct, the opposite order seems more natural to me. I think it's easier to read if the top-level subject and assertions are grouped together and the where lies above or or below. What do you think?

Copy link
Contributor

@lornajane lornajane left a comment

Choose a reason for hiding this comment

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

Nice addition!

@lornajane lornajane merged commit 2581a52 into main Apr 26, 2024
2 checks passed
@lornajane lornajane deleted the json-schema-strict-strings branch April 26, 2024 07:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants