예제 #1
0
        public void ShouldParse(StringValues versions, string[] strings)
        {
            var result = SemanticVersionParser.Parse(versions);

            result.DeclaredVersions.Select(x => x.SemanticVersion.ToString()).ShouldBe(new[] { "1.0", "2.0-alpha", "2.0", "3.0-beta" });
            result.DeclaredVersions.Select(x => x.String).ShouldBe(strings);
            result.ValidationErrorMessage.ShouldBe("Invalid api version, supported semantic versions: 1.0, 2.0, 3.0-beta.");
        }
        public bool Validate(ValidationContext validationContext)
        {
            var parserResult = SemanticVersionParser.Parse(validationContext.ValidVersions);

            if (string.IsNullOrEmpty(validationContext.Version))
            {
                if (validationContext.Optional)
                {
                    return(true);
                }

                validationContext.ErrorMessage = parserResult.ValidationErrorMessage;
                return(false);
            }

            if (!SemanticVersionParser.TryParse(validationContext.Version, out var semanticVersion))
            {
                validationContext.ErrorMessage = parserResult.ValidationErrorMessage;
                return(false);
            }

            foreach (var declaredVersion in parserResult.DeclaredVersions)
            {
                if (semanticVersion.Major != declaredVersion.SemanticVersion.Major)
                {
                    continue;
                }

                var comparison =
                    SemanticVersionComparer.Default.Compare(semanticVersion, declaredVersion.SemanticVersion);

                if (comparison > 0)
                {
                    continue;
                }

                validationContext.MatchedVersion = declaredVersion.String;
                return(true);
            }

            validationContext.ErrorMessage = parserResult.ValidationErrorMessage;
            return(false);
        }