-
-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #266 from stefanprodan/semver-validation
Add semver validation to Timoni's CUE schemas
- Loading branch information
Showing
1 changed file
with
29 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]) | ||
} |