Skip to content

feat(functions): add unicodeRegExp option to schema core function #2809

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

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from

Conversation

mtso
Copy link

@mtso mtso commented Apr 17, 2025

Fixes #2419.

Checklist

  • Tests added / updated
  • Docs added / updated

Does this PR introduce a breaking change?

  • Yes
  • No

Screenshots

Before:

Screenshot 2025-04-17 at 8 09 46 PM

After:

Screenshot 2025-04-17 at 8 09 02 PM

Added unicodeRegExp to core function docs
Screenshot 2025-04-17 at 8 21 44 PM

Additional context

Adds an option unicodeRegExp to the schema core function. Defaults to "false". When "true", uses the unicode flag "u" with "pattern" and "patternProperties" fields during schema validation; otherwise does not use the "u" flag when "false" or omitted.

The unicodeRegExp option is optional, and thus backwards compatible with the existing behavior of spectral's schema core function. When the option is omitted, the behavior is defaulted to "false".

The unicodeRegExp option is also added as an optional flag to oasSchema and oasExample oas functions. To preserve the existing behavior of the standard oas ruleset, unicodeRegExp: false has been explicitly added to the standard oas ruleset's duplicated-entry-in-enum, oas2-valid-schema-example, oas2-valid-media-example, oas3-valid-media-example and oas3-valid-schema-example rules.

Adding this option allows rulesets that use the schema core function to opt into using the unicode "u" flag when validating the "pattern" and "patternProperty" fields.

Adding this option to oasExample and oasSchema also allows rulesets that extend from spectral:oas to replace the oas3-valid-media-example and oas3-valid-schema-example rules with a version that opts into using unicode regular expressions. Doing so is currently a little involved, though, requiring re-specifying the given block of the rules as shown below. But it at least makes the option available to extending rulesets.

// functions/oasExample.js
import { default as oasExample } from '@stoplight/spectral-rulesets/dist/oas/functions/oasExample';

export default oasExample;
# spectral.yaml
extends: ['spectral:oas']

functions:
- 'oasExample'

rules:
  oas3-valid-schema-example:
    given:
      - $.components.schemas..[?(@property !== 'properties' && @ && (@ && @.example !== void 0 || @.default !== void 0) && (@.enum || @.type || @.format || @.$ref || @.properties || @.items))]
      - $..content..[?(@property !== 'properties' && @ && (@ && @.example !== void 0 || @.default !== void 0) && (@.enum || @.type || @.format || @.$ref || @.properties || @.items))]
      - $..headers..[?(@property !== 'properties' && @ && (@ && @.example !== void 0 || @.default !== void 0) && (@.enum || @.type || @.format || @.$ref || @.properties || @.items))]
      - $..parameters..[?(@property !== 'properties' && @ && (@ && @.example !== void 0 || @.default !== void 0) && (@.enum || @.type || @.format || @.$ref || @.properties || @.items))]
    then:
      function: oasExample
      functionOptions:
        schemaField: '$'
        oasVersion: 3
        type: 'schema'
        unicodeRegExp: true

  oas3-valid-media-example:
    given:
      - '$..content..[?(@ && @.schema && (@.example !== void 0 || @.examples))]'
      - '$..headers..[?(@ && @.schema && (@.example !== void 0 || @.examples))]'
      - '$..parameters..[?(@ && @.schema && (@.example !== void 0 || @.examples))]'
    then:
      function: oasExample
      functionOptions:
        schemaField: 'schema'
        oasVersion: 3
        type: 'media'
        unicodeRegExp: true

@mtso mtso requested a review from a team as a code owner April 17, 2025 12:44
@mtso mtso changed the title Add unicodeRegExp option to schema core function feat(functions): Add unicodeRegExp option to schema core function Apr 17, 2025
@mtso mtso changed the title feat(functions): Add unicodeRegExp option to schema core function feat(functions): add unicodeRegExp option to schema core function Apr 17, 2025
@mtso
Copy link
Author

mtso commented Apr 24, 2025

@mnaumanali94 / @frankkilcommins , when you get the chance, could you please take a look?

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.

Spectal complains when a openapi pattern uses a Unicode character set
1 participant