String Format ruleset question.
PawelKolat opened this issue · 2 comments
First of all apologies if this is not the right place to post this I'm using Prism and Spectral for basic OAS validation and would need some guidance in rulesets writing. If this is not the right place please could you point me to the right forum.
Problem:
Designers make mistakes in defining models, e.g.
EmailAddress:
type: string
description: 'This is a generic email type'
I'd like to make a rule which checks that format: email has been added and that it's spelled correctly not, e.g. format: emeil
EmailAddress:
type: string
format: email
description: 'This is a generic email type'
I was wondering if anyone has done anything similar?
Solution attempt:
My attempt https://github.com/PawelKolat/style-guides/blob/master/.spectral.yml
Detects missing format and misspelled format in template https://github.com/PawelKolat/style-guides/blob/master/template.yaml
As you may see it's only limited to schema and parameters and I'm unable to make it work well to target .e.g
emailHeader:
type: object
#I excluded header in the regex to not expect the format: email here
description: the email Header object.
properties:
senderEmailAddress:
type: string
#format missing - but I want to detect it here.
description: |
Address from which the email is sent.
example: abc@gmail.com
I can of course change my regex and target it everywhere but I'm beginning to think there must be a better way of doing this. I already have 4 rules for one email. This will explode when I add date, and patterns check for strings which need patterns.
Maybe my regex could be more flexible in this detection but I'm running out of ideas.
Many thanks
You can make then an array which will help a little, as you'll be able to have 2 rules instead of 4.
2my-oas3-api-email-schema-must-have-email-format:
description: "email must be of email format - schema"
message: " 2 This property should be of email format"
formats: ["oas3"]
given: "$.components[schemas][?(
@property.toLowerCase().includes('email')&&
((@property.toLowerCase() != 'emailheader'))&&
((@property.toLowerCase() != 'emailrequest'))&&
((@property.toLowerCase() != 'emailbody'))&&
((@property.toLowerCase() != 'emailtemplate'))
)].format"
then:
- field: format
function: truthy
- function: pattern
functionOptions:
match: 'email'
But generally im not sure what else you could do, this is a very domain specific rule, which you've already got problems with as it should not match about certain properties that do have email in there. 😅
Thanks I didn't know I can pass it as an array so that's great to know. Sadly the regex in 2my-oas3-api-email-schema-must-have-email-format ends with .format and in 1my-oas3-api-email-schema-must-have-format-exclude-special-models the .format is omitted. So it wouldn't work.
Sorry about the late response, and thanks for your comment.