diff --git a/CLAUDE.md b/CLAUDE.md index ccf0437d9..01e76f3b1 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -100,6 +100,15 @@ Favour duplicated, statically-defined generated methods over reflection-based co When working on the grammar/textual-notation generator (`SysML2.NET.CodeGenerator/HandleBarHelpers/RulesHelper.cs`), read `SysML2.NET.CodeGenerator/GRAMMAR.md` for the KEBNF model, cursor/builder conventions, and existing code-gen patterns. +### OpenAPI-based generation: the `PrimitiveConstraint[Request].value` rule + +The `OpenApi*Generator`s read `Resources/ptc-25-02-30.json` and emit the PSM DTOs, REST client, serializers and validators. One property in that document has no C# equivalent in its schema: `PrimitiveConstraint.value` and `PrimitiveConstraintRequest.value`. Each is `oneOf [array of oneOf(boolean | number | string | Identified | null), null]`, which `QueryCoreTypeName` cannot resolve. + +- **The specific rule:** `PropertyTypeOverrides` in `SysML2.NET.CodeGenerator/Generators/OpenApiHandleBarsGenerators/OpenApiDtoGenerator.cs` maps both to `List`. `ConstraintValue` is the hand-written tagged union `SysML2.NET/PSM/DTO/ConstraintValue.cs`. +- **The DTO generator is the single owner of that mapping.** Do not copy the override into another generator, and do not expose it for reuse. A downstream generator relies on the generated DTO already carrying `List Value`. +- **Derive downstream rules from the schema keywords, not from the C# type.** For example, the request validator walks `schema.Properties` (minus `@type`) rather than `QueryMappableProperties`. `QueryMappableProperties` drops `value` because it is unmappable, and that would silently lose its `minItems: 1`. +- **A property the DTO lacks surfaces as a compile error** in the generated code, never as a skipped rule. + ### Textual notation reviewer is MANDATORY **Every change touching these paths MUST be verified by the `textual-notation-reviewer` agent (`.claude/agents/textual-notation-reviewer.md`) before being reported complete or committed:** @@ -132,6 +141,7 @@ SysML2.NET.Serializer.TextualNotation - Writers/, AutoGenTextualNotationBuilder/ SysML2.NET.Dal - Data Access Layer (Assembler, ElementFactory) SysML2.NET.REST - REST client + Session SysML2.NET.Kpar - Reader/Writer for .kpar archives +SysML2.NET.Validation - FluentValidation validators for the PSM request DTOs (PSM/AutoGenValidator/, generated) SysML2.NET.Viewer - Blazor WebAssembly app (net9.0) SysML2.NET.CodeGenerator - Code generation tool (net10.0, not packaged) ``` diff --git a/Directory.Packages.props b/Directory.Packages.props index c4ddc91ed..904edf022 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -29,6 +29,7 @@ + diff --git a/README.md b/README.md index dd6987195..3d97d4fb3 100644 --- a/README.md +++ b/README.md @@ -31,6 +31,7 @@ project [SysML2.NET.Kpar](https://www.nuget.org/packages/SysML2.NET.Kpar) | ![NuGet Version](https://img.shields.io/nuget/v/SysML2.NET.Kpar) [SysML2.NET.REST](https://www.nuget.org/packages/SysML2.NET.REST) | ![NuGet Version](https://img.shields.io/nuget/v/SysML2.NET.REST) [SysML2.NET.DAL](https://www.nuget.org/packages/SysML2.NET.DAL) | ![NuGet Version](https://img.shields.io/nuget/v/SysML2.NET.DAL) +[SysML2.NET.Validation](https://www.nuget.org/packages/SysML2.NET.Validation) | ![NuGet Version](https://img.shields.io/nuget/v/SysML2.NET.Validation) The reference web-application is available on [docker-hub](https://hub.docker.com/r/stariongroup/sysml2.net.viewer). A demo-version is hosted at http://viewer.sysml2.net. @@ -53,7 +54,7 @@ Every NuGet version is declared once, in [Directory.Packages.props](Directory.Pa The redistributable libraries multi-target `netstandard2.1;net10.0`, so a referenced version is a **minimum floor** for every downstream consumer. The version policy follows from that: -- **Libraries** (`SysML2.NET`, `SysML2.NET.Serializer.*`, `SysML2.NET.Dal`, `SysML2.NET.REST`, `SysML2.NET.Kpar`, `SysML2.NET.Extensions`, `SysML2.NET.Semantics`) reference the lowest version that provides the APIs used. Stable framework abstractions stay at an LTS floor; serialization and cryptography packages track current. +- **Libraries** (`SysML2.NET`, `SysML2.NET.Serializer.*`, `SysML2.NET.Dal`, `SysML2.NET.REST`, `SysML2.NET.Kpar`, `SysML2.NET.Extensions`, `SysML2.NET.Semantics`, `SysML2.NET.Validation`) reference the lowest version that provides the APIs used. Stable framework abstractions stay at an LTS floor; serialization and cryptography packages track current. - **Applications and test projects** track current versions. - A library dependency is raised only when a required new API needs it, or when a CVE affects that package. diff --git a/SysML2.NET.CodeGenerator.Tests/Expected/OpenAPI/Validation/AutoGenValidator/BranchRequestValidator.cs b/SysML2.NET.CodeGenerator.Tests/Expected/OpenAPI/Validation/AutoGenValidator/BranchRequestValidator.cs new file mode 100644 index 000000000..77d93cc06 --- /dev/null +++ b/SysML2.NET.CodeGenerator.Tests/Expected/OpenAPI/Validation/AutoGenValidator/BranchRequestValidator.cs @@ -0,0 +1,44 @@ +// ------------------------------------------------------------------------------------------------ +// +// +// Copyright (C) 2022-2026 Starion Group S.A. +// SPDX-License-Identifier: Apache-2.0 +// +// +// ------------------------------------------------------------------------------------------------ + +// ------------------------------------------------------------------------------------------------ +// +// --------THIS IS AN AUTOMATICALLY GENERATED FILE. ANY MANUAL CHANGES WILL BE OVERWRITTEN!-------- +// ------------------------------------------------------------------------------------------------ + +namespace SysML2.NET.Validation.PSM +{ + using System; + using System.CodeDom.Compiler; + + using FluentValidation; + + using SysML2.NET.PSM.DTO; + + /// + /// Validates a against the BranchRequest schema of the Systems Modeling API and Services + /// + [GeneratedCode("SysML2.NET", "latest")] + public sealed partial class BranchRequestValidator : RequestValidator + { + /// + /// Initializes a new instance of the class + /// and registers its generated rules + /// + public BranchRequestValidator() + { + this.RuleFor(dto => dto.Head).Must(value => !value.HasValue || value.Value != Guid.Empty); + this.RuleFor(dto => dto.Name).NotEmpty(); + } + } +} + +// ------------------------------------------------------------------------------------------------ +// --------THIS IS AN AUTOMATICALLY GENERATED FILE. ANY MANUAL CHANGES WILL BE OVERWRITTEN!-------- +// ------------------------------------------------------------------------------------------------ diff --git a/SysML2.NET.CodeGenerator.Tests/Expected/OpenAPI/Validation/AutoGenValidator/CommitRequestValidator.cs b/SysML2.NET.CodeGenerator.Tests/Expected/OpenAPI/Validation/AutoGenValidator/CommitRequestValidator.cs new file mode 100644 index 000000000..4689e23a3 --- /dev/null +++ b/SysML2.NET.CodeGenerator.Tests/Expected/OpenAPI/Validation/AutoGenValidator/CommitRequestValidator.cs @@ -0,0 +1,44 @@ +// ------------------------------------------------------------------------------------------------ +// +// +// Copyright (C) 2022-2026 Starion Group S.A. +// SPDX-License-Identifier: Apache-2.0 +// +// +// ------------------------------------------------------------------------------------------------ + +// ------------------------------------------------------------------------------------------------ +// +// --------THIS IS AN AUTOMATICALLY GENERATED FILE. ANY MANUAL CHANGES WILL BE OVERWRITTEN!-------- +// ------------------------------------------------------------------------------------------------ + +namespace SysML2.NET.Validation.PSM +{ + using System; + using System.CodeDom.Compiler; + + using FluentValidation; + + using SysML2.NET.PSM.DTO; + + /// + /// Validates a against the CommitRequest schema of the Systems Modeling API and Services + /// + [GeneratedCode("SysML2.NET", "latest")] + public sealed partial class CommitRequestValidator : RequestValidator + { + /// + /// Initializes a new instance of the class + /// and registers its generated rules + /// + public CommitRequestValidator() + { + this.RuleFor(dto => dto.Change).NotNull(); + this.RuleForEach(dto => dto.Change).NotNull().SetValidator(new DataVersionRequestValidator()); + } + } +} + +// ------------------------------------------------------------------------------------------------ +// --------THIS IS AN AUTOMATICALLY GENERATED FILE. ANY MANUAL CHANGES WILL BE OVERWRITTEN!-------- +// ------------------------------------------------------------------------------------------------ diff --git a/SysML2.NET.CodeGenerator.Tests/Expected/OpenAPI/Validation/AutoGenValidator/CompositeConstraintRequestValidator.cs b/SysML2.NET.CodeGenerator.Tests/Expected/OpenAPI/Validation/AutoGenValidator/CompositeConstraintRequestValidator.cs new file mode 100644 index 000000000..a671c6e60 --- /dev/null +++ b/SysML2.NET.CodeGenerator.Tests/Expected/OpenAPI/Validation/AutoGenValidator/CompositeConstraintRequestValidator.cs @@ -0,0 +1,46 @@ +// ------------------------------------------------------------------------------------------------ +// +// +// Copyright (C) 2022-2026 Starion Group S.A. +// SPDX-License-Identifier: Apache-2.0 +// +// +// ------------------------------------------------------------------------------------------------ + +// ------------------------------------------------------------------------------------------------ +// +// --------THIS IS AN AUTOMATICALLY GENERATED FILE. ANY MANUAL CHANGES WILL BE OVERWRITTEN!-------- +// ------------------------------------------------------------------------------------------------ + +namespace SysML2.NET.Validation.PSM +{ + using System; + using System.CodeDom.Compiler; + + using FluentValidation; + + using SysML2.NET.PSM.DTO; + + /// + /// Validates a against the CompositeConstraintRequest schema of the Systems Modeling API and Services + /// + [GeneratedCode("SysML2.NET", "latest")] + public sealed partial class CompositeConstraintRequestValidator : RequestValidator + { + /// + /// Initializes a new instance of the class + /// and registers its generated rules + /// + public CompositeConstraintRequestValidator() + { + this.RuleFor(dto => dto.Constraint).NotNull(); + this.RuleFor(dto => dto.Constraint).Must(value => value == null || value.Count >= 2); + this.RuleForEach(dto => dto.Constraint).NotNull(); + this.RuleFor(dto => dto.Operator).IsInEnum(); + } + } +} + +// ------------------------------------------------------------------------------------------------ +// --------THIS IS AN AUTOMATICALLY GENERATED FILE. ANY MANUAL CHANGES WILL BE OVERWRITTEN!-------- +// ------------------------------------------------------------------------------------------------ diff --git a/SysML2.NET.CodeGenerator.Tests/Expected/OpenAPI/Validation/AutoGenValidator/DataIdentityRequestValidator.cs b/SysML2.NET.CodeGenerator.Tests/Expected/OpenAPI/Validation/AutoGenValidator/DataIdentityRequestValidator.cs new file mode 100644 index 000000000..13a60012a --- /dev/null +++ b/SysML2.NET.CodeGenerator.Tests/Expected/OpenAPI/Validation/AutoGenValidator/DataIdentityRequestValidator.cs @@ -0,0 +1,43 @@ +// ------------------------------------------------------------------------------------------------ +// +// +// Copyright (C) 2022-2026 Starion Group S.A. +// SPDX-License-Identifier: Apache-2.0 +// +// +// ------------------------------------------------------------------------------------------------ + +// ------------------------------------------------------------------------------------------------ +// +// --------THIS IS AN AUTOMATICALLY GENERATED FILE. ANY MANUAL CHANGES WILL BE OVERWRITTEN!-------- +// ------------------------------------------------------------------------------------------------ + +namespace SysML2.NET.Validation.PSM +{ + using System; + using System.CodeDom.Compiler; + + using FluentValidation; + + using SysML2.NET.PSM.DTO; + + /// + /// Validates a against the DataIdentityRequest schema of the Systems Modeling API and Services + /// + [GeneratedCode("SysML2.NET", "latest")] + public sealed partial class DataIdentityRequestValidator : RequestValidator + { + /// + /// Initializes a new instance of the class + /// and registers its generated rules + /// + public DataIdentityRequestValidator() + { + this.RuleFor(dto => dto.Id).NotEmpty(); + } + } +} + +// ------------------------------------------------------------------------------------------------ +// --------THIS IS AN AUTOMATICALLY GENERATED FILE. ANY MANUAL CHANGES WILL BE OVERWRITTEN!-------- +// ------------------------------------------------------------------------------------------------ diff --git a/SysML2.NET.CodeGenerator.Tests/Expected/OpenAPI/Validation/AutoGenValidator/DataVersionRequestValidator.cs b/SysML2.NET.CodeGenerator.Tests/Expected/OpenAPI/Validation/AutoGenValidator/DataVersionRequestValidator.cs new file mode 100644 index 000000000..88e54e3aa --- /dev/null +++ b/SysML2.NET.CodeGenerator.Tests/Expected/OpenAPI/Validation/AutoGenValidator/DataVersionRequestValidator.cs @@ -0,0 +1,49 @@ +// ------------------------------------------------------------------------------------------------ +// +// +// Copyright (C) 2022-2026 Starion Group S.A. +// SPDX-License-Identifier: Apache-2.0 +// +// +// ------------------------------------------------------------------------------------------------ + +// ------------------------------------------------------------------------------------------------ +// +// --------THIS IS AN AUTOMATICALLY GENERATED FILE. ANY MANUAL CHANGES WILL BE OVERWRITTEN!-------- +// ------------------------------------------------------------------------------------------------ + +namespace SysML2.NET.Validation.PSM +{ + using System; + using System.CodeDom.Compiler; + + using FluentValidation; + + using SysML2.NET.PSM.DTO; + + /// + /// Validates a against the DataVersionRequest schema of the Systems Modeling API and Services + /// + [GeneratedCode("SysML2.NET", "latest")] + public sealed partial class DataVersionRequestValidator : RequestValidator + { + /// + /// Initializes a new instance of the class + /// and registers its generated rules + /// + public DataVersionRequestValidator() + { + this.RuleFor(dto => dto.Identity).SetValidator(new DataIdentityRequestValidator()); + this.RuleFor(dto => dto.Payload).SetInheritanceValidator(validators => + { + validators.Add(new ExternalDataRequestValidator()); + validators.Add(new ExternalRelationshipRequestValidator()); + validators.Add(new ProjectUsageRequestValidator()); + }); + } + } +} + +// ------------------------------------------------------------------------------------------------ +// --------THIS IS AN AUTOMATICALLY GENERATED FILE. ANY MANUAL CHANGES WILL BE OVERWRITTEN!-------- +// ------------------------------------------------------------------------------------------------ diff --git a/SysML2.NET.CodeGenerator.Tests/Expected/OpenAPI/Validation/AutoGenValidator/ExternalDataRequestValidator.cs b/SysML2.NET.CodeGenerator.Tests/Expected/OpenAPI/Validation/AutoGenValidator/ExternalDataRequestValidator.cs new file mode 100644 index 000000000..79a431d20 --- /dev/null +++ b/SysML2.NET.CodeGenerator.Tests/Expected/OpenAPI/Validation/AutoGenValidator/ExternalDataRequestValidator.cs @@ -0,0 +1,44 @@ +// ------------------------------------------------------------------------------------------------ +// +// +// Copyright (C) 2022-2026 Starion Group S.A. +// SPDX-License-Identifier: Apache-2.0 +// +// +// ------------------------------------------------------------------------------------------------ + +// ------------------------------------------------------------------------------------------------ +// +// --------THIS IS AN AUTOMATICALLY GENERATED FILE. ANY MANUAL CHANGES WILL BE OVERWRITTEN!-------- +// ------------------------------------------------------------------------------------------------ + +namespace SysML2.NET.Validation.PSM +{ + using System; + using System.CodeDom.Compiler; + + using FluentValidation; + + using SysML2.NET.PSM.DTO; + + /// + /// Validates a against the ExternalDataRequest schema of the Systems Modeling API and Services + /// + [GeneratedCode("SysML2.NET", "latest")] + public sealed partial class ExternalDataRequestValidator : RequestValidator + { + /// + /// Initializes a new instance of the class + /// and registers its generated rules + /// + public ExternalDataRequestValidator() + { + this.RuleFor(dto => dto.Id).Must(value => !value.HasValue || value.Value != Guid.Empty); + this.RuleFor(dto => dto.ResourceIdentifier).Must(value => value == null || value.IsAbsoluteUri); + } + } +} + +// ------------------------------------------------------------------------------------------------ +// --------THIS IS AN AUTOMATICALLY GENERATED FILE. ANY MANUAL CHANGES WILL BE OVERWRITTEN!-------- +// ------------------------------------------------------------------------------------------------ diff --git a/SysML2.NET.CodeGenerator.Tests/Expected/OpenAPI/Validation/AutoGenValidator/ExternalRelationshipRequestValidator.cs b/SysML2.NET.CodeGenerator.Tests/Expected/OpenAPI/Validation/AutoGenValidator/ExternalRelationshipRequestValidator.cs new file mode 100644 index 000000000..215f5c9a2 --- /dev/null +++ b/SysML2.NET.CodeGenerator.Tests/Expected/OpenAPI/Validation/AutoGenValidator/ExternalRelationshipRequestValidator.cs @@ -0,0 +1,45 @@ +// ------------------------------------------------------------------------------------------------ +// +// +// Copyright (C) 2022-2026 Starion Group S.A. +// SPDX-License-Identifier: Apache-2.0 +// +// +// ------------------------------------------------------------------------------------------------ + +// ------------------------------------------------------------------------------------------------ +// +// --------THIS IS AN AUTOMATICALLY GENERATED FILE. ANY MANUAL CHANGES WILL BE OVERWRITTEN!-------- +// ------------------------------------------------------------------------------------------------ + +namespace SysML2.NET.Validation.PSM +{ + using System; + using System.CodeDom.Compiler; + + using FluentValidation; + + using SysML2.NET.PSM.DTO; + + /// + /// Validates a against the ExternalRelationshipRequest schema of the Systems Modeling API and Services + /// + [GeneratedCode("SysML2.NET", "latest")] + public sealed partial class ExternalRelationshipRequestValidator : RequestValidator + { + /// + /// Initializes a new instance of the class + /// and registers its generated rules + /// + public ExternalRelationshipRequestValidator() + { + this.RuleFor(dto => dto.Id).Must(value => !value.HasValue || value.Value != Guid.Empty); + this.RuleFor(dto => dto.ElementEnd).Must(value => !value.HasValue || value.Value != Guid.Empty); + this.RuleFor(dto => dto.ExternalDataEnd).Must(value => !value.HasValue || value.Value != Guid.Empty); + } + } +} + +// ------------------------------------------------------------------------------------------------ +// --------THIS IS AN AUTOMATICALLY GENERATED FILE. ANY MANUAL CHANGES WILL BE OVERWRITTEN!-------- +// ------------------------------------------------------------------------------------------------ diff --git a/SysML2.NET.CodeGenerator.Tests/Expected/OpenAPI/Validation/AutoGenValidator/PrimitiveConstraintRequestValidator.cs b/SysML2.NET.CodeGenerator.Tests/Expected/OpenAPI/Validation/AutoGenValidator/PrimitiveConstraintRequestValidator.cs new file mode 100644 index 000000000..50b47a8f8 --- /dev/null +++ b/SysML2.NET.CodeGenerator.Tests/Expected/OpenAPI/Validation/AutoGenValidator/PrimitiveConstraintRequestValidator.cs @@ -0,0 +1,45 @@ +// ------------------------------------------------------------------------------------------------ +// +// +// Copyright (C) 2022-2026 Starion Group S.A. +// SPDX-License-Identifier: Apache-2.0 +// +// +// ------------------------------------------------------------------------------------------------ + +// ------------------------------------------------------------------------------------------------ +// +// --------THIS IS AN AUTOMATICALLY GENERATED FILE. ANY MANUAL CHANGES WILL BE OVERWRITTEN!-------- +// ------------------------------------------------------------------------------------------------ + +namespace SysML2.NET.Validation.PSM +{ + using System; + using System.CodeDom.Compiler; + + using FluentValidation; + + using SysML2.NET.PSM.DTO; + + /// + /// Validates a against the PrimitiveConstraintRequest schema of the Systems Modeling API and Services + /// + [GeneratedCode("SysML2.NET", "latest")] + public sealed partial class PrimitiveConstraintRequestValidator : RequestValidator + { + /// + /// Initializes a new instance of the class + /// and registers its generated rules + /// + public PrimitiveConstraintRequestValidator() + { + this.RuleFor(dto => dto.Operator).IsInEnum(); + this.RuleFor(dto => dto.Property).NotEmpty(); + this.RuleFor(dto => dto.Value).Must(value => value == null || value.Count >= 1); + } + } +} + +// ------------------------------------------------------------------------------------------------ +// --------THIS IS AN AUTOMATICALLY GENERATED FILE. ANY MANUAL CHANGES WILL BE OVERWRITTEN!-------- +// ------------------------------------------------------------------------------------------------ diff --git a/SysML2.NET.CodeGenerator.Tests/Expected/OpenAPI/Validation/AutoGenValidator/ProjectRequestValidator.cs b/SysML2.NET.CodeGenerator.Tests/Expected/OpenAPI/Validation/AutoGenValidator/ProjectRequestValidator.cs new file mode 100644 index 000000000..edfcafaed --- /dev/null +++ b/SysML2.NET.CodeGenerator.Tests/Expected/OpenAPI/Validation/AutoGenValidator/ProjectRequestValidator.cs @@ -0,0 +1,44 @@ +// ------------------------------------------------------------------------------------------------ +// +// +// Copyright (C) 2022-2026 Starion Group S.A. +// SPDX-License-Identifier: Apache-2.0 +// +// +// ------------------------------------------------------------------------------------------------ + +// ------------------------------------------------------------------------------------------------ +// +// --------THIS IS AN AUTOMATICALLY GENERATED FILE. ANY MANUAL CHANGES WILL BE OVERWRITTEN!-------- +// ------------------------------------------------------------------------------------------------ + +namespace SysML2.NET.Validation.PSM +{ + using System; + using System.CodeDom.Compiler; + + using FluentValidation; + + using SysML2.NET.PSM.DTO; + + /// + /// Validates a against the ProjectRequest schema of the Systems Modeling API and Services + /// + [GeneratedCode("SysML2.NET", "latest")] + public sealed partial class ProjectRequestValidator : RequestValidator + { + /// + /// Initializes a new instance of the class + /// and registers its generated rules + /// + public ProjectRequestValidator() + { + this.RuleFor(dto => dto.DefaultBranch).Must(value => !value.HasValue || value.Value != Guid.Empty); + this.RuleFor(dto => dto.Name).NotEmpty(); + } + } +} + +// ------------------------------------------------------------------------------------------------ +// --------THIS IS AN AUTOMATICALLY GENERATED FILE. ANY MANUAL CHANGES WILL BE OVERWRITTEN!-------- +// ------------------------------------------------------------------------------------------------ diff --git a/SysML2.NET.CodeGenerator.Tests/Expected/OpenAPI/Validation/AutoGenValidator/ProjectUsageRequestValidator.cs b/SysML2.NET.CodeGenerator.Tests/Expected/OpenAPI/Validation/AutoGenValidator/ProjectUsageRequestValidator.cs new file mode 100644 index 000000000..a60be067d --- /dev/null +++ b/SysML2.NET.CodeGenerator.Tests/Expected/OpenAPI/Validation/AutoGenValidator/ProjectUsageRequestValidator.cs @@ -0,0 +1,45 @@ +// ------------------------------------------------------------------------------------------------ +// +// +// Copyright (C) 2022-2026 Starion Group S.A. +// SPDX-License-Identifier: Apache-2.0 +// +// +// ------------------------------------------------------------------------------------------------ + +// ------------------------------------------------------------------------------------------------ +// +// --------THIS IS AN AUTOMATICALLY GENERATED FILE. ANY MANUAL CHANGES WILL BE OVERWRITTEN!-------- +// ------------------------------------------------------------------------------------------------ + +namespace SysML2.NET.Validation.PSM +{ + using System; + using System.CodeDom.Compiler; + + using FluentValidation; + + using SysML2.NET.PSM.DTO; + + /// + /// Validates a against the ProjectUsageRequest schema of the Systems Modeling API and Services + /// + [GeneratedCode("SysML2.NET", "latest")] + public sealed partial class ProjectUsageRequestValidator : RequestValidator + { + /// + /// Initializes a new instance of the class + /// and registers its generated rules + /// + public ProjectUsageRequestValidator() + { + this.RuleFor(dto => dto.Id).Must(value => !value.HasValue || value.Value != Guid.Empty); + this.RuleFor(dto => dto.UsedCommit).Must(value => !value.HasValue || value.Value != Guid.Empty); + this.RuleFor(dto => dto.UsedProject).Must(value => !value.HasValue || value.Value != Guid.Empty); + } + } +} + +// ------------------------------------------------------------------------------------------------ +// --------THIS IS AN AUTOMATICALLY GENERATED FILE. ANY MANUAL CHANGES WILL BE OVERWRITTEN!-------- +// ------------------------------------------------------------------------------------------------ diff --git a/SysML2.NET.CodeGenerator.Tests/Expected/OpenAPI/Validation/AutoGenValidator/QueryRequestValidator.cs b/SysML2.NET.CodeGenerator.Tests/Expected/OpenAPI/Validation/AutoGenValidator/QueryRequestValidator.cs new file mode 100644 index 000000000..0eb30cba9 --- /dev/null +++ b/SysML2.NET.CodeGenerator.Tests/Expected/OpenAPI/Validation/AutoGenValidator/QueryRequestValidator.cs @@ -0,0 +1,48 @@ +// ------------------------------------------------------------------------------------------------ +// +// +// Copyright (C) 2022-2026 Starion Group S.A. +// SPDX-License-Identifier: Apache-2.0 +// +// +// ------------------------------------------------------------------------------------------------ + +// ------------------------------------------------------------------------------------------------ +// +// --------THIS IS AN AUTOMATICALLY GENERATED FILE. ANY MANUAL CHANGES WILL BE OVERWRITTEN!-------- +// ------------------------------------------------------------------------------------------------ + +namespace SysML2.NET.Validation.PSM +{ + using System; + using System.CodeDom.Compiler; + + using FluentValidation; + + using SysML2.NET.PSM.DTO; + + /// + /// Validates a against the QueryRequest schema of the Systems Modeling API and Services + /// + [GeneratedCode("SysML2.NET", "latest")] + public sealed partial class QueryRequestValidator : RequestValidator + { + /// + /// Initializes a new instance of the class + /// and registers its generated rules + /// + public QueryRequestValidator() + { + this.RuleFor(dto => dto.Name).NotEmpty(); + this.RuleFor(dto => dto.Where).SetInheritanceValidator(validators => + { + validators.Add(new CompositeConstraintRequestValidator()); + validators.Add(new PrimitiveConstraintRequestValidator()); + }); + } + } +} + +// ------------------------------------------------------------------------------------------------ +// --------THIS IS AN AUTOMATICALLY GENERATED FILE. ANY MANUAL CHANGES WILL BE OVERWRITTEN!-------- +// ------------------------------------------------------------------------------------------------ diff --git a/SysML2.NET.CodeGenerator.Tests/Expected/OpenAPI/Validation/AutoGenValidator/TagRequestValidator.cs b/SysML2.NET.CodeGenerator.Tests/Expected/OpenAPI/Validation/AutoGenValidator/TagRequestValidator.cs new file mode 100644 index 000000000..22d201a3e --- /dev/null +++ b/SysML2.NET.CodeGenerator.Tests/Expected/OpenAPI/Validation/AutoGenValidator/TagRequestValidator.cs @@ -0,0 +1,44 @@ +// ------------------------------------------------------------------------------------------------ +// +// +// Copyright (C) 2022-2026 Starion Group S.A. +// SPDX-License-Identifier: Apache-2.0 +// +// +// ------------------------------------------------------------------------------------------------ + +// ------------------------------------------------------------------------------------------------ +// +// --------THIS IS AN AUTOMATICALLY GENERATED FILE. ANY MANUAL CHANGES WILL BE OVERWRITTEN!-------- +// ------------------------------------------------------------------------------------------------ + +namespace SysML2.NET.Validation.PSM +{ + using System; + using System.CodeDom.Compiler; + + using FluentValidation; + + using SysML2.NET.PSM.DTO; + + /// + /// Validates a against the TagRequest schema of the Systems Modeling API and Services + /// + [GeneratedCode("SysML2.NET", "latest")] + public sealed partial class TagRequestValidator : RequestValidator + { + /// + /// Initializes a new instance of the class + /// and registers its generated rules + /// + public TagRequestValidator() + { + this.RuleFor(dto => dto.Name).NotEmpty(); + this.RuleFor(dto => dto.TaggedCommit).NotEmpty(); + } + } +} + +// ------------------------------------------------------------------------------------------------ +// --------THIS IS AN AUTOMATICALLY GENERATED FILE. ANY MANUAL CHANGES WILL BE OVERWRITTEN!-------- +// ------------------------------------------------------------------------------------------------ diff --git a/SysML2.NET.CodeGenerator.Tests/Extensions/OpenApiSchemaExtensionsTestFixture.cs b/SysML2.NET.CodeGenerator.Tests/Extensions/OpenApiSchemaExtensionsTestFixture.cs index cd6eab0bd..7f430ab04 100644 --- a/SysML2.NET.CodeGenerator.Tests/Extensions/OpenApiSchemaExtensionsTestFixture.cs +++ b/SysML2.NET.CodeGenerator.Tests/Extensions/OpenApiSchemaExtensionsTestFixture.cs @@ -133,6 +133,7 @@ public void VerifyMultiplicityAndNullability() Assert.That(() => ((IOpenApiSchema)null).QueryIsNullable(true), Throws.TypeOf()); Assert.That(() => ((IOpenApiSchema)null).QueryAdmitsNull(), Throws.TypeOf()); Assert.That(() => ((IOpenApiSchema)null).QueryIsValueType(), Throws.TypeOf()); + Assert.That(() => ((IOpenApiSchema)null).QueryMinItems(), Throws.TypeOf()); using (Assert.EnterMultipleScope()) { @@ -160,6 +161,11 @@ public void VerifyMultiplicityAndNullability() Assert.That(this.schemas["ExternalData"].Properties["resourceIdentifier"].QueryIsValueType(), Is.False); Assert.That(this.schemas["Project"].Properties["alias"].QueryIsValueType(), Is.False); Assert.That(this.schemas["DataVersion"].Properties["payload"].QueryIsValueType(), Is.False); + + Assert.That(this.schemas["CompositeConstraintRequest"].Properties["constraint"].QueryMinItems(), Is.EqualTo(2)); + Assert.That(this.schemas["PrimitiveConstraintRequest"].Properties["value"].QueryMinItems(), Is.EqualTo(1)); + Assert.That(this.schemas["CommitRequest"].Properties["change"].QueryMinItems(), Is.Null); + Assert.That(this.schemas["ProjectRequest"].Properties["name"].QueryMinItems(), Is.Null); } } diff --git a/SysML2.NET.CodeGenerator.Tests/Generators/OpenApiHandleBarsGenerators/OpenApiRequestValidatorGeneratorTestFixture.cs b/SysML2.NET.CodeGenerator.Tests/Generators/OpenApiHandleBarsGenerators/OpenApiRequestValidatorGeneratorTestFixture.cs new file mode 100644 index 000000000..8e6243d9c --- /dev/null +++ b/SysML2.NET.CodeGenerator.Tests/Generators/OpenApiHandleBarsGenerators/OpenApiRequestValidatorGeneratorTestFixture.cs @@ -0,0 +1,93 @@ +// ------------------------------------------------------------------------------------------------ +// +// +// Copyright (C) 2022-2026 Starion Group S.A. +// SPDX-License-Identifier: Apache-2.0 +// +// +// ------------------------------------------------------------------------------------------------ + +namespace SysML2.NET.CodeGenerator.Tests.Generators.OpenApiHandleBarsGenerators +{ + using System; + using System.Collections.Generic; + using System.IO; + using System.Linq; + using System.Threading.Tasks; + + using Microsoft.OpenApi; + + using NUnit.Framework; + + using SysML2.NET.CodeGenerator.Generators.OpenApiHandleBarsGenerators; + + [TestFixture] + public class OpenApiRequestValidatorGeneratorTestFixture + { + private OpenApiDocument openApiDocument; + private DirectoryInfo outputDirectory; + private OpenApiRequestValidatorGenerator openApiRequestValidatorGenerator; + + [OneTimeSetUp] + public async Task SetUp() + { + this.openApiDocument = await OpenApiDocumentLoader.LoadAsync(); + + var directoryInfo = new DirectoryInfo(TestContext.CurrentContext.TestDirectory); + + this.outputDirectory = directoryInfo.CreateSubdirectory(Path.Combine("OpenAPI", "_SysML2.NET.Validation.PSM")); + this.openApiRequestValidatorGenerator = new OpenApiRequestValidatorGenerator(); + } + + private static IEnumerable ValidatedSchemas() + { + var openApiDocument = OpenApiDocumentLoader.LoadAsync().GetAwaiter().GetResult(); + + return OpenApiRequestValidatorGenerator.QueryValidatedSchemas(openApiDocument).Select(schema => schema.Key); + } + + [Test] + public async Task VerifyGenerateAsync() + { + await Assert.ThatAsync(() => this.openApiRequestValidatorGenerator.GenerateAsync(null, this.outputDirectory), Throws.TypeOf()); + await Assert.ThatAsync(() => this.openApiRequestValidatorGenerator.GenerateAsync(this.openApiDocument, null), Throws.TypeOf()); + + await Assert.ThatAsync(() => this.openApiRequestValidatorGenerator.GenerateAsync(this.openApiDocument, this.outputDirectory), Throws.Nothing); + } + + [Test] + public void VerifyQueryValidatedSchemas() + { + Assert.That(() => OpenApiRequestValidatorGenerator.QueryValidatedSchemas(null), Throws.TypeOf()); + + var schemaNames = OpenApiRequestValidatorGenerator.QueryValidatedSchemas(this.openApiDocument).Select(schema => schema.Key).ToList(); + + using (Assert.EnterMultipleScope()) + { + Assert.That(schemaNames, Has.Count.EqualTo(12)); + Assert.That(schemaNames, Does.Contain("ProjectRequest")); + Assert.That(schemaNames, Does.Not.Contain("ConstraintRequest")); + Assert.That(schemaNames, Does.Not.Contain("DataRequest")); + Assert.That(schemaNames, Does.Not.Contain("Project")); + Assert.That(schemaNames, Does.Not.Contain("PartUsageRequest")); + } + } + + [Test] + [TestCaseSource(nameof(ValidatedSchemas))] + [Category("Expected")] + public async Task VerifyGenerateRequestValidatorAsync(string schemaName) + { + await Assert.ThatAsync(() => this.openApiRequestValidatorGenerator.GenerateRequestValidatorAsync(null, this.outputDirectory, schemaName), Throws.TypeOf()); + await Assert.ThatAsync(() => this.openApiRequestValidatorGenerator.GenerateRequestValidatorAsync(this.openApiDocument, null, schemaName), Throws.TypeOf()); + await Assert.ThatAsync(() => this.openApiRequestValidatorGenerator.GenerateRequestValidatorAsync(this.openApiDocument, this.outputDirectory, " "), Throws.TypeOf()); + + var generatedCode = await this.openApiRequestValidatorGenerator.GenerateRequestValidatorAsync(this.openApiDocument, this.outputDirectory, schemaName); + + var expected = await File.ReadAllTextAsync(Path.Combine(TestContext.CurrentContext.TestDirectory, + $"Expected/OpenAPI/Validation/AutoGenValidator/{schemaName}Validator.cs")); + + Assert.That(generatedCode, Is.EqualTo(expected)); + } + } +} diff --git a/SysML2.NET.CodeGenerator/Extensions/OpenApiSchemaExtensions.cs b/SysML2.NET.CodeGenerator/Extensions/OpenApiSchemaExtensions.cs index 57a64adcc..65cfc80e8 100644 --- a/SysML2.NET.CodeGenerator/Extensions/OpenApiSchemaExtensions.cs +++ b/SysML2.NET.CodeGenerator/Extensions/OpenApiSchemaExtensions.cs @@ -291,6 +291,18 @@ public static bool QueryAdmitsNull(this IOpenApiSchema schema) && schema.OneOf?.Any(branch => branch is not OpenApiSchemaReference && branch.Type == JsonSchemaType.Null) == true; } + /// + /// Queries the minimum number of items of the list that the schema declares, looking through a nullable wrapper + /// + /// The subject . + /// The minimum number of items, or null when the schema declares no list or no lower bound. + public static int? QueryMinItems(this IOpenApiSchema schema) + { + ArgumentNullException.ThrowIfNull(schema); + + return schema.QueryIsCollection() ? schema.QueryUnwrapped().MinItems : null; + } + /// /// Queries the C# type of the schema, without its list wrapper or nullable annotation /// diff --git a/SysML2.NET.CodeGenerator/Generators/OpenApiHandleBarsGenerators/OpenApiDtoGenerator.cs b/SysML2.NET.CodeGenerator/Generators/OpenApiHandleBarsGenerators/OpenApiDtoGenerator.cs index 821660c9d..6c788c4bd 100644 --- a/SysML2.NET.CodeGenerator/Generators/OpenApiHandleBarsGenerators/OpenApiDtoGenerator.cs +++ b/SysML2.NET.CodeGenerator/Generators/OpenApiHandleBarsGenerators/OpenApiDtoGenerator.cs @@ -28,7 +28,7 @@ public class OpenApiDtoGenerator : OpenApiHandleBarsGenerator /// /// The suffix that marks a schema as one that is sent to the Systems Modeling API and Services /// - private const string RequestSchemaNameSuffix = "Request"; + public const string RequestSchemaNameSuffix = "Request"; /// /// The name of the template that generates a data transfer object class diff --git a/SysML2.NET.CodeGenerator/Generators/OpenApiHandleBarsGenerators/OpenApiRequestValidatorGenerator.cs b/SysML2.NET.CodeGenerator/Generators/OpenApiHandleBarsGenerators/OpenApiRequestValidatorGenerator.cs new file mode 100644 index 000000000..2a2196ad3 --- /dev/null +++ b/SysML2.NET.CodeGenerator/Generators/OpenApiHandleBarsGenerators/OpenApiRequestValidatorGenerator.cs @@ -0,0 +1,197 @@ +// ------------------------------------------------------------------------------------------------ +// +// +// Copyright (C) 2022-2026 Starion Group S.A. +// SPDX-License-Identifier: Apache-2.0 +// +// +// ------------------------------------------------------------------------------------------------ + +namespace SysML2.NET.CodeGenerator.Generators.OpenApiHandleBarsGenerators +{ + using System; + using System.Collections.Generic; + using System.IO; + using System.Linq; + using System.Threading.Tasks; + + using Microsoft.OpenApi; + + using SysML2.NET.CodeGenerator.Extensions; + using SysML2.NET.CodeGenerator.OpenApiHandleBarHelpers; + + /// + /// OpenAPI based handlebars generator for the FluentValidation validators of the request data transfer objects + /// + public class OpenApiRequestValidatorGenerator : OpenApiHandleBarsGenerator + { + /// + /// The name of the template that generates a request validator + /// + private const string ValidatorTemplateName = "psm-request-validator-openapi-template"; + + /// + /// The names of the schemas that a validator is generated for, scoped to the current generation run + /// + private HashSet validatedSchemaNames = []; + + /// + /// Queries the request schemas that a validator is generated for + /// + /// + /// the that contains the OpenAPI model to generate from + /// + /// + /// The request schemas that are not unions, ordered by name + /// + public static IReadOnlyList> QueryValidatedSchemas(OpenApiDocument openApiDocument) + { + ArgumentNullException.ThrowIfNull(openApiDocument); + + return QuerySchemas(openApiDocument) + .Where(schema => schema.Key.EndsWith(OpenApiDtoGenerator.RequestSchemaNameSuffix, StringComparison.Ordinal) && !schema.Value.QueryIsUnion()) + .ToList(); + } + + /// + /// Generates the validators of the request schemas of the OpenAPI document + /// + /// + /// the that contains the OpenAPI model to generate from + /// + /// + /// The target + /// + /// + /// an awaitable + /// + public override Task GenerateAsync(OpenApiDocument openApiDocument, DirectoryInfo outputDirectory) + { + ArgumentNullException.ThrowIfNull(openApiDocument); + ArgumentNullException.ThrowIfNull(outputDirectory); + + return this.GenerateInternalAsync(openApiDocument, outputDirectory); + } + + /// + /// Generates the validator of the specified request schema + /// + /// + /// the that contains the OpenAPI model to generate from + /// + /// + /// The target + /// + /// + /// The name of the request schema to generate the validator for + /// + /// + /// The generated code + /// + public Task GenerateRequestValidatorAsync(OpenApiDocument openApiDocument, DirectoryInfo outputDirectory, string name) + { + ArgumentNullException.ThrowIfNull(openApiDocument); + ArgumentNullException.ThrowIfNull(outputDirectory); + ArgumentException.ThrowIfNullOrWhiteSpace(name); + + return this.GenerateRequestValidatorInternalAsync(openApiDocument, outputDirectory, name); + } + + /// + /// Register the custom helpers + /// + protected override void RegisterHelpers() + { + this.Handlebars.RegisterValidatorHelper(schemaName => this.validatedSchemaNames.Contains(schemaName)); + } + + /// + /// Register the code templates + /// + protected override void RegisterTemplates() + { + this.RegisterTemplate(ValidatorTemplateName); + } + + /// + /// Generates the validators of the request schemas of the OpenAPI document + /// + /// + /// the that contains the OpenAPI model to generate from + /// + /// + /// The target + /// + /// + /// an awaitable + /// + private async Task GenerateInternalAsync(OpenApiDocument openApiDocument, DirectoryInfo outputDirectory) + { + foreach (var schema in this.AssignGenerationScope(openApiDocument)) + { + await this.WriteValidatorAsync(schema, outputDirectory); + } + } + + /// + /// Generates the validator of the specified request schema + /// + /// + /// the that contains the OpenAPI model to generate from + /// + /// + /// The target + /// + /// + /// The name of the request schema to generate the validator for + /// + /// + /// The generated code + /// + private async Task GenerateRequestValidatorInternalAsync(OpenApiDocument openApiDocument, DirectoryInfo outputDirectory, string name) + { + var schemas = this.AssignGenerationScope(openApiDocument); + + return await this.WriteValidatorAsync(schemas.Single(schema => schema.Key == name), outputDirectory); + } + + /// + /// Queries the request schemas and assigns the state that the registered helpers read from + /// + /// + /// the that contains the OpenAPI model to generate from + /// + /// + /// The request schemas that a validator is generated for, ordered by name + /// + private IReadOnlyList> AssignGenerationScope(OpenApiDocument openApiDocument) + { + var schemas = QueryValidatedSchemas(openApiDocument); + + this.validatedSchemaNames = schemas.Select(schema => schema.Key).ToHashSet(StringComparer.Ordinal); + + return schemas; + } + + /// + /// Applies the validator template to the schema and writes the result to disk + /// + /// + /// The request schema to generate the validator for + /// + /// + /// The target + /// + /// + /// The generated code + /// + private async Task WriteValidatorAsync(KeyValuePair schema, DirectoryInfo outputDirectory) + { + var generatedCode = this.CodeCleanup(this.Templates[ValidatorTemplateName](schema)); + + await WriteAsync(generatedCode, outputDirectory, $"{schema.Key}Validator.cs"); + + return generatedCode; + } + } +} diff --git a/SysML2.NET.CodeGenerator/OpenApiHandleBarHelpers/ValidatorHelper.cs b/SysML2.NET.CodeGenerator/OpenApiHandleBarHelpers/ValidatorHelper.cs new file mode 100644 index 000000000..ab5b4a8c9 --- /dev/null +++ b/SysML2.NET.CodeGenerator/OpenApiHandleBarHelpers/ValidatorHelper.cs @@ -0,0 +1,151 @@ +// ------------------------------------------------------------------------------------------------ +// +// +// Copyright (C) 2022-2026 Starion Group S.A. +// SPDX-License-Identifier: Apache-2.0 +// +// +// ------------------------------------------------------------------------------------------------ + +namespace SysML2.NET.CodeGenerator.OpenApiHandleBarHelpers +{ + using System; + using System.Collections.Generic; + using System.Linq; + + using HandlebarsDotNet; + + using Microsoft.OpenApi; + + using SysML2.NET.CodeGenerator.Extensions; + + /// + /// A handlebars block helper that writes the FluentValidation rules derived from a request + /// + public static class ValidatorHelper + { + /// + /// Registers the + /// + /// + /// The context with which the helper needs to be registered + /// + /// + /// A function that returns whether a validator is generated for a schema name + /// + public static void RegisterValidatorHelper(this IHandlebars handlebars, Func queryHasValidator) + { + ArgumentNullException.ThrowIfNull(handlebars); + ArgumentNullException.ThrowIfNull(queryHasValidator); + + handlebars.RegisterHelper("Validator.WriteRules", (writer, _, arguments) => + { + if (arguments.Length != 2) + { + throw new HandlebarsException("{{#Validator.WriteRules}} helper must have exactly two arguments"); + } + + var className = arguments[0] as string ?? throw new ArgumentException("supposed to be a schema name"); + var classSchema = arguments[1] as IOpenApiSchema ?? throw new ArgumentException("supposed to be IOpenApiSchema"); + + foreach (var rule in QueryRules(className, classSchema, queryHasValidator)) + { + writer.WriteSafeString(rule + Environment.NewLine); + } + }); + } + + /// + /// Queries the rule statements derived from every property of the class schema except its type discriminator + /// + /// The name of the class that the validator validates. + /// The of the class. + /// A function that returns whether a validator is generated for a schema name. + /// The rule statements, ordered by property name. + private static List QueryRules(string className, IOpenApiSchema classSchema, Func queryHasValidator) + { + return (classSchema.Properties ?? new Dictionary()) + .Where(property => property.Key != OpenApiSchemaExtensions.TypeDiscriminatorPropertyName) + .OrderBy(property => property.Key, StringComparer.Ordinal) + .SelectMany(property => QueryPropertyRules(className, classSchema, property.Key, property.Value, queryHasValidator)) + .ToList(); + } + + /// + /// Queries the rule statements derived from one property of the class schema + /// + /// The name of the class that declares the property. + /// The of the class. + /// The name of the property as it appears in the schema. + /// The of the property. + /// A function that returns whether a validator is generated for a schema name. + /// The rule statements. + private static IEnumerable QueryPropertyRules(string className, IOpenApiSchema classSchema, string propertyName, IOpenApiSchema propertySchema, Func queryHasValidator) + { + var selector = $"dto => dto.{OpenApiSchemaExtensions.QueryPropertyName(propertyName)}"; + var isPresenceRequired = classSchema.Required?.Contains(propertyName) == true && !propertySchema.QueryAdmitsNull(); + var isCollection = propertySchema.QueryIsCollection(); + var coreTypeName = propertySchema.QueryCoreTypeName(className, propertyName); + + if (coreTypeName == "Guid" && !isCollection) + { + yield return isPresenceRequired + ? $"this.RuleFor({selector}).NotEmpty();" + : $"this.RuleFor({selector}).Must(value => !value.HasValue || value.Value != Guid.Empty);"; + } + else if (coreTypeName == "string" && !isCollection && isPresenceRequired) + { + yield return $"this.RuleFor({selector}).NotEmpty();"; + } + else if (isPresenceRequired && (isCollection || !propertySchema.QueryIsValueType())) + { + yield return $"this.RuleFor({selector}).NotNull();"; + } + + if (coreTypeName == OpenApiSchemaExtensions.QueryEnumerationTypeName(className, propertyName)) + { + yield return $"this.RuleFor({selector}).IsInEnum();"; + } + + if (propertySchema.QueryMinItems() is { } minItems) + { + yield return $"this.RuleFor({selector}).Must(value => value == null || value.Count >= {minItems});"; + } + + if (coreTypeName == "Uri") + { + yield return $"this.RuleFor({selector}).Must(value => value == null || value.IsAbsoluteUri);"; + } + + var ruleName = isCollection ? "RuleForEach" : "RuleFor"; + var itemGuard = isCollection ? ".NotNull()" : string.Empty; + + if (propertySchema.QueryIsCompositeReference()) + { + var referencedName = propertySchema.QueryTerminalReferenceName(); + + if (queryHasValidator(referencedName)) + { + yield return $"this.{ruleName}({selector}){itemGuard}.SetValidator(new {referencedName}Validator());"; + } + } + else if (propertySchema.QueryIsPolymorphicReference()) + { + var alternativeNames = propertySchema.QueryTerminalReference().QueryUnionAlternativeNames() + .Where(queryHasValidator) + .ToList(); + + if (alternativeNames.Count > 0) + { + var registrations = string.Concat(alternativeNames.Select(alternativeName => $"validators.Add<{alternativeName}>(new {alternativeName}Validator());{Environment.NewLine}")); + + yield return $"this.{ruleName}({selector}){itemGuard}.SetInheritanceValidator(validators =>{Environment.NewLine}{{{Environment.NewLine}{registrations}}});"; + } + else if (isCollection) + { + yield return $"this.RuleForEach({selector}).NotNull();"; + } + } + } + } +} diff --git a/SysML2.NET.CodeGenerator/SysML2.NET.CodeGenerator.csproj b/SysML2.NET.CodeGenerator/SysML2.NET.CodeGenerator.csproj index ea6d8b8de..e4ec501f2 100644 --- a/SysML2.NET.CodeGenerator/SysML2.NET.CodeGenerator.csproj +++ b/SysML2.NET.CodeGenerator/SysML2.NET.CodeGenerator.csproj @@ -304,6 +304,9 @@ Always + + Always + Always diff --git a/SysML2.NET.CodeGenerator/Templates/OpenAPI/psm-request-validator-openapi-template.hbs b/SysML2.NET.CodeGenerator/Templates/OpenAPI/psm-request-validator-openapi-template.hbs new file mode 100644 index 000000000..d24b4c45a --- /dev/null +++ b/SysML2.NET.CodeGenerator/Templates/OpenAPI/psm-request-validator-openapi-template.hbs @@ -0,0 +1,43 @@ +// ------------------------------------------------------------------------------------------------ +// +// +// Copyright (C) 2022-2026 Starion Group S.A. +// SPDX-License-Identifier: Apache-2.0 +// +// +// ------------------------------------------------------------------------------------------------ + +// ------------------------------------------------------------------------------------------------ +// +// --------THIS IS AN AUTOMATICALLY GENERATED FILE. ANY MANUAL CHANGES WILL BE OVERWRITTEN!-------- +// ------------------------------------------------------------------------------------------------ + +namespace SysML2.NET.Validation.PSM +{ + using System; + using System.CodeDom.Compiler; + + using FluentValidation; + + using SysML2.NET.PSM.DTO; + + /// + /// Validates a against the {{this.Key}} schema of the Systems Modeling API and Services + /// + [GeneratedCode("SysML2.NET", "latest")] + public sealed partial class {{this.Key}}Validator : RequestValidator<{{this.Key}}> + { + /// + /// Initializes a new instance of the class + /// and registers its generated rules + /// + public {{this.Key}}Validator() + { + {{#Validator.WriteRules this.Key this.Value}} + } + } +} + +// ------------------------------------------------------------------------------------------------ +// --------THIS IS AN AUTOMATICALLY GENERATED FILE. ANY MANUAL CHANGES WILL BE OVERWRITTEN!-------- +// ------------------------------------------------------------------------------------------------ diff --git a/SysML2.NET.Validation.Tests/PSM/AutoGenValidator/BranchRequestValidatorTestFixture.cs b/SysML2.NET.Validation.Tests/PSM/AutoGenValidator/BranchRequestValidatorTestFixture.cs new file mode 100644 index 000000000..b69111a68 --- /dev/null +++ b/SysML2.NET.Validation.Tests/PSM/AutoGenValidator/BranchRequestValidatorTestFixture.cs @@ -0,0 +1,55 @@ +// ------------------------------------------------------------------------------------------------ +// +// +// Copyright (C) 2022-2026 Starion Group S.A. +// SPDX-License-Identifier: Apache-2.0 +// +// +// ------------------------------------------------------------------------------------------------ + +namespace SysML2.NET.Validation.Tests.PSM.AutoGenValidator +{ + using System; + using System.Linq; + + using NUnit.Framework; + + using SysML2.NET.PSM.DTO; + using SysML2.NET.Validation.PSM; + + [TestFixture] + public class BranchRequestValidatorTestFixture + { + private static readonly string[] NameFailure = ["Name"]; + + [Test] + public void VerifyValidate() + { + var validator = new BranchRequestValidator(); + var branchRequest = new BranchRequest { Name = "the branch", Head = Guid.NewGuid() }; + + Assert.That(validator.Validate(branchRequest).IsValid, Is.True); + + branchRequest.Head = null; + + Assert.That(validator.Validate(branchRequest).IsValid, Is.True); + + branchRequest.Head = Guid.Empty; + + Assert.That(validator.Validate(branchRequest).Errors.Select(failure => failure.PropertyName), Is.EquivalentTo(["Head"])); + + branchRequest.Head = Guid.NewGuid(); + branchRequest.Name = null; + + Assert.That(validator.Validate(branchRequest).Errors.Select(failure => failure.PropertyName), Is.EquivalentTo(NameFailure)); + + branchRequest.Name = string.Empty; + + Assert.That(validator.Validate(branchRequest).Errors.Select(failure => failure.PropertyName), Is.EquivalentTo(NameFailure)); + + branchRequest.Name = " "; + + Assert.That(validator.Validate(branchRequest).Errors.Select(failure => failure.PropertyName), Is.EquivalentTo(NameFailure)); + } + } +} diff --git a/SysML2.NET.Validation.Tests/PSM/AutoGenValidator/CommitRequestValidatorTestFixture.cs b/SysML2.NET.Validation.Tests/PSM/AutoGenValidator/CommitRequestValidatorTestFixture.cs new file mode 100644 index 000000000..a10dde316 --- /dev/null +++ b/SysML2.NET.Validation.Tests/PSM/AutoGenValidator/CommitRequestValidatorTestFixture.cs @@ -0,0 +1,50 @@ +// ------------------------------------------------------------------------------------------------ +// +// +// Copyright (C) 2022-2026 Starion Group S.A. +// SPDX-License-Identifier: Apache-2.0 +// +// +// ------------------------------------------------------------------------------------------------ + +namespace SysML2.NET.Validation.Tests.PSM.AutoGenValidator +{ + using System; + using System.Linq; + + using NUnit.Framework; + + using SysML2.NET.PSM.DTO; + using SysML2.NET.Validation.PSM; + + [TestFixture] + public class CommitRequestValidatorTestFixture + { + [Test] + public void VerifyValidate() + { + var validator = new CommitRequestValidator(); + var dataIdentityRequest = new DataIdentityRequest { Id = Guid.NewGuid() }; + var commitRequest = new CommitRequest { Change = [new DataVersionRequest { Identity = dataIdentityRequest }] }; + + Assert.That(validator.Validate(commitRequest).IsValid, Is.True); + + commitRequest.Change = []; + + Assert.That(validator.Validate(commitRequest).IsValid, Is.True); + + commitRequest.Change = null; + + Assert.That(validator.Validate(commitRequest).Errors.Select(failure => failure.PropertyName), Is.EquivalentTo(["Change"])); + + commitRequest.Change = [new DataVersionRequest(), null]; + + Assert.That(validator.Validate(commitRequest).Errors.Select(failure => failure.PropertyName), Is.EquivalentTo(["Change[1]"])); + + dataIdentityRequest.Id = Guid.Empty; + commitRequest.Change = [new DataVersionRequest { Identity = dataIdentityRequest }]; + + Assert.That(validator.Validate(commitRequest).Errors.Select(failure => failure.PropertyName), Is.EquivalentTo(["Change[0].Identity.Id"])); + } + } +} diff --git a/SysML2.NET.Validation.Tests/PSM/AutoGenValidator/CompositeConstraintRequestValidatorTestFixture.cs b/SysML2.NET.Validation.Tests/PSM/AutoGenValidator/CompositeConstraintRequestValidatorTestFixture.cs new file mode 100644 index 000000000..fa00d150b --- /dev/null +++ b/SysML2.NET.Validation.Tests/PSM/AutoGenValidator/CompositeConstraintRequestValidatorTestFixture.cs @@ -0,0 +1,55 @@ +// ------------------------------------------------------------------------------------------------ +// +// +// Copyright (C) 2022-2026 Starion Group S.A. +// SPDX-License-Identifier: Apache-2.0 +// +// +// ------------------------------------------------------------------------------------------------ + +namespace SysML2.NET.Validation.Tests.PSM.AutoGenValidator +{ + using System.Linq; + + using NUnit.Framework; + + using SysML2.NET.PSM.DTO; + using SysML2.NET.PSM.Enumerations; + using SysML2.NET.Validation.PSM; + + [TestFixture] + public class CompositeConstraintRequestValidatorTestFixture + { + [Test] + public void VerifyValidate() + { + var validator = new CompositeConstraintRequestValidator(); + var constraint = new PrimitiveConstraint { Property = "name" }; + + var compositeConstraintRequest = new CompositeConstraintRequest + { + Constraint = [constraint, constraint], + Operator = CompositeConstraintRequestOperator.and + }; + + Assert.That(validator.Validate(compositeConstraintRequest).IsValid, Is.True); + + compositeConstraintRequest.Constraint = [constraint]; + + Assert.That(validator.Validate(compositeConstraintRequest).Errors.Select(failure => failure.PropertyName), Is.EquivalentTo(["Constraint"])); + + compositeConstraintRequest.Constraint = null; + + Assert.That(validator.Validate(compositeConstraintRequest).Errors.Select(failure => failure.PropertyName), Is.EquivalentTo(["Constraint"])); + + compositeConstraintRequest.Constraint = [constraint, null]; + + Assert.That(validator.Validate(compositeConstraintRequest).Errors.Select(failure => failure.PropertyName), Is.EquivalentTo(["Constraint[1]"])); + + compositeConstraintRequest.Constraint = [constraint, constraint]; + compositeConstraintRequest.Operator = (CompositeConstraintRequestOperator)99; + + Assert.That(validator.Validate(compositeConstraintRequest).Errors.Select(failure => failure.PropertyName), Is.EquivalentTo(["Operator"])); + } + } +} diff --git a/SysML2.NET.Validation.Tests/PSM/AutoGenValidator/DataIdentityRequestValidatorTestFixture.cs b/SysML2.NET.Validation.Tests/PSM/AutoGenValidator/DataIdentityRequestValidatorTestFixture.cs new file mode 100644 index 000000000..06280bd34 --- /dev/null +++ b/SysML2.NET.Validation.Tests/PSM/AutoGenValidator/DataIdentityRequestValidatorTestFixture.cs @@ -0,0 +1,36 @@ +// ------------------------------------------------------------------------------------------------ +// +// +// Copyright (C) 2022-2026 Starion Group S.A. +// SPDX-License-Identifier: Apache-2.0 +// +// +// ------------------------------------------------------------------------------------------------ + +namespace SysML2.NET.Validation.Tests.PSM.AutoGenValidator +{ + using System; + using System.Linq; + + using NUnit.Framework; + + using SysML2.NET.PSM.DTO; + using SysML2.NET.Validation.PSM; + + [TestFixture] + public class DataIdentityRequestValidatorTestFixture + { + [Test] + public void VerifyValidate() + { + var validator = new DataIdentityRequestValidator(); + var dataIdentityRequest = new DataIdentityRequest { Id = Guid.NewGuid() }; + + Assert.That(validator.Validate(dataIdentityRequest).IsValid, Is.True); + + dataIdentityRequest.Id = Guid.Empty; + + Assert.That(validator.Validate(dataIdentityRequest).Errors.Select(failure => failure.PropertyName), Is.EquivalentTo(["Id"])); + } + } +} diff --git a/SysML2.NET.Validation.Tests/PSM/AutoGenValidator/DataVersionRequestValidatorTestFixture.cs b/SysML2.NET.Validation.Tests/PSM/AutoGenValidator/DataVersionRequestValidatorTestFixture.cs new file mode 100644 index 000000000..23117adc7 --- /dev/null +++ b/SysML2.NET.Validation.Tests/PSM/AutoGenValidator/DataVersionRequestValidatorTestFixture.cs @@ -0,0 +1,58 @@ +// ------------------------------------------------------------------------------------------------ +// +// +// Copyright (C) 2022-2026 Starion Group S.A. +// SPDX-License-Identifier: Apache-2.0 +// +// +// ------------------------------------------------------------------------------------------------ + +namespace SysML2.NET.Validation.Tests.PSM.AutoGenValidator +{ + using System; + using System.Linq; + + using NUnit.Framework; + + using SysML2.NET.Core.DTO.Root.Namespaces; + using SysML2.NET.PSM.DTO; + using SysML2.NET.Validation.PSM; + + [TestFixture] + public class DataVersionRequestValidatorTestFixture + { + [Test] + public void VerifyValidate() + { + var validator = new DataVersionRequestValidator(); + var dataVersionRequest = new DataVersionRequest(); + + Assert.That(validator.Validate(dataVersionRequest).IsValid, Is.True); + + dataVersionRequest.Payload = new Namespace { Id = Guid.NewGuid() }; + + Assert.That(validator.Validate(dataVersionRequest).IsValid, Is.True); + + dataVersionRequest.Payload = new ExternalDataRequest { Id = Guid.NewGuid() }; + + Assert.That(validator.Validate(dataVersionRequest).IsValid, Is.True); + + dataVersionRequest.Payload = new ExternalDataRequest { Id = Guid.Empty }; + + Assert.That(validator.Validate(dataVersionRequest).Errors.Select(failure => failure.PropertyName), Is.EquivalentTo(["Payload.Id"])); + + dataVersionRequest.Payload = new ExternalRelationshipRequest { ElementEnd = Guid.Empty }; + + Assert.That(validator.Validate(dataVersionRequest).Errors.Select(failure => failure.PropertyName), Is.EquivalentTo(["Payload.ElementEnd"])); + + dataVersionRequest.Payload = new ProjectUsageRequest { UsedCommit = Guid.Empty }; + + Assert.That(validator.Validate(dataVersionRequest).Errors.Select(failure => failure.PropertyName), Is.EquivalentTo(["Payload.UsedCommit"])); + + dataVersionRequest.Payload = null; + dataVersionRequest.Identity = new DataIdentityRequest { Id = Guid.Empty }; + + Assert.That(validator.Validate(dataVersionRequest).Errors.Select(failure => failure.PropertyName), Is.EquivalentTo(["Identity.Id"])); + } + } +} diff --git a/SysML2.NET.Validation.Tests/PSM/AutoGenValidator/ExternalDataRequestValidatorTestFixture.cs b/SysML2.NET.Validation.Tests/PSM/AutoGenValidator/ExternalDataRequestValidatorTestFixture.cs new file mode 100644 index 000000000..ea2a330d5 --- /dev/null +++ b/SysML2.NET.Validation.Tests/PSM/AutoGenValidator/ExternalDataRequestValidatorTestFixture.cs @@ -0,0 +1,46 @@ +// ------------------------------------------------------------------------------------------------ +// +// +// Copyright (C) 2022-2026 Starion Group S.A. +// SPDX-License-Identifier: Apache-2.0 +// +// +// ------------------------------------------------------------------------------------------------ + +namespace SysML2.NET.Validation.Tests.PSM.AutoGenValidator +{ + using System; + using System.Linq; + + using NUnit.Framework; + + using SysML2.NET.PSM.DTO; + using SysML2.NET.Validation.PSM; + + [TestFixture] + public class ExternalDataRequestValidatorTestFixture + { + [Test] + public void VerifyValidate() + { + var validator = new ExternalDataRequestValidator(); + var externalDataRequest = new ExternalDataRequest { ResourceIdentifier = new Uri("https://example.com/the-resource") }; + + Assert.That(validator.Validate(externalDataRequest).IsValid, Is.True); + + externalDataRequest.ResourceIdentifier = null; + externalDataRequest.Id = Guid.NewGuid(); + + Assert.That(validator.Validate(externalDataRequest).IsValid, Is.True); + + externalDataRequest.ResourceIdentifier = new Uri("the-resource", UriKind.Relative); + + Assert.That(validator.Validate(externalDataRequest).Errors.Select(failure => failure.PropertyName), Is.EquivalentTo(["ResourceIdentifier"])); + + externalDataRequest.ResourceIdentifier = null; + externalDataRequest.Id = Guid.Empty; + + Assert.That(validator.Validate(externalDataRequest).Errors.Select(failure => failure.PropertyName), Is.EquivalentTo(["Id"])); + } + } +} diff --git a/SysML2.NET.Validation.Tests/PSM/AutoGenValidator/ExternalRelationshipRequestValidatorTestFixture.cs b/SysML2.NET.Validation.Tests/PSM/AutoGenValidator/ExternalRelationshipRequestValidatorTestFixture.cs new file mode 100644 index 000000000..ceac4f238 --- /dev/null +++ b/SysML2.NET.Validation.Tests/PSM/AutoGenValidator/ExternalRelationshipRequestValidatorTestFixture.cs @@ -0,0 +1,45 @@ +// ------------------------------------------------------------------------------------------------ +// +// +// Copyright (C) 2022-2026 Starion Group S.A. +// SPDX-License-Identifier: Apache-2.0 +// +// +// ------------------------------------------------------------------------------------------------ + +namespace SysML2.NET.Validation.Tests.PSM.AutoGenValidator +{ + using System; + using System.Linq; + + using NUnit.Framework; + + using SysML2.NET.PSM.DTO; + using SysML2.NET.Validation.PSM; + + [TestFixture] + public class ExternalRelationshipRequestValidatorTestFixture + { + [Test] + public void VerifyValidate() + { + var validator = new ExternalRelationshipRequestValidator(); + var externalRelationshipRequest = new ExternalRelationshipRequest(); + + Assert.That(validator.Validate(externalRelationshipRequest).IsValid, Is.True); + + externalRelationshipRequest.Id = Guid.NewGuid(); + externalRelationshipRequest.ElementEnd = Guid.NewGuid(); + externalRelationshipRequest.ExternalDataEnd = Guid.NewGuid(); + + Assert.That(validator.Validate(externalRelationshipRequest).IsValid, Is.True); + + externalRelationshipRequest.Id = Guid.Empty; + externalRelationshipRequest.ElementEnd = Guid.Empty; + externalRelationshipRequest.ExternalDataEnd = Guid.Empty; + + Assert.That(validator.Validate(externalRelationshipRequest).Errors.Select(failure => failure.PropertyName), + Is.EquivalentTo(["Id", "ElementEnd", "ExternalDataEnd"])); + } + } +} diff --git a/SysML2.NET.Validation.Tests/PSM/AutoGenValidator/PrimitiveConstraintRequestValidatorTestFixture.cs b/SysML2.NET.Validation.Tests/PSM/AutoGenValidator/PrimitiveConstraintRequestValidatorTestFixture.cs new file mode 100644 index 000000000..52818cf56 --- /dev/null +++ b/SysML2.NET.Validation.Tests/PSM/AutoGenValidator/PrimitiveConstraintRequestValidatorTestFixture.cs @@ -0,0 +1,63 @@ +// ------------------------------------------------------------------------------------------------ +// +// +// Copyright (C) 2022-2026 Starion Group S.A. +// SPDX-License-Identifier: Apache-2.0 +// +// +// ------------------------------------------------------------------------------------------------ + +namespace SysML2.NET.Validation.Tests.PSM.AutoGenValidator +{ + using System.Linq; + + using NUnit.Framework; + + using SysML2.NET.PSM.DTO; + using SysML2.NET.PSM.Enumerations; + using SysML2.NET.Validation.PSM; + + [TestFixture] + public class PrimitiveConstraintRequestValidatorTestFixture + { + private static readonly string[] PropertyFailure = ["Property"]; + + [Test] + public void VerifyValidate() + { + var validator = new PrimitiveConstraintRequestValidator(); + + var primitiveConstraintRequest = new PrimitiveConstraintRequest + { + Operator = PrimitiveConstraintRequestOperator.equalto, + Property = "name", + Value = [ConstraintValue.From("the value")] + }; + + Assert.That(validator.Validate(primitiveConstraintRequest).IsValid, Is.True); + + primitiveConstraintRequest.Value = null; + + Assert.That(validator.Validate(primitiveConstraintRequest).IsValid, Is.True); + + primitiveConstraintRequest.Value = []; + + Assert.That(validator.Validate(primitiveConstraintRequest).Errors.Select(failure => failure.PropertyName), Is.EquivalentTo(["Value"])); + + primitiveConstraintRequest.Value = [ConstraintValue.Null]; + primitiveConstraintRequest.Property = null; + primitiveConstraintRequest.Operator = (PrimitiveConstraintRequestOperator)99; + + Assert.That(validator.Validate(primitiveConstraintRequest).Errors.Select(failure => failure.PropertyName), Is.EquivalentTo(["Property", "Operator"])); + + primitiveConstraintRequest.Operator = PrimitiveConstraintRequestOperator.equalto; + primitiveConstraintRequest.Property = string.Empty; + + Assert.That(validator.Validate(primitiveConstraintRequest).Errors.Select(failure => failure.PropertyName), Is.EquivalentTo(PropertyFailure)); + + primitiveConstraintRequest.Property = " "; + + Assert.That(validator.Validate(primitiveConstraintRequest).Errors.Select(failure => failure.PropertyName), Is.EquivalentTo(PropertyFailure)); + } + } +} diff --git a/SysML2.NET.Validation.Tests/PSM/AutoGenValidator/ProjectRequestValidatorTestFixture.cs b/SysML2.NET.Validation.Tests/PSM/AutoGenValidator/ProjectRequestValidatorTestFixture.cs new file mode 100644 index 000000000..fee3fa4f9 --- /dev/null +++ b/SysML2.NET.Validation.Tests/PSM/AutoGenValidator/ProjectRequestValidatorTestFixture.cs @@ -0,0 +1,55 @@ +// ------------------------------------------------------------------------------------------------ +// +// +// Copyright (C) 2022-2026 Starion Group S.A. +// SPDX-License-Identifier: Apache-2.0 +// +// +// ------------------------------------------------------------------------------------------------ + +namespace SysML2.NET.Validation.Tests.PSM.AutoGenValidator +{ + using System; + using System.Linq; + + using NUnit.Framework; + + using SysML2.NET.PSM.DTO; + using SysML2.NET.Validation.PSM; + + [TestFixture] + public class ProjectRequestValidatorTestFixture + { + private static readonly string[] NameFailure = ["Name"]; + + [Test] + public void VerifyValidate() + { + var validator = new ProjectRequestValidator(); + var projectRequest = new ProjectRequest { Name = "the project" }; + + Assert.That(validator.Validate(projectRequest).IsValid, Is.True); + + projectRequest.DefaultBranch = Guid.NewGuid(); + + Assert.That(validator.Validate(projectRequest).IsValid, Is.True); + + projectRequest.DefaultBranch = Guid.Empty; + + Assert.That(validator.Validate(projectRequest).Errors.Select(failure => failure.PropertyName), Is.EquivalentTo(["DefaultBranch"])); + + projectRequest.DefaultBranch = null; + projectRequest.Name = null; + + Assert.That(validator.Validate(projectRequest).Errors.Select(failure => failure.PropertyName), Is.EquivalentTo(NameFailure)); + + projectRequest.Name = string.Empty; + + Assert.That(validator.Validate(projectRequest).Errors.Select(failure => failure.PropertyName), Is.EquivalentTo(NameFailure)); + + projectRequest.Name = " "; + + Assert.That(validator.Validate(projectRequest).Errors.Select(failure => failure.PropertyName), Is.EquivalentTo(NameFailure)); + } + } +} diff --git a/SysML2.NET.Validation.Tests/PSM/AutoGenValidator/ProjectUsageRequestValidatorTestFixture.cs b/SysML2.NET.Validation.Tests/PSM/AutoGenValidator/ProjectUsageRequestValidatorTestFixture.cs new file mode 100644 index 000000000..ee7245be0 --- /dev/null +++ b/SysML2.NET.Validation.Tests/PSM/AutoGenValidator/ProjectUsageRequestValidatorTestFixture.cs @@ -0,0 +1,45 @@ +// ------------------------------------------------------------------------------------------------ +// +// +// Copyright (C) 2022-2026 Starion Group S.A. +// SPDX-License-Identifier: Apache-2.0 +// +// +// ------------------------------------------------------------------------------------------------ + +namespace SysML2.NET.Validation.Tests.PSM.AutoGenValidator +{ + using System; + using System.Linq; + + using NUnit.Framework; + + using SysML2.NET.PSM.DTO; + using SysML2.NET.Validation.PSM; + + [TestFixture] + public class ProjectUsageRequestValidatorTestFixture + { + [Test] + public void VerifyValidate() + { + var validator = new ProjectUsageRequestValidator(); + var projectUsageRequest = new ProjectUsageRequest(); + + Assert.That(validator.Validate(projectUsageRequest).IsValid, Is.True); + + projectUsageRequest.Id = Guid.NewGuid(); + projectUsageRequest.UsedCommit = Guid.NewGuid(); + projectUsageRequest.UsedProject = Guid.NewGuid(); + + Assert.That(validator.Validate(projectUsageRequest).IsValid, Is.True); + + projectUsageRequest.Id = Guid.Empty; + projectUsageRequest.UsedCommit = Guid.Empty; + projectUsageRequest.UsedProject = Guid.Empty; + + Assert.That(validator.Validate(projectUsageRequest).Errors.Select(failure => failure.PropertyName), + Is.EquivalentTo(["Id", "UsedCommit", "UsedProject"])); + } + } +} diff --git a/SysML2.NET.Validation.Tests/PSM/AutoGenValidator/QueryRequestValidatorTestFixture.cs b/SysML2.NET.Validation.Tests/PSM/AutoGenValidator/QueryRequestValidatorTestFixture.cs new file mode 100644 index 000000000..826fd1629 --- /dev/null +++ b/SysML2.NET.Validation.Tests/PSM/AutoGenValidator/QueryRequestValidatorTestFixture.cs @@ -0,0 +1,59 @@ +// ------------------------------------------------------------------------------------------------ +// +// +// Copyright (C) 2022-2026 Starion Group S.A. +// SPDX-License-Identifier: Apache-2.0 +// +// +// ------------------------------------------------------------------------------------------------ + +namespace SysML2.NET.Validation.Tests.PSM.AutoGenValidator +{ + using System.Linq; + + using NUnit.Framework; + + using SysML2.NET.PSM.DTO; + using SysML2.NET.PSM.Enumerations; + using SysML2.NET.Validation.PSM; + + [TestFixture] + public class QueryRequestValidatorTestFixture + { + private static readonly string[] NameFailure = ["Name"]; + + [Test] + public void VerifyValidate() + { + var validator = new QueryRequestValidator(); + var queryRequest = new QueryRequest { Name = "the query" }; + + Assert.That(validator.Validate(queryRequest).IsValid, Is.True); + + queryRequest.Where = new PrimitiveConstraintRequest { Property = "name" }; + + Assert.That(validator.Validate(queryRequest).IsValid, Is.True); + + queryRequest.Where = new PrimitiveConstraintRequest(); + + Assert.That(validator.Validate(queryRequest).Errors.Select(failure => failure.PropertyName), Is.EquivalentTo(["Where.Property"])); + + queryRequest.Where = new CompositeConstraintRequest { Constraint = [new PrimitiveConstraint()], Operator = CompositeConstraintRequestOperator.or }; + + Assert.That(validator.Validate(queryRequest).Errors.Select(failure => failure.PropertyName), Is.EquivalentTo(["Where.Constraint"])); + + queryRequest.Where = null; + queryRequest.Name = null; + + Assert.That(validator.Validate(queryRequest).Errors.Select(failure => failure.PropertyName), Is.EquivalentTo(NameFailure)); + + queryRequest.Name = string.Empty; + + Assert.That(validator.Validate(queryRequest).Errors.Select(failure => failure.PropertyName), Is.EquivalentTo(NameFailure)); + + queryRequest.Name = " "; + + Assert.That(validator.Validate(queryRequest).Errors.Select(failure => failure.PropertyName), Is.EquivalentTo(NameFailure)); + } + } +} diff --git a/SysML2.NET.Validation.Tests/PSM/AutoGenValidator/TagRequestValidatorTestFixture.cs b/SysML2.NET.Validation.Tests/PSM/AutoGenValidator/TagRequestValidatorTestFixture.cs new file mode 100644 index 000000000..1f6fe6739 --- /dev/null +++ b/SysML2.NET.Validation.Tests/PSM/AutoGenValidator/TagRequestValidatorTestFixture.cs @@ -0,0 +1,51 @@ +// ------------------------------------------------------------------------------------------------ +// +// +// Copyright (C) 2022-2026 Starion Group S.A. +// SPDX-License-Identifier: Apache-2.0 +// +// +// ------------------------------------------------------------------------------------------------ + +namespace SysML2.NET.Validation.Tests.PSM.AutoGenValidator +{ + using System; + using System.Linq; + + using NUnit.Framework; + + using SysML2.NET.PSM.DTO; + using SysML2.NET.Validation.PSM; + + [TestFixture] + public class TagRequestValidatorTestFixture + { + private static readonly string[] NameFailure = ["Name"]; + + [Test] + public void VerifyValidate() + { + var validator = new TagRequestValidator(); + var tagRequest = new TagRequest { Name = "the tag", TaggedCommit = Guid.NewGuid() }; + + Assert.That(validator.Validate(tagRequest).IsValid, Is.True); + + tagRequest.TaggedCommit = Guid.Empty; + + Assert.That(validator.Validate(tagRequest).Errors.Select(failure => failure.PropertyName), Is.EquivalentTo(["TaggedCommit"])); + + tagRequest.TaggedCommit = Guid.NewGuid(); + tagRequest.Name = null; + + Assert.That(validator.Validate(tagRequest).Errors.Select(failure => failure.PropertyName), Is.EquivalentTo(NameFailure)); + + tagRequest.Name = string.Empty; + + Assert.That(validator.Validate(tagRequest).Errors.Select(failure => failure.PropertyName), Is.EquivalentTo(NameFailure)); + + tagRequest.Name = " "; + + Assert.That(validator.Validate(tagRequest).Errors.Select(failure => failure.PropertyName), Is.EquivalentTo(NameFailure)); + } + } +} diff --git a/SysML2.NET.Validation.Tests/PSM/RequestValidatorTestFixture.cs b/SysML2.NET.Validation.Tests/PSM/RequestValidatorTestFixture.cs new file mode 100644 index 000000000..2d4bad631 --- /dev/null +++ b/SysML2.NET.Validation.Tests/PSM/RequestValidatorTestFixture.cs @@ -0,0 +1,61 @@ +// ------------------------------------------------------------------------------------------------ +// +// +// Copyright (C) 2022-2026 Starion Group S.A. +// SPDX-License-Identifier: Apache-2.0 +// +// +// ------------------------------------------------------------------------------------------------ + +namespace SysML2.NET.Validation.Tests.PSM +{ + using FluentValidation; + + using NUnit.Framework; + + using SysML2.NET.PSM.DTO; + using SysML2.NET.Validation.PSM; + + [TestFixture] + public class RequestValidatorTestFixture + { + [Test] + public void VerifyAddCustomValidation() + { + var projectRequest = new ProjectRequest { Name = "the project" }; + + Assert.That(new ValidatorWithoutCustomValidation().Validate(projectRequest).IsValid, Is.True); + + var validator = new ValidatorWithCustomValidation(); + + Assert.That(validator.Validate(projectRequest).IsValid, Is.True); + + projectRequest.Name = " "; + + var result = validator.Validate(projectRequest); + + using (Assert.EnterMultipleScope()) + { + Assert.That(result.IsValid, Is.False); + Assert.That(result.Errors, Has.Count.EqualTo(1)); + Assert.That(result.Errors[0].PropertyName, Is.EqualTo(nameof(ProjectRequest.Name))); + } + + Assert.That(new ValidatorWithoutCustomValidation().Validate(projectRequest).IsValid, Is.True); + } + + private sealed class ValidatorWithoutCustomValidation : RequestValidator + { + } + + private sealed class ValidatorWithCustomValidation : RequestValidator + { + protected override void AddCustomValidation() + { + base.AddCustomValidation(); + + this.RuleFor(dto => dto.Name).Must(value => !string.IsNullOrWhiteSpace(value)); + } + } + } +} diff --git a/SysML2.NET.Validation.Tests/SysML2.NET.Validation.Tests.csproj b/SysML2.NET.Validation.Tests/SysML2.NET.Validation.Tests.csproj new file mode 100644 index 000000000..621d479ab --- /dev/null +++ b/SysML2.NET.Validation.Tests/SysML2.NET.Validation.Tests.csproj @@ -0,0 +1,34 @@ + + + + net10.0 + 14.0 + Starion Group S.A. + Sam Gerene + Nunit test suite for the SysML2.NET.Validation Library + Copyright © Starion Group S.A. + Apache-2.0 + https://github.com/STARIONGROUP/SysML2.NET.git + Git + false + disable + false + true + en-US + + + + + + + + + + + + + + + + + diff --git a/SysML2.NET.Validation/PSM/AutoGenValidator/BranchRequestValidator.cs b/SysML2.NET.Validation/PSM/AutoGenValidator/BranchRequestValidator.cs new file mode 100644 index 000000000..77d93cc06 --- /dev/null +++ b/SysML2.NET.Validation/PSM/AutoGenValidator/BranchRequestValidator.cs @@ -0,0 +1,44 @@ +// ------------------------------------------------------------------------------------------------ +// +// +// Copyright (C) 2022-2026 Starion Group S.A. +// SPDX-License-Identifier: Apache-2.0 +// +// +// ------------------------------------------------------------------------------------------------ + +// ------------------------------------------------------------------------------------------------ +// +// --------THIS IS AN AUTOMATICALLY GENERATED FILE. ANY MANUAL CHANGES WILL BE OVERWRITTEN!-------- +// ------------------------------------------------------------------------------------------------ + +namespace SysML2.NET.Validation.PSM +{ + using System; + using System.CodeDom.Compiler; + + using FluentValidation; + + using SysML2.NET.PSM.DTO; + + /// + /// Validates a against the BranchRequest schema of the Systems Modeling API and Services + /// + [GeneratedCode("SysML2.NET", "latest")] + public sealed partial class BranchRequestValidator : RequestValidator + { + /// + /// Initializes a new instance of the class + /// and registers its generated rules + /// + public BranchRequestValidator() + { + this.RuleFor(dto => dto.Head).Must(value => !value.HasValue || value.Value != Guid.Empty); + this.RuleFor(dto => dto.Name).NotEmpty(); + } + } +} + +// ------------------------------------------------------------------------------------------------ +// --------THIS IS AN AUTOMATICALLY GENERATED FILE. ANY MANUAL CHANGES WILL BE OVERWRITTEN!-------- +// ------------------------------------------------------------------------------------------------ diff --git a/SysML2.NET.Validation/PSM/AutoGenValidator/CommitRequestValidator.cs b/SysML2.NET.Validation/PSM/AutoGenValidator/CommitRequestValidator.cs new file mode 100644 index 000000000..4689e23a3 --- /dev/null +++ b/SysML2.NET.Validation/PSM/AutoGenValidator/CommitRequestValidator.cs @@ -0,0 +1,44 @@ +// ------------------------------------------------------------------------------------------------ +// +// +// Copyright (C) 2022-2026 Starion Group S.A. +// SPDX-License-Identifier: Apache-2.0 +// +// +// ------------------------------------------------------------------------------------------------ + +// ------------------------------------------------------------------------------------------------ +// +// --------THIS IS AN AUTOMATICALLY GENERATED FILE. ANY MANUAL CHANGES WILL BE OVERWRITTEN!-------- +// ------------------------------------------------------------------------------------------------ + +namespace SysML2.NET.Validation.PSM +{ + using System; + using System.CodeDom.Compiler; + + using FluentValidation; + + using SysML2.NET.PSM.DTO; + + /// + /// Validates a against the CommitRequest schema of the Systems Modeling API and Services + /// + [GeneratedCode("SysML2.NET", "latest")] + public sealed partial class CommitRequestValidator : RequestValidator + { + /// + /// Initializes a new instance of the class + /// and registers its generated rules + /// + public CommitRequestValidator() + { + this.RuleFor(dto => dto.Change).NotNull(); + this.RuleForEach(dto => dto.Change).NotNull().SetValidator(new DataVersionRequestValidator()); + } + } +} + +// ------------------------------------------------------------------------------------------------ +// --------THIS IS AN AUTOMATICALLY GENERATED FILE. ANY MANUAL CHANGES WILL BE OVERWRITTEN!-------- +// ------------------------------------------------------------------------------------------------ diff --git a/SysML2.NET.Validation/PSM/AutoGenValidator/CompositeConstraintRequestValidator.cs b/SysML2.NET.Validation/PSM/AutoGenValidator/CompositeConstraintRequestValidator.cs new file mode 100644 index 000000000..a671c6e60 --- /dev/null +++ b/SysML2.NET.Validation/PSM/AutoGenValidator/CompositeConstraintRequestValidator.cs @@ -0,0 +1,46 @@ +// ------------------------------------------------------------------------------------------------ +// +// +// Copyright (C) 2022-2026 Starion Group S.A. +// SPDX-License-Identifier: Apache-2.0 +// +// +// ------------------------------------------------------------------------------------------------ + +// ------------------------------------------------------------------------------------------------ +// +// --------THIS IS AN AUTOMATICALLY GENERATED FILE. ANY MANUAL CHANGES WILL BE OVERWRITTEN!-------- +// ------------------------------------------------------------------------------------------------ + +namespace SysML2.NET.Validation.PSM +{ + using System; + using System.CodeDom.Compiler; + + using FluentValidation; + + using SysML2.NET.PSM.DTO; + + /// + /// Validates a against the CompositeConstraintRequest schema of the Systems Modeling API and Services + /// + [GeneratedCode("SysML2.NET", "latest")] + public sealed partial class CompositeConstraintRequestValidator : RequestValidator + { + /// + /// Initializes a new instance of the class + /// and registers its generated rules + /// + public CompositeConstraintRequestValidator() + { + this.RuleFor(dto => dto.Constraint).NotNull(); + this.RuleFor(dto => dto.Constraint).Must(value => value == null || value.Count >= 2); + this.RuleForEach(dto => dto.Constraint).NotNull(); + this.RuleFor(dto => dto.Operator).IsInEnum(); + } + } +} + +// ------------------------------------------------------------------------------------------------ +// --------THIS IS AN AUTOMATICALLY GENERATED FILE. ANY MANUAL CHANGES WILL BE OVERWRITTEN!-------- +// ------------------------------------------------------------------------------------------------ diff --git a/SysML2.NET.Validation/PSM/AutoGenValidator/DataIdentityRequestValidator.cs b/SysML2.NET.Validation/PSM/AutoGenValidator/DataIdentityRequestValidator.cs new file mode 100644 index 000000000..13a60012a --- /dev/null +++ b/SysML2.NET.Validation/PSM/AutoGenValidator/DataIdentityRequestValidator.cs @@ -0,0 +1,43 @@ +// ------------------------------------------------------------------------------------------------ +// +// +// Copyright (C) 2022-2026 Starion Group S.A. +// SPDX-License-Identifier: Apache-2.0 +// +// +// ------------------------------------------------------------------------------------------------ + +// ------------------------------------------------------------------------------------------------ +// +// --------THIS IS AN AUTOMATICALLY GENERATED FILE. ANY MANUAL CHANGES WILL BE OVERWRITTEN!-------- +// ------------------------------------------------------------------------------------------------ + +namespace SysML2.NET.Validation.PSM +{ + using System; + using System.CodeDom.Compiler; + + using FluentValidation; + + using SysML2.NET.PSM.DTO; + + /// + /// Validates a against the DataIdentityRequest schema of the Systems Modeling API and Services + /// + [GeneratedCode("SysML2.NET", "latest")] + public sealed partial class DataIdentityRequestValidator : RequestValidator + { + /// + /// Initializes a new instance of the class + /// and registers its generated rules + /// + public DataIdentityRequestValidator() + { + this.RuleFor(dto => dto.Id).NotEmpty(); + } + } +} + +// ------------------------------------------------------------------------------------------------ +// --------THIS IS AN AUTOMATICALLY GENERATED FILE. ANY MANUAL CHANGES WILL BE OVERWRITTEN!-------- +// ------------------------------------------------------------------------------------------------ diff --git a/SysML2.NET.Validation/PSM/AutoGenValidator/DataVersionRequestValidator.cs b/SysML2.NET.Validation/PSM/AutoGenValidator/DataVersionRequestValidator.cs new file mode 100644 index 000000000..88e54e3aa --- /dev/null +++ b/SysML2.NET.Validation/PSM/AutoGenValidator/DataVersionRequestValidator.cs @@ -0,0 +1,49 @@ +// ------------------------------------------------------------------------------------------------ +// +// +// Copyright (C) 2022-2026 Starion Group S.A. +// SPDX-License-Identifier: Apache-2.0 +// +// +// ------------------------------------------------------------------------------------------------ + +// ------------------------------------------------------------------------------------------------ +// +// --------THIS IS AN AUTOMATICALLY GENERATED FILE. ANY MANUAL CHANGES WILL BE OVERWRITTEN!-------- +// ------------------------------------------------------------------------------------------------ + +namespace SysML2.NET.Validation.PSM +{ + using System; + using System.CodeDom.Compiler; + + using FluentValidation; + + using SysML2.NET.PSM.DTO; + + /// + /// Validates a against the DataVersionRequest schema of the Systems Modeling API and Services + /// + [GeneratedCode("SysML2.NET", "latest")] + public sealed partial class DataVersionRequestValidator : RequestValidator + { + /// + /// Initializes a new instance of the class + /// and registers its generated rules + /// + public DataVersionRequestValidator() + { + this.RuleFor(dto => dto.Identity).SetValidator(new DataIdentityRequestValidator()); + this.RuleFor(dto => dto.Payload).SetInheritanceValidator(validators => + { + validators.Add(new ExternalDataRequestValidator()); + validators.Add(new ExternalRelationshipRequestValidator()); + validators.Add(new ProjectUsageRequestValidator()); + }); + } + } +} + +// ------------------------------------------------------------------------------------------------ +// --------THIS IS AN AUTOMATICALLY GENERATED FILE. ANY MANUAL CHANGES WILL BE OVERWRITTEN!-------- +// ------------------------------------------------------------------------------------------------ diff --git a/SysML2.NET.Validation/PSM/AutoGenValidator/ExternalDataRequestValidator.cs b/SysML2.NET.Validation/PSM/AutoGenValidator/ExternalDataRequestValidator.cs new file mode 100644 index 000000000..79a431d20 --- /dev/null +++ b/SysML2.NET.Validation/PSM/AutoGenValidator/ExternalDataRequestValidator.cs @@ -0,0 +1,44 @@ +// ------------------------------------------------------------------------------------------------ +// +// +// Copyright (C) 2022-2026 Starion Group S.A. +// SPDX-License-Identifier: Apache-2.0 +// +// +// ------------------------------------------------------------------------------------------------ + +// ------------------------------------------------------------------------------------------------ +// +// --------THIS IS AN AUTOMATICALLY GENERATED FILE. ANY MANUAL CHANGES WILL BE OVERWRITTEN!-------- +// ------------------------------------------------------------------------------------------------ + +namespace SysML2.NET.Validation.PSM +{ + using System; + using System.CodeDom.Compiler; + + using FluentValidation; + + using SysML2.NET.PSM.DTO; + + /// + /// Validates a against the ExternalDataRequest schema of the Systems Modeling API and Services + /// + [GeneratedCode("SysML2.NET", "latest")] + public sealed partial class ExternalDataRequestValidator : RequestValidator + { + /// + /// Initializes a new instance of the class + /// and registers its generated rules + /// + public ExternalDataRequestValidator() + { + this.RuleFor(dto => dto.Id).Must(value => !value.HasValue || value.Value != Guid.Empty); + this.RuleFor(dto => dto.ResourceIdentifier).Must(value => value == null || value.IsAbsoluteUri); + } + } +} + +// ------------------------------------------------------------------------------------------------ +// --------THIS IS AN AUTOMATICALLY GENERATED FILE. ANY MANUAL CHANGES WILL BE OVERWRITTEN!-------- +// ------------------------------------------------------------------------------------------------ diff --git a/SysML2.NET.Validation/PSM/AutoGenValidator/ExternalRelationshipRequestValidator.cs b/SysML2.NET.Validation/PSM/AutoGenValidator/ExternalRelationshipRequestValidator.cs new file mode 100644 index 000000000..215f5c9a2 --- /dev/null +++ b/SysML2.NET.Validation/PSM/AutoGenValidator/ExternalRelationshipRequestValidator.cs @@ -0,0 +1,45 @@ +// ------------------------------------------------------------------------------------------------ +// +// +// Copyright (C) 2022-2026 Starion Group S.A. +// SPDX-License-Identifier: Apache-2.0 +// +// +// ------------------------------------------------------------------------------------------------ + +// ------------------------------------------------------------------------------------------------ +// +// --------THIS IS AN AUTOMATICALLY GENERATED FILE. ANY MANUAL CHANGES WILL BE OVERWRITTEN!-------- +// ------------------------------------------------------------------------------------------------ + +namespace SysML2.NET.Validation.PSM +{ + using System; + using System.CodeDom.Compiler; + + using FluentValidation; + + using SysML2.NET.PSM.DTO; + + /// + /// Validates a against the ExternalRelationshipRequest schema of the Systems Modeling API and Services + /// + [GeneratedCode("SysML2.NET", "latest")] + public sealed partial class ExternalRelationshipRequestValidator : RequestValidator + { + /// + /// Initializes a new instance of the class + /// and registers its generated rules + /// + public ExternalRelationshipRequestValidator() + { + this.RuleFor(dto => dto.Id).Must(value => !value.HasValue || value.Value != Guid.Empty); + this.RuleFor(dto => dto.ElementEnd).Must(value => !value.HasValue || value.Value != Guid.Empty); + this.RuleFor(dto => dto.ExternalDataEnd).Must(value => !value.HasValue || value.Value != Guid.Empty); + } + } +} + +// ------------------------------------------------------------------------------------------------ +// --------THIS IS AN AUTOMATICALLY GENERATED FILE. ANY MANUAL CHANGES WILL BE OVERWRITTEN!-------- +// ------------------------------------------------------------------------------------------------ diff --git a/SysML2.NET.Validation/PSM/AutoGenValidator/PrimitiveConstraintRequestValidator.cs b/SysML2.NET.Validation/PSM/AutoGenValidator/PrimitiveConstraintRequestValidator.cs new file mode 100644 index 000000000..50b47a8f8 --- /dev/null +++ b/SysML2.NET.Validation/PSM/AutoGenValidator/PrimitiveConstraintRequestValidator.cs @@ -0,0 +1,45 @@ +// ------------------------------------------------------------------------------------------------ +// +// +// Copyright (C) 2022-2026 Starion Group S.A. +// SPDX-License-Identifier: Apache-2.0 +// +// +// ------------------------------------------------------------------------------------------------ + +// ------------------------------------------------------------------------------------------------ +// +// --------THIS IS AN AUTOMATICALLY GENERATED FILE. ANY MANUAL CHANGES WILL BE OVERWRITTEN!-------- +// ------------------------------------------------------------------------------------------------ + +namespace SysML2.NET.Validation.PSM +{ + using System; + using System.CodeDom.Compiler; + + using FluentValidation; + + using SysML2.NET.PSM.DTO; + + /// + /// Validates a against the PrimitiveConstraintRequest schema of the Systems Modeling API and Services + /// + [GeneratedCode("SysML2.NET", "latest")] + public sealed partial class PrimitiveConstraintRequestValidator : RequestValidator + { + /// + /// Initializes a new instance of the class + /// and registers its generated rules + /// + public PrimitiveConstraintRequestValidator() + { + this.RuleFor(dto => dto.Operator).IsInEnum(); + this.RuleFor(dto => dto.Property).NotEmpty(); + this.RuleFor(dto => dto.Value).Must(value => value == null || value.Count >= 1); + } + } +} + +// ------------------------------------------------------------------------------------------------ +// --------THIS IS AN AUTOMATICALLY GENERATED FILE. ANY MANUAL CHANGES WILL BE OVERWRITTEN!-------- +// ------------------------------------------------------------------------------------------------ diff --git a/SysML2.NET.Validation/PSM/AutoGenValidator/ProjectRequestValidator.cs b/SysML2.NET.Validation/PSM/AutoGenValidator/ProjectRequestValidator.cs new file mode 100644 index 000000000..edfcafaed --- /dev/null +++ b/SysML2.NET.Validation/PSM/AutoGenValidator/ProjectRequestValidator.cs @@ -0,0 +1,44 @@ +// ------------------------------------------------------------------------------------------------ +// +// +// Copyright (C) 2022-2026 Starion Group S.A. +// SPDX-License-Identifier: Apache-2.0 +// +// +// ------------------------------------------------------------------------------------------------ + +// ------------------------------------------------------------------------------------------------ +// +// --------THIS IS AN AUTOMATICALLY GENERATED FILE. ANY MANUAL CHANGES WILL BE OVERWRITTEN!-------- +// ------------------------------------------------------------------------------------------------ + +namespace SysML2.NET.Validation.PSM +{ + using System; + using System.CodeDom.Compiler; + + using FluentValidation; + + using SysML2.NET.PSM.DTO; + + /// + /// Validates a against the ProjectRequest schema of the Systems Modeling API and Services + /// + [GeneratedCode("SysML2.NET", "latest")] + public sealed partial class ProjectRequestValidator : RequestValidator + { + /// + /// Initializes a new instance of the class + /// and registers its generated rules + /// + public ProjectRequestValidator() + { + this.RuleFor(dto => dto.DefaultBranch).Must(value => !value.HasValue || value.Value != Guid.Empty); + this.RuleFor(dto => dto.Name).NotEmpty(); + } + } +} + +// ------------------------------------------------------------------------------------------------ +// --------THIS IS AN AUTOMATICALLY GENERATED FILE. ANY MANUAL CHANGES WILL BE OVERWRITTEN!-------- +// ------------------------------------------------------------------------------------------------ diff --git a/SysML2.NET.Validation/PSM/AutoGenValidator/ProjectUsageRequestValidator.cs b/SysML2.NET.Validation/PSM/AutoGenValidator/ProjectUsageRequestValidator.cs new file mode 100644 index 000000000..a60be067d --- /dev/null +++ b/SysML2.NET.Validation/PSM/AutoGenValidator/ProjectUsageRequestValidator.cs @@ -0,0 +1,45 @@ +// ------------------------------------------------------------------------------------------------ +// +// +// Copyright (C) 2022-2026 Starion Group S.A. +// SPDX-License-Identifier: Apache-2.0 +// +// +// ------------------------------------------------------------------------------------------------ + +// ------------------------------------------------------------------------------------------------ +// +// --------THIS IS AN AUTOMATICALLY GENERATED FILE. ANY MANUAL CHANGES WILL BE OVERWRITTEN!-------- +// ------------------------------------------------------------------------------------------------ + +namespace SysML2.NET.Validation.PSM +{ + using System; + using System.CodeDom.Compiler; + + using FluentValidation; + + using SysML2.NET.PSM.DTO; + + /// + /// Validates a against the ProjectUsageRequest schema of the Systems Modeling API and Services + /// + [GeneratedCode("SysML2.NET", "latest")] + public sealed partial class ProjectUsageRequestValidator : RequestValidator + { + /// + /// Initializes a new instance of the class + /// and registers its generated rules + /// + public ProjectUsageRequestValidator() + { + this.RuleFor(dto => dto.Id).Must(value => !value.HasValue || value.Value != Guid.Empty); + this.RuleFor(dto => dto.UsedCommit).Must(value => !value.HasValue || value.Value != Guid.Empty); + this.RuleFor(dto => dto.UsedProject).Must(value => !value.HasValue || value.Value != Guid.Empty); + } + } +} + +// ------------------------------------------------------------------------------------------------ +// --------THIS IS AN AUTOMATICALLY GENERATED FILE. ANY MANUAL CHANGES WILL BE OVERWRITTEN!-------- +// ------------------------------------------------------------------------------------------------ diff --git a/SysML2.NET.Validation/PSM/AutoGenValidator/QueryRequestValidator.cs b/SysML2.NET.Validation/PSM/AutoGenValidator/QueryRequestValidator.cs new file mode 100644 index 000000000..0eb30cba9 --- /dev/null +++ b/SysML2.NET.Validation/PSM/AutoGenValidator/QueryRequestValidator.cs @@ -0,0 +1,48 @@ +// ------------------------------------------------------------------------------------------------ +// +// +// Copyright (C) 2022-2026 Starion Group S.A. +// SPDX-License-Identifier: Apache-2.0 +// +// +// ------------------------------------------------------------------------------------------------ + +// ------------------------------------------------------------------------------------------------ +// +// --------THIS IS AN AUTOMATICALLY GENERATED FILE. ANY MANUAL CHANGES WILL BE OVERWRITTEN!-------- +// ------------------------------------------------------------------------------------------------ + +namespace SysML2.NET.Validation.PSM +{ + using System; + using System.CodeDom.Compiler; + + using FluentValidation; + + using SysML2.NET.PSM.DTO; + + /// + /// Validates a against the QueryRequest schema of the Systems Modeling API and Services + /// + [GeneratedCode("SysML2.NET", "latest")] + public sealed partial class QueryRequestValidator : RequestValidator + { + /// + /// Initializes a new instance of the class + /// and registers its generated rules + /// + public QueryRequestValidator() + { + this.RuleFor(dto => dto.Name).NotEmpty(); + this.RuleFor(dto => dto.Where).SetInheritanceValidator(validators => + { + validators.Add(new CompositeConstraintRequestValidator()); + validators.Add(new PrimitiveConstraintRequestValidator()); + }); + } + } +} + +// ------------------------------------------------------------------------------------------------ +// --------THIS IS AN AUTOMATICALLY GENERATED FILE. ANY MANUAL CHANGES WILL BE OVERWRITTEN!-------- +// ------------------------------------------------------------------------------------------------ diff --git a/SysML2.NET.Validation/PSM/AutoGenValidator/TagRequestValidator.cs b/SysML2.NET.Validation/PSM/AutoGenValidator/TagRequestValidator.cs new file mode 100644 index 000000000..22d201a3e --- /dev/null +++ b/SysML2.NET.Validation/PSM/AutoGenValidator/TagRequestValidator.cs @@ -0,0 +1,44 @@ +// ------------------------------------------------------------------------------------------------ +// +// +// Copyright (C) 2022-2026 Starion Group S.A. +// SPDX-License-Identifier: Apache-2.0 +// +// +// ------------------------------------------------------------------------------------------------ + +// ------------------------------------------------------------------------------------------------ +// +// --------THIS IS AN AUTOMATICALLY GENERATED FILE. ANY MANUAL CHANGES WILL BE OVERWRITTEN!-------- +// ------------------------------------------------------------------------------------------------ + +namespace SysML2.NET.Validation.PSM +{ + using System; + using System.CodeDom.Compiler; + + using FluentValidation; + + using SysML2.NET.PSM.DTO; + + /// + /// Validates a against the TagRequest schema of the Systems Modeling API and Services + /// + [GeneratedCode("SysML2.NET", "latest")] + public sealed partial class TagRequestValidator : RequestValidator + { + /// + /// Initializes a new instance of the class + /// and registers its generated rules + /// + public TagRequestValidator() + { + this.RuleFor(dto => dto.Name).NotEmpty(); + this.RuleFor(dto => dto.TaggedCommit).NotEmpty(); + } + } +} + +// ------------------------------------------------------------------------------------------------ +// --------THIS IS AN AUTOMATICALLY GENERATED FILE. ANY MANUAL CHANGES WILL BE OVERWRITTEN!-------- +// ------------------------------------------------------------------------------------------------ diff --git a/SysML2.NET.Validation/PSM/RequestValidator.cs b/SysML2.NET.Validation/PSM/RequestValidator.cs new file mode 100644 index 000000000..3c6de8bff --- /dev/null +++ b/SysML2.NET.Validation/PSM/RequestValidator.cs @@ -0,0 +1,50 @@ +// ------------------------------------------------------------------------------------------------ +// +// +// Copyright (C) 2022-2026 Starion Group S.A. +// SPDX-License-Identifier: Apache-2.0 +// +// +// ------------------------------------------------------------------------------------------------ + +namespace SysML2.NET.Validation.PSM +{ + using FluentValidation; + + using SysML2.NET.PSM.DTO; + + /// + /// Base class of the validators that check a request payload of the Systems Modeling API and Services before it is processed + /// + /// + /// The type that is validated + /// + public abstract class RequestValidator : AbstractValidator where TRequest : IRequest + { + /// + /// Initializes a new instance of the class and registers its hand-coded rules + /// + protected RequestValidator() + { + this.RegisterCustomValidation(); + } + + /// + /// Registers the hand-coded rules that complement the rules derived from the OpenAPI document + /// + /// + /// The base constructor invokes this method once, before the derived constructor registers the generated rules. + /// + protected virtual void AddCustomValidation() + { + } + + /// + /// Invokes from the constructor through a non-virtual call site + /// + private void RegisterCustomValidation() + { + this.AddCustomValidation(); + } + } +} diff --git a/SysML2.NET.Validation/SysML2.NET.Validation.csproj b/SysML2.NET.Validation/SysML2.NET.Validation.csproj new file mode 100644 index 000000000..ba971f967 --- /dev/null +++ b/SysML2.NET.Validation/SysML2.NET.Validation.csproj @@ -0,0 +1,30 @@ + + + + net10.0 + 14.0 + 0.26.0 + A .NET implementation of the OMG SysML v2 REST/HTTP PSM request validators + SysML2.NET.Validation + Starion Group S.A. + Copyright © Starion Group S.A. + Apache-2.0 + https://github.com/STARIONGROUP/SysML2.NET.git + Git + Sam Gerené + true + true + true + + + + + + + + + + + + + diff --git a/SysML2.NET.sln b/SysML2.NET.sln index 0a602b48f..aa93c9ce6 100644 --- a/SysML2.NET.sln +++ b/SysML2.NET.sln @@ -76,6 +76,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SysML2.NET.Semantics", "Sys EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SysML2.NET.Semantics.Tests", "SysML2.NET.Semantics.Tests\SysML2.NET.Semantics.Tests.csproj", "{AE364357-591B-468D-9C39-9656B9A189C7}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SysML2.NET.Validation", "SysML2.NET.Validation\SysML2.NET.Validation.csproj", "{6DB872A1-678F-4AAF-952A-63C76F3DF7AF}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SysML2.NET.Validation.Tests", "SysML2.NET.Validation.Tests\SysML2.NET.Validation.Tests.csproj", "{42F385BC-BA62-4EEA-9F44-B7CC6F3B9A6F}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -350,6 +354,30 @@ Global {AE364357-591B-468D-9C39-9656B9A189C7}.Release|x64.Build.0 = Release|Any CPU {AE364357-591B-468D-9C39-9656B9A189C7}.Release|x86.ActiveCfg = Release|Any CPU {AE364357-591B-468D-9C39-9656B9A189C7}.Release|x86.Build.0 = Release|Any CPU + {6DB872A1-678F-4AAF-952A-63C76F3DF7AF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {6DB872A1-678F-4AAF-952A-63C76F3DF7AF}.Debug|Any CPU.Build.0 = Debug|Any CPU + {6DB872A1-678F-4AAF-952A-63C76F3DF7AF}.Debug|x64.ActiveCfg = Debug|Any CPU + {6DB872A1-678F-4AAF-952A-63C76F3DF7AF}.Debug|x64.Build.0 = Debug|Any CPU + {6DB872A1-678F-4AAF-952A-63C76F3DF7AF}.Debug|x86.ActiveCfg = Debug|Any CPU + {6DB872A1-678F-4AAF-952A-63C76F3DF7AF}.Debug|x86.Build.0 = Debug|Any CPU + {6DB872A1-678F-4AAF-952A-63C76F3DF7AF}.Release|Any CPU.ActiveCfg = Release|Any CPU + {6DB872A1-678F-4AAF-952A-63C76F3DF7AF}.Release|Any CPU.Build.0 = Release|Any CPU + {6DB872A1-678F-4AAF-952A-63C76F3DF7AF}.Release|x64.ActiveCfg = Release|Any CPU + {6DB872A1-678F-4AAF-952A-63C76F3DF7AF}.Release|x64.Build.0 = Release|Any CPU + {6DB872A1-678F-4AAF-952A-63C76F3DF7AF}.Release|x86.ActiveCfg = Release|Any CPU + {6DB872A1-678F-4AAF-952A-63C76F3DF7AF}.Release|x86.Build.0 = Release|Any CPU + {42F385BC-BA62-4EEA-9F44-B7CC6F3B9A6F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {42F385BC-BA62-4EEA-9F44-B7CC6F3B9A6F}.Debug|Any CPU.Build.0 = Debug|Any CPU + {42F385BC-BA62-4EEA-9F44-B7CC6F3B9A6F}.Debug|x64.ActiveCfg = Debug|Any CPU + {42F385BC-BA62-4EEA-9F44-B7CC6F3B9A6F}.Debug|x64.Build.0 = Debug|Any CPU + {42F385BC-BA62-4EEA-9F44-B7CC6F3B9A6F}.Debug|x86.ActiveCfg = Debug|Any CPU + {42F385BC-BA62-4EEA-9F44-B7CC6F3B9A6F}.Debug|x86.Build.0 = Debug|Any CPU + {42F385BC-BA62-4EEA-9F44-B7CC6F3B9A6F}.Release|Any CPU.ActiveCfg = Release|Any CPU + {42F385BC-BA62-4EEA-9F44-B7CC6F3B9A6F}.Release|Any CPU.Build.0 = Release|Any CPU + {42F385BC-BA62-4EEA-9F44-B7CC6F3B9A6F}.Release|x64.ActiveCfg = Release|Any CPU + {42F385BC-BA62-4EEA-9F44-B7CC6F3B9A6F}.Release|x64.Build.0 = Release|Any CPU + {42F385BC-BA62-4EEA-9F44-B7CC6F3B9A6F}.Release|x86.ActiveCfg = Release|Any CPU + {42F385BC-BA62-4EEA-9F44-B7CC6F3B9A6F}.Release|x86.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE