Skip to content

Commit

Permalink
Merge pull request #266 from stefanprodan/semver-validation
Browse files Browse the repository at this point in the history
Add semver validation to Timoni's CUE schemas
  • Loading branch information
stefanprodan authored Dec 3, 2023
2 parents b5541f1 + 30f1c76 commit 3aa7e45
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions schemas/timoni.sh/core/v1alpha1/semver.cue
Original file line number Diff line number Diff line change
@@ -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])
}

0 comments on commit 3aa7e45

Please sign in to comment.