From time to time, there's a need to have some more flexibility than just the core functions within Spectral. This is where the ability to define your own custom functions is awesome!
Here's a collection of custom Spectral functions I find myself using from time to time.
| Function | Description | Details | 
|---|---|---|
| hal-casing | Apply casing rules in combination with Hypertext Application Language (HAL) keywords | view | 
| curie-casing | Apply casing rules in combination with Compact URI syntax | view | 
To leverage these rules along with you *.spectral.yaml|json ruleset, do the following:
- create a folder relative to your ruleset file called custom-functions
- within your ruleset file specify the functionDirandfunctionsas follows:
functions: [hal-casing, curie-casing]
functionsDir: ./custom-functionsA full example ruleset taking advantage of these rules can be found in the .spectral.yaml file in this repo.
Feel free to use or extend this work (referencing to this repo is preferred). PRs back with more useful functions are much appreciated 💚.
🌟 the repo if it's been useful for your work.
If your APIs are conforming to the HAL specification and leveraging application/hal+json, then you might find it cumbersome to use the core casing function. This hal-casing function gives you the ability to apply your casing of choice (e.g., camelCase or snake-case), while safely allowing reserved properties for HAL as defined in the HAL specification.
Supported cases:
| name | sample | 
|---|---|
| flat | verylongname | 
| camel | veryLongName | 
| pascal | VeryLongName | 
| kebab | very-long-name | 
| cobol | VERY-LONG-NAME | 
| snake | very_long_name | 
| macro | VERY_LONG_NAME | 
camelCase-property-names:
description: Property names must be camelCase
severity: error
recommended: true
message: '{{property}} MUST follow camelCase'
given: $..properties.*~
then:
    function: hal-casing
    functionOptions:
    type: camelnotice use of
_embeddedand_linksreserved HAL keywords
schemas:
Policy:
    type: object
    properties:
    policyNumber:
        description: The insurance policy number
        type: string
        example: IXP12345678
    iso100:
        description: Policy complies with random ISO
        type: boolean
        example: true           
PagedPolicies:
    type: object
    properties:
    _embedded:
        type: object
        properties:
        policies:
            type: array
            items:
            $ref: '#/components/schemas/Policy'
    _links:
        type: object
        properties:
        self:
            type: object
            properties:
            href:
                type: stringIf your APIs specify or leverage compact URIs conforming to the CURIE Syntax, then you might find it cumbersome to use the core casing function. This curie-casing function gives you the ability to apply your casing of choice (e.g., camelCase or snake-case),
This curie-casing function gives you the ability to apply your casing of choice (e.g., camelCase or snake-case), while safely allowing CURIEs. Currently the function applies the chosen casing to both the prefix and the reference (e.g., both sides of the : delimiter).
Supported cases:
| name | sample | 
|---|---|
| flat | verylongname | 
| camel | veryLongName | 
| pascal | VeryLongName | 
| kebab | very-long-name | 
| cobol | VERY-LONG-NAME | 
| snake | very_long_name | 
| macro | VERY_LONG_NAME | 
  curie-property-names:
    description: CURIE properties must follow camelCase
    severity: error
    recommended: true
    message: '{{property}} MUST follow camelCase on both sides of the ":"'
    given: $..properties.*~
    then:
      function: curie-casing
      functionOptions:
        type: camel   schemas:
    Policy:
      type: object
      properties:
        policyNumber:
          description: The insurance policy number
          type: string
          example: IXP12345678
        iso100:
          description: Policy complies with random ISO
          type: boolean
          example: true           
    PagedPolicies:
      type: object
      properties:
        _embedded:
          type: object
          properties:
            policies:
              type: array
              items:
                $ref: '#/components/schemas/Policy'
        _links:
          type: object
          properties:
            self:
              type: object
              properties:
                href:
                  type: string
            ea:policy:
              type: object
              properties:
                self:
                  type: object
                  properties:
                    href:
                      type: string
                ea:customer:
                  type: object
                  properties:
                    href:
                      type: string