Skip to content

Are there options for not defaulting to semver #2809

@dillon-glovebox

Description

@dillon-glovebox

Currently using Stoplight as a part of Huma. I've run into a nuance where Elements defaults the API Version into a formatted semantic version string which is obviously a common use-case. However, from what I can find there doesn't seem to be an alternative solution for versions that use a commit-hash or anything other than semantic versioning.

I believe this is the snippet that performs the formatting here:

const enhanceVersionString = (version: string): string => {
  if (version[0] === 'v') return version;

  return `v${version}`;
};

If there's not a dedicated solution would it be a value add to RegEx check for a compatible semver and only format it IF there is not match?

// Matches semver + inclusion of initial 'v'
const semverReg = /^v?(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$/;

const enhanceVersionString = (version: string): string => {
  if (!semverReg.test(version)) {
    return version;
  }

  return version.startsWith('v') ? version : `v${version}`;
};

RegEx Source: From the Semver page directly. Included some testing samples here.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions