Skip to content

Instantly share code, notes, and snippets.

@kinlane
Last active November 21, 2024 17:55
Show Gist options
  • Save kinlane/61ee94aa579d6067e8a5e85261caad1f to your computer and use it in GitHub Desktop.
Save kinlane/61ee94aa579d6067e8a5e85261caad1f to your computer and use it in GitHub Desktop.
json-schema-spectral-rules-1
---
rules:
# ID
json-schema-2020-12-id-error:
description: Schema MUST have a unique identifier for each object.
message: Schema MUST Have a $id.
severity: error
given: $
then:
field: "$id"
function: truthy
json-schema-2020-12-id-info:
description: Schema MUST have a unique identifier for each object.
message: Schema Has an $id.
severity: info
given: $
then:
field: "$id"
function: falsy
json-schema-2020-12-id-source-url-error:
description: The id for a schema MUST have a valid URL pointing to central register.
message: The $id for schema MUST reference central registry.
severity: error
given: $
then:
field: "$id"
function: pattern
functionOptions:
match: \b(example.com)\b
json-schema-2020-12-id-source-url-info:
description: The id for a schema MUST have a valid URL pointing to central register.
message: The $id for schema references central registry.
severity: info
given: $
then:
field: "$id"
function: pattern
functionOptions:
notMatch: \b(example.com)\b
# Schema
json-schema-2020-12-schema-error:
description: Schema MUST have a $schema property.
message: Schema MUST Have a $schema.
severity: error
given: $
then:
field: "$schema"
function: truthy
json-schema-2020-12-schema-info:
description: Schema MUST have a $schema property.
message: Schema Has a $schema.
severity: info
given: $
then:
field: "$schema"
function: falsy
json-schema-2020-12-schema-draft-error:
description: The $schema for a schema MUST use the latest draft
message: The $schema for schema MUST use the latest draft.
severity: error
given: $
then:
field: "$schema"
function: pattern
functionOptions:
match: 'https://json-schema.org/draft/2020-12/schema'
json-schema-2020-12-schema-draft-info:
description: The $schema for a schema MUST use the latest draft
message: The $schema for schema uses the latest draft.
severity: info
given: $
then:
field: "$schema"
function: pattern
functionOptions:
notMatch: 'https://json-schema.org/draft/2020-12/schema'
# Title
json-schema-2020-12-title-error:
description: Schema MUST have a title for the entire object, describing an object in plain language.
message: Schema MUST Have a Title.
severity: error
given: $
then:
field: title
function: truthy
json-schema-2020-12-title-info:
description: Schema MUST have a title for the entire object, describing an object in plain language.
message: Schemas Has a Title.
severity: info
given: $
then:
field: title
function: falsy
json-schema-2020-12-title-pascal-case-error:
description: Schema names should always be PascalCase, and be used in title for a schema to help ensure readability and consistency.
message: Schema Title MUST Be PascalCase.
severity: error
given: $
then:
- field: title
function: pattern
functionOptions:
match: ^[A-Z](([a-z]+[A-Z]?)*)$
- field: title
function: pattern
functionOptions:
match: ^[A-Z](([a-z0-9]+[A-Z]?)*)$
json-schema-2020-12-title-pascal-case-info:
description: Schema names should always be PascalCase, and be used in title for a schema to help ensure readability and consistency.
message: Schema Title Are PascalCase.
severity: info
given: $
then:
- field: title
function: pattern
functionOptions:
notMatch: ^[A-Z](([a-z]+[A-Z]?)*)$
- field: title
function: pattern
functionOptions:
notMatch: ^[A-Z](([a-z0-9]+[A-Z]?)*)$
json-schema-2020-12-title-length-error:
description: Schema names and resulting title should be kept to less than 25 characters.
message: Schema Names MUST Be Less Than 25 Characters
severity: error
given: $
then:
field: title
function: length
functionOptions:
max: 25
json-schema-2020-12-title-length-error:
description: Schema names and resulting title should be kept to greater than 3 characters.
message: Schema Names MUST Be Greater Than 3 Characters
severity: error
given: $
then:
field: title
function: length
functionOptions:
min: 3
#Description
json-schema-2020-12-description-error:
description: Schema MUST have a description for the entire object, explaining in plain language what the object is for.
message: Schema MUST Have a Description.
severity: error
given: $
then:
field: description
function: truthy
json-schema-2020-12-description-info:
description: Schema MUST have a description for the entire object, explaining in plain language what the object is for.
message: Schemas Has a Description.
severity: info
given: $
then:
field: description
function: falsy
json-schema-2020-12-description-length-error:
description: The description for a schema should not be too long, helping keep as readable and consumable as possible by users.
message: Schema Description MUST be Less Than 250 Characters
severity: error
given: $
then:
field: description
function: length
functionOptions:
max: 250
# Types
json-schema-2020-12-type-error:
description: All schema must have a type property.
message: Schema MUST Have Type Property
severity: error
given: $
then:
field: type
function: truthy
json-schema-2020-12-type-info:
description: All schema must have a type property.
message: Schema Have Type Property
severity: info
given: $
then:
field: type
function: falsy
# Property Names
json-schema-2020-12-properties-names-camel-case-error:
description: All schema properties should be camel case for consistency.
message: Schema Property Names MUST Be camelCase.
severity: error
given: $.properties
then:
- field: "@key"
function: pattern
functionOptions:
notMatch: ^[A-Z][a-z0-9]*[A-Z0-9][a-z0-9]+[A-Za-z0-9]*$
json-schema-2020-12-properties-names-camel-case-info:
description: All schema properties should be camel case for consistency.
message: Schema Property Names Are camelCase.
severity: info
given: $.properties
then:
- field: "@key"
function: pattern
functionOptions:
match: ^[A-Z][a-z0-9]*[A-Z0-9][a-z0-9]+[A-Za-z0-9]*$
json-schema-2020-12-properties-names-max-error:
description: It makes sense to keep schema property names a consistent length.
message: Schema Properties Name Length MUST Be Less Than 25
severity: error
given: $.properties
then:
field: "@key"
function: length
functionOptions:
max: 25
json-schema-2020-12-properties-names-min-error:
description: It makes sense to keep schema property names a consistent length.
message: Schema Properties Name Length MUST Be More Than 3
severity: error
given: $.properties
then:
field: "@key"
function: length
functionOptions:
min: 3
#Property Descriptions
json-schema-2020-12-properties-descriptions-error:
description: Schema property descriptions should be complete and useful to users.
message: Schema Properties MUST Have Description
severity: error
given: $.properties.*
then:
field: description
function: truthy
json-schema-2020-12-properties-descriptions-info:
description: Schema property descriptions should be complete and useful to users.
message: Schema Properties Have Description
severity: info
given: $.properties.*
then:
field: description
function: falsy
json-schema-2020-12-properties-descriptions-length-error:
description: Schema property descriptions should not be too long, helping ensure they aren't too verbose and complex.
message: Schema Properties Description MUST Be Less Than 250 Characters
severity: error
given: $.properties.*
then:
field: description
function: length
functionOptions:
max: 250
json-schema-2020-12-properties-descriptions-length-error:
description: Schema property descriptions should not be too short, helping ensure they are actually helpful.
message: Schema Properties Description MUST Be More Than 10 Characters.
severity: error
given: $.properties.*
then:
field: description
function: length
functionOptions:
min: 10
# Types
json-schema-2020-12-type-error:
description: All schema properties must have a type property.
message: Schema MUST Have Type Property
severity: error
given: $.properties.*
then:
field: type
function: truthy
json-schema-2020-12-type-info:
description: All schema properties must have a type property.
message: Schema Have Type Property
severity: info
given: $.properties.*
then:
field: type
function: falsy
# Properties Required
json-schema-2020-12-required-error:
description: All schema should have possess a required property.
message: Schema MUST Have a Required Property for Objects.
severity: error
given: $
then:
field: required
function: truthy
json-schema-2020-12-required-info:
description: All schema should have possess a required property.
message: Schema Have a Required Property for Objects.
severity: info
given: $
then:
field: required
function: falsy
json-schema-2020-12-required-error:
description: All schema should have at least one required property.
message: MUST Be At Least One Tag
given: $
severity: error
then:
field: required
function: length
functionOptions:
min: 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment