Replies: 1 comment 1 reply
-
You can give this a try. I haven't tested it, but it should work. import { isPlainObject } from '@stoplight/json';
import { createRulesetFunction } from '@stoplight/spectral-core';
type Input = string;
type Options = {
value: unknown;
versionPath: unknown;
[key: string]: unknown;
};
function getVersion(data: unknown): string | null {
if (isPlainObject(data) && isPlainObject(data.info) && typeof data.info.version === 'string') {
return data.info.version;
}
return null;
}
export default createRulesetFunction<Input, Options>(
{
input: {
type: 'string',
},
options: {
type: 'object',
additionalProperties: true,
properties: {
value: true,
versionPath: true,
},
required: ['value', 'versionPath'],
},
},
function versionControl(targetVal, options, { document }) {
const { value } = options;
const { versionPath } = options;
const version = getVersion(document);
if (version === null) {
return [
{
message: 'Document does not contain valid version',
path: ['info', 'version']
}
]
}
// const test = new RegExp("/v[1-9][0-9]*/")
const versionPattern = /(v[1-9][0-9]*)/g;
const exist = versionPattern.exec(targetVal);
// const many = exist.length
if (targetVal !== value) {
return [
{
message: `Must include ${versionPath}.`,
},
];
}
return [];
},
); |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Describe the bug
it is hard to say its a bug, but i am trying to create a custom function that can compare the given value with another value is jsonpath. for example '$.info.version' of 'v2' has to match the first part of the paths.
instead me putting version number in rule, i want the rule to dynamically select the version to compare it with all the paths
i selected all the paths using give, but how can i grab '$.info.version' value in custom function?
{
"info": {
"version": "v2",
}
},
"paths": {
"/v2/testRequest": {}
}
}
customFunction.ts
Expected behavior
need spectral to check the versions with first part of given
Beta Was this translation helpful? Give feedback.
All reactions