question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Handling multiple 'Match' rules picks last rule

See original GitHub issue

I have a scenario where a certain value needs to match 2 separate regular expressions, and emit a different error message for each of them.

Example:

RuleFor(c => c.MobilePhoneNumber)
    .NotEmpty() 
    .Length(10)
    .Matches(@"^3").WithMessage("'{PropertyName}' should start with '3'.")
    .Matches(@"^\d*$").WithMessage("'{PropertyName}' should only contain digits.");

However, when the swagger UI is rendered, I’m only getting the last regular expression

mobilePhoneNumber* 
 | stringmaxLength: 10
 | minLength: 10
 | pattern: ^\d*$

Is there something that could be done from the library side to properly show both rules in this particular case, so that the caller knows it needs to match more than one expression for the value to be valid?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
petriashevcommented, Jan 9, 2021

Some investigations:

Using https://json-schema-validator.herokuapp.com/index.jsp

Schema:

{
    "title": "Person",
    "type": "object",
    "properties": {
        "mobilePhoneNumber": {
            "type": "string",
            "maxLength": 10,
            "minLength": 10,
            "allOf": [
                {
                    "pattern": "^\\d*$"
                },
                {
                    "pattern": "^3"
                }
            ]
        }
    }
}

TestCases:

  • 3123456780 (success)
  • 3a23456780 (failure - has non digit symbol)
  • 312 (failure - length less then 10l)

Sample with two failed patterns image

Result: Schema with two patterns works as expected!

1reaction
petriashevcommented, Jan 5, 2021

I started to use allOf in new version and all patterns are in schema. But Swagger UI doesnot recognizes two patterns and shows only last!!!

Try this sample in https://editor.swagger.io/

Simple example:

openapi: 3.0.1
info:
  title: My API
  version: v1
paths:
  /api/Person/AddPhoneEntity:
    post:
      tags:
        - Person
      parameters:
        - name: MobilePhoneNumber
          in: query
          required: true
          schema:
            $ref: '#/components/schemas/PhoneNumber'
      responses:
        '200':
          description: Success
          content:
            text/json:
              schema:
                $ref: '#/components/schemas/PhoneEntity'
components:
  schemas:
    PhoneEntity:
      required:
        - mobilePhoneNumber
      type: object
      properties:
        mobilePhoneNumber:
          $ref: '#/components/schemas/PhoneNumber'
      additionalProperties: false
      
    PhoneNumber:
      type: string
      maxLength: 10
      minLength: 10
      allOf:
        - pattern: ^\d*$
        - pattern: ^3 

Example with refs

openapi: 3.0.1
info:
  title: My API
  version: v1
paths:
  /api/Person/AddPhoneEntity:
    post:
      tags:
        - Person
      parameters:
        - name: MobilePhoneNumber
          in: query
          required: true
          schema:
            $ref: '#/components/schemas/PhoneNumber'
      responses:
        '200':
          description: Success
          content:
            text/json:
              schema:
                $ref: '#/components/schemas/PhoneEntity'
components:
  schemas:
    PhoneEntity:
      required:
        - mobilePhoneNumber
      type: object
      properties:
        mobilePhoneNumber:
          $ref: '#/components/schemas/PhoneNumber'
      additionalProperties: false
      
    PhoneNumber:
      type: string
      allOf:
        - $ref: '#/components/schemas/OnlyDigits'
        - $ref: '#/components/schemas/StartsWith3'
        - $ref: '#/components/schemas/TenSymbols'
        
    OnlyDigits:
      type: string
      pattern: ^\d*$
      
    StartsWith3:
      type: string
      pattern: ^3   
      
    TenSymbols:
      type: string
      maxLength: 10
      minLength: 10

Result:

image

Read more comments on GitHub >

github_iconTop Results From Across the Web

Is this a viable approach to resolving multiple matches in ...
I am unsure of the best way to handle when multiple rules are matched. The existing lexers I've looked at handle this by...
Read more >
Cisco ISE - What does "Multiple Matched Rule Applies" ...
This means that ISE just doesnt stop if you meet the first rule condition it keeps processing till you match all the rules...one...
Read more >
Implementing multiple match rules in IDQ
I have to implement multiple match rules in IDQ in the following way: First Name AND Last Name AND Gender - All Exact....
Read more >
Rule-based matching · spaCy Usage Documentation
spaCy features a rule-matching engine, the Matcher , that operates over tokens, similar to regular expressions. The rules can refer to token annotations ......
Read more >
Selection rules and actions
You can explicitly select on multiple tables and schemas by specifying multiple selection rules.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found