Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 2 additions & 43 deletions .spectral.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ functions:
- conditionallyDefined
- requireExampleOrRef
- requireRequestBodyDescription
- xEnturPermissions

rules:
# =============================================================================
Expand Down Expand Up @@ -164,53 +165,11 @@ rules:
# -------------------------------------------------------------------------

entur-permissions:
message: "x-entur-permissions must match the schema"
documentationUrl: "https://github.com/entur/api-guidelines/blob/main/guidelines.md#233-documenting-permissions-for-partner-endpoints"
severity: error
given: $.paths.*[get,post,put,patch,delete,options,head,trace].x-entur-permissions
then:
function: schema
functionOptions:
schema:
type: object
required:
- value
properties:
description:
type: string
value:
$ref: "#/$defs/permission-node"
$defs:
permission-node:
oneOf:
- $ref: "#/$defs/permission-leaf"
- $ref: "#/$defs/permission-all"
- $ref: "#/$defs/permission-any"

permission-leaf:
type: string
pattern: "^[0-9a-zA-ZæøåØÆÅ][0-9a-zA-ZæøåØÆÅ.\\-]{0,99}:(les|opprett|endre|slett)$"

permission-all:
type: object
required:
- all
properties:
all:
type: array
items:
- $ref: "#/$defs/permission-node"

permission-any:
type: object
required:
- any
properties:
any:
type: array
items:
- $ref: "#/$defs/permission-node"

function: xEnturPermissions

# -------------------------------------------------------------------------
# 2.4 Entur Metadata
Expand Down
47 changes: 47 additions & 0 deletions functions/xEnturPermissions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
module.exports = (targetVal, options, context) => {
if (typeof targetVal !== "object") {
return [{message: "\"x-entur-permissions\" must be an object."}]
}

const messages = []
for (const prop of Object.getOwnPropertyNames(targetVal)) {
if (!["value", "description"].includes(prop)) {
messages.push({ message: `Unrecognized property \"${prop}\"`, path: [...context.path, prop] })
}
}
if (targetVal.description != null && typeof targetVal.description !== "string") {
messages.push({ message: "\"description\" property must be a string", path: [...context.path, "description"] })
}

messages.push(...xEnturPermissionsValue(targetVal.value, [...context.path, "value"]))

return messages
}

function xEnturPermissionsValue(targetVal, path = []) {
if (typeof targetVal === "string") {
const match = targetVal.toLocaleLowerCase().match(/^[0-9a-zæøå][0-9a-zæøå.\\-]{0,99}:(les|opprett|endre|slett)$/)
if (!match) {
return [{ message: "Must match format \"permission:(les/opprett/endre/slett)\"", path}]
} else {
return []
}
} else if (typeof targetVal === "object") {
const props = Object.getOwnPropertyNames(targetVal);

if (props.length != 1 || !["any", "all"].includes(props[0])) {
return [{ message: "Must be an object with either \"any\" or \"all\"", path}]
}

const val = targetVal[props[0]];
if (!Array.isArray(val)) {
return [{ message: "Must be an array", path: [...path, props[0]] }]
}

return val.flatMap((x, index) => xEnturPermissionsValue(x, [...path, props[0], index]));
} else {
return [{ message: "Must be either an object or a string", path }]

}

}
10 changes: 6 additions & 4 deletions specs/reference-spec-with-errors.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"openapi": "3.1.0",
"info": {
"title": "Items API",

Check warning on line 4 in specs/reference-spec-with-errors.json

View workflow job for this annotation

GitHub Actions / lint-reference-spec-with-errors

entur-info-title-no-api

"version": "1.0.0",
"description": "A long and descriptive description",
"termsOfService": "https://developer.entur.org/terms-of-service",
Expand All @@ -10,9 +10,9 @@
"url": "https://entur.no",
"email": "support@entur.no"
},
"x-entur-metadata": {
"owner": "team-Api",

Check failure on line 14 in specs/reference-spec-with-errors.json

View workflow job for this annotation

GitHub Actions / lint-reference-spec-with-errors

entur-info-metadata-owner-valid

"team-Api" must match the pattern "^team-[a-z0-9]+(-[a-z0-9]+)*$" Documentation: https://github.com/entur/api-guidelines/blob/main/guidelines.md#242-specification-owner
"audience": "public"

Check failure on line 15 in specs/reference-spec-with-errors.json

View workflow job for this annotation

GitHub Actions / lint-reference-spec-with-errors

entur-info-metadata-audience-valid

"public" must be equal to one of the allowed values: "open", "partner", "internal" Documentation: https://github.com/entur/api-guidelines/blob/main/guidelines.md#24-entur-metadata

Check failure on line 15 in specs/reference-spec-with-errors.json

View workflow job for this annotation

GitHub Actions / lint-reference-spec-with-errors

entur-info-metadata-id

The OpenAPI info section MUST include "x-entur-metadata.id". Documentation: https://github.com/entur/api-guidelines/blob/main/guidelines.md#241-identifying-a-specification
}
},

