Skip to content

Instantly share code, notes, and snippets.

@jpbetz
Created August 3, 2023 15:04
Show Gist options
  • Save jpbetz/f01f0d21055407f7a9701ca51198c395 to your computer and use it in GitHub Desktop.
Save jpbetz/f01f0d21055407f7a9701ca51198c395 to your computer and use it in GitHub Desktop.
Check regex validity with CEL
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
  name: regextests.stable.example.com
spec:
  group: stable.example.com
  versions:
    - name: v1
      served: true
      storage: true
      schema:
        openAPIV3Schema:
          type: object
          properties:
            spec:
              type: object
              properties:
                regex:
                  type: string
                  x-kubernetes-validations:
                    # This rule only checks if the regex is valid.
                    # It results in an error if regex is ivalid, else the expression evaluates to true.
                    - rule: "''.findAll(self).size() >= 0" 
  scope: Namespaced
  names:
    plural: regextests
    singular: regextest
    kind: RegexTest
apiVersion: stable.example.com/v1
kind: RegexTest
metadata:
  name: invalid
spec:
  regex: '[0-9'
kubectl apply -f isregex/isregex-cr-invalid.yaml
The RegexTest "invalid" is invalid: spec.regex: Invalid value: "string": Illegal regex: error parsing regexp: missing closing ]: `[0-9` evaluating rule: ''.findAll(self).size() >= 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment