You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
custom function to filter and get everything that starts with x-
use core pattern function to match the regex for x-myextension, match: '\b(x-myextension-[\w-]*)\b',
Additional context
I suspect my customFunction is not working as expected, since it doesn't filter anything, and I get error : Extension Name [openapi] is not prefixed with x-myextension.
Any suggestions what can be wrong with the below ?
CustomFunction(extensionMap.js)
import {
createRulesetFunction,
IFunctionResult} from "@stoplight/spectral-core";
function extensionMap(input: any): IFunctionResult[]{
let results= [];
let extension = input.toString();
// Check if the extension starts with 'x-'
if(extension.startsWith('x-')){
results.push(extension)
}
return results;
}
);
Rule:
import { pattern} from "@stoplight/spectral-functions";
import {DiagnosticSeverity} from "@stoplight/types";
import extensionMap from "./functions/extensionsMap";
export default {
rules: {
"extension-x-myextension": {
message: "Extension Name [{{property}}] is not prefixed with x-myextension.",
description:
"Extensions MUST start with x-myextension",
given:
'$..~',
then: [
{
function: extensionMap,
},
{
function: pattern,
functionOptions: {
match: '\b(x-myextension-[\w-])\b',
}
}
],
severity: DiagnosticSeverity.Error,
resolved: false
}
}
};
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Created a custom rule to do the below
Additional context
I suspect my customFunction is not working as expected, since it doesn't filter anything, and I get error : Extension Name [openapi] is not prefixed with x-myextension.
Any suggestions what can be wrong with the below ?
CustomFunction(extensionMap.js)
import {
createRulesetFunction,
IFunctionResult} from "@stoplight/spectral-core";
export default createRulesetFunction(
{
input: {
type: 'object',
},
options: null
},
function extensionMap(input: any): IFunctionResult[]{
let results= [];
let extension = input.toString();
// Check if the extension starts with 'x-'
if(extension.startsWith('x-')){
results.push(extension)
}
return results;
}
);
Rule:
import { pattern} from "@stoplight/spectral-functions";
import {DiagnosticSeverity} from "@stoplight/types";
import extensionMap from "./functions/extensionsMap";
export default {
rules: {
"extension-x-myextension": {
message: "Extension Name [{{property}}] is not prefixed with x-myextension.",
description:
"Extensions MUST start with x-myextension",
given:
'$..~',
then: [
{
function: extensionMap,
},
{
function: pattern,
functionOptions: {
match: '\b(x-myextension-[\w-])\b',
}
}
],
severity: DiagnosticSeverity.Error,
resolved: false
}
}
};
Beta Was this translation helpful? Give feedback.
All reactions