Expand Down Expand Up @@ -61,7 +61,7 @@
"properties": {
"id": {
"type": "string",
"example": 100

Check failure on line 64 in specs/reference-spec-with-errors.json

View workflow job for this annotation

GitHub Actions / lint-reference-spec-with-errors

oas3-valid-schema-example

"example" property type must be string Documentation: https://meta.stoplight.io/docs/spectral/docs/reference/openapi-rules.md#oas3-valid-schema-example
},
"name": {
"type": "string",
Expand All @@ -69,7 +69,7 @@
}
},
"example": {
"id": 100,

Check failure on line 72 in specs/reference-spec-with-errors.json

View workflow job for this annotation

GitHub Actions / lint-reference-spec-with-errors

oas3-valid-schema-example

"id" property type must be string Documentation: https://meta.stoplight.io/docs/spectral/docs/reference/openapi-rules.md#oas3-valid-schema-example
"name": "Item 100"
}
},
Expand Down Expand Up @@ -160,7 +160,8 @@
}
},
"x-entur-permissions": {
"value": "items:les"
"description": 10,

Check failure on line 163 in specs/reference-spec-with-errors.json

View workflow job for this annotation

GitHub Actions / lint-reference-spec-with-errors

entur-permissions

"value": {"and": ["items:les"]}

Check failure on line 164 in specs/reference-spec-with-errors.json

View workflow job for this annotation

GitHub Actions / lint-reference-spec-with-errors

entur-permissions

Comment thread
egrimstad marked this conversation as resolved.
}
},
"post": {
Expand Down Expand Up @@ -195,14 +196,15 @@
}
},
"x-entur-permissions": {
"bar": "something",

Check failure on line 199 in specs/reference-spec-with-errors.json

View workflow job for this annotation

GitHub Actions / lint-reference-spec-with-errors

entur-permissions

"description": "You need access to read organisations, and create items.",
"value": {
"all": [
"organisations:les",
"organisations:le",

Check failure on line 203 in specs/reference-spec-with-errors.json

View workflow job for this annotation

GitHub Actions / lint-reference-spec-with-errors

entur-permissions

{
"any": [
"items:opprett",
"items-global:opprett"
"items:oppret",

Check failure on line 206 in specs/reference-spec-with-errors.json

View workflow job for this annotation

GitHub Actions / lint-reference-spec-with-errors

entur-permissions

"items-global:oprett"
]
}
]
Expand All @@ -220,7 +222,7 @@
"tags": [
"Items"
],
"deprecated": true,

Check notice on line 225 in specs/reference-spec-with-errors.json

View workflow job for this annotation

GitHub Actions / lint-reference-spec-with-errors

entur-sunset-operation

"x-sunset" should be defined when "deprecated" is true Documentation: https://github.com/entur/api-guidelines/blob/main/guidelines.md#254-deprecation
"summary": "Get an item",
"description": "Get an item with given id.",
"operationId": "getItem",
Expand Down
2 changes: 1 addition & 1 deletion specs/reference-spec.json
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@
}
},
"x-entur-permissions": {
"value": "items:les"
"value": "items:LES"
}
},
"post": {
Expand Down