Skip to content

Commit 401072c

Browse files
committed
add a validation rule for the OAuthFlow object
1 parent 31a3cd9 commit 401072c

File tree

3 files changed

+89
-0
lines changed

3 files changed

+89
-0
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT license.
3+
4+
using System;
5+
using Microsoft.OpenApi.Models;
6+
using Microsoft.OpenApi.Properties;
7+
8+
namespace Microsoft.OpenApi.Validations.Rules
9+
{
10+
/// <summary>
11+
/// The validation rules for <see cref="OpenApiOAuthFlow"/>.
12+
/// </summary>
13+
internal static class OpenApiOAuthFlowRules
14+
{
15+
/// <summary>
16+
/// Validate the field is required.
17+
/// </summary>
18+
public static readonly ValidationRule<OpenApiOAuthFlow> FieldIsRequired =
19+
new ValidationRule<OpenApiOAuthFlow>(
20+
(context, flow) =>
21+
{
22+
// authorizationUrl
23+
context.Push("authorizationUrl");
24+
if (flow.AuthorizationUrl == null)
25+
{
26+
ValidationError error = new ValidationError(ErrorReason.Required, context.PathString,
27+
String.Format(SRResource.Validation_FieldIsRequired, "authorizationUrl", "OAuth Flow"));
28+
context.AddError(error);
29+
}
30+
context.Pop();
31+
32+
// tokenUrl
33+
context.Push("tokenUrl");
34+
if (flow.TokenUrl == null)
35+
{
36+
ValidationError error = new ValidationError(ErrorReason.Required, context.PathString,
37+
String.Format(SRResource.Validation_FieldIsRequired, "tokenUrl", "OAuth Flow"));
38+
context.AddError(error);
39+
}
40+
context.Pop();
41+
42+
// scopes
43+
context.Push("scopes");
44+
if (flow.Scopes == null)
45+
{
46+
ValidationError error = new ValidationError(ErrorReason.Required, context.PathString,
47+
String.Format(SRResource.Validation_FieldIsRequired, "scopes", "OAuth Flow"));
48+
context.AddError(error);
49+
}
50+
context.Pop();
51+
});
52+
53+
// add more rule.
54+
}
55+
}

src/Microsoft.OpenApi/Validations/ValidationRuleSet.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ public sealed class ValidationRuleSet : IEnumerable<ValidationRule>
4040

4141
OpenApiResponseRules.FieldIsRequired,
4242

43+
OpenApiOAuthFlowRules.FieldIsRequired,
44+
4345
OpenApiExtensibleRules.ExtensionNameMustStartWithXDash,
4446
// add more default rules.
4547
};
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT license.
3+
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using Microsoft.OpenApi.Models;
7+
using Microsoft.OpenApi.Validations;
8+
using Xunit;
9+
10+
namespace Microsoft.OpenApi.Tests.Models
11+
{
12+
public class OpenApiOAuthFlowValidationTests
13+
{
14+
[Fact]
15+
public void ValidateFixedFieldsIsRequiredInResponse()
16+
{
17+
// Arrange
18+
IEnumerable<ValidationError> errors;
19+
OpenApiOAuthFlow oAuthFlow = new OpenApiOAuthFlow();
20+
21+
// Act
22+
bool result = oAuthFlow.Validate(out errors);
23+
24+
// Assert
25+
Assert.False(result);
26+
Assert.NotNull(errors);
27+
Assert.Equal(2, errors.Count());
28+
Assert.Equal(new[] { "The field 'authorizationUrl' in 'OAuth Flow' object is REQUIRED.",
29+
"The field 'tokenUrl' in 'OAuth Flow' object is REQUIRED."}, errors.Select(e => e.ErrorMessage));
30+
}
31+
}
32+
}

0 commit comments

Comments
 (0)