From 30f1c764f3d5ca3b9a9eb221b96a2490ed7d81f4 Mon Sep 17 00:00:00 2001 From: Stefan Prodan Date: Sun, 3 Dec 2023 19:29:43 +0200 Subject: [PATCH] Add semver validation to Timoni's CUE schemas Signed-off-by: Stefan Prodan --- schemas/timoni.sh/core/v1alpha1/semver.cue | 29 ++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 schemas/timoni.sh/core/v1alpha1/semver.cue diff --git a/schemas/timoni.sh/core/v1alpha1/semver.cue b/schemas/timoni.sh/core/v1alpha1/semver.cue new file mode 100644 index 00000000..ecd1e397 --- /dev/null +++ b/schemas/timoni.sh/core/v1alpha1/semver.cue @@ -0,0 +1,29 @@ +// Copyright 2023 Stefan Prodan +// SPDX-License-Identifier: Apache-2.0 + +package v1alpha1 + +import ( + "strconv" + "strings" +) + +// SemVer validates the input version string and extracts the major and minor version numbers. +// When Minimum is set, the major and minor parts must be greater or equal to the minimum +// or a validation error is returned. +#SemVer: { + // Input version string in strict semver format. + #Version!: string & =~"^\\d+\\.\\d+\\.\\d+(-[0-9A-Za-z-]+(\\.[0-9A-Za-z-]+)*)?(\\+[0-9A-Za-z-]+(\\.[0-9A-Za-z-]+)*)?$" + + // Minimum is the minimum allowed MAJOR.MINOR version. + #Minimum: *"0.0.0" | string & =~"^\\d+\\.\\d+\\.\\d+(-[0-9A-Za-z-]+(\\.[0-9A-Za-z-]+)*)?(\\+[0-9A-Za-z-]+(\\.[0-9A-Za-z-]+)*)?$" + + let minMajor = strconv.Atoi(strings.Split(#Minimum, ".")[0]) + let minMinor = strconv.Atoi(strings.Split(#Minimum, ".")[1]) + + major: int & >=minMajor + major: strconv.Atoi(strings.Split(#Version, ".")[0]) + + minor: int & >=minMinor + minor: strconv.Atoi(strings.Split(#Version, ".")[1]) +}