Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move Yaml Validator into Persistence Part 2 #700

Merged
merged 24 commits into from
Jul 23, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
f47376e
move Yaml Validator into persistence to setup for PAC CLI integration
abaskk-msft Jul 9, 2024
cee7faf
clean yaml validator project residue to integrate into persistence an…
abaskk-msft Jul 10, 2024
ef050cd
couple schema with schema loader
abaskk-msft Jul 11, 2024
ca58cc6
iteration two, ready for consumption by pac cli
abaskk-msft Jul 15, 2024
43dfe41
supress unused member of class
abaskk-msft Jul 15, 2024
5322752
rewrite of schema loader to store schema in dll on build and access f…
abaskk-msft Jul 17, 2024
dfb79a3
clean up new schema loader
abaskk-msft Jul 17, 2024
1d5d8c7
Merge branch 'master' into users/t-abaskar/persistence-yaml-validator…
abaskk-msft Jul 17, 2024
378656e
keep current commit head
abaskk-msft Jul 17, 2024
f773b62
add suppression suggestions
abaskk-msft Jul 17, 2024
ca1d151
access assembly from type to improve performance
abaskk-msft Jul 17, 2024
54b9b17
Interfaces for external use, make classes that interact with dependen…
abaskk-msft Jul 17, 2024
be6a1bf
Merge branch 'master' into users/t-abaskar/persistence-yaml-validator…
abaskk-msft Jul 17, 2024
e4def43
delete unused files
abaskk-msft Jul 17, 2024
82f1abb
check for validator interface instead of validator
abaskk-msft Jul 18, 2024
7a07194
make schema loader internal
abaskk-msft Jul 18, 2024
32c1de6
add factory to service provider. Create internal access for test in P…
abaskk-msft Jul 18, 2024
18b912b
remove unused directive
abaskk-msft Jul 19, 2024
caa24cb
remove unused file
abaskk-msft Jul 19, 2024
d06cf27
nit: whietspace at end of file
abaskk-msft Jul 19, 2024
1cc054c
make supressions inline
abaskk-msft Jul 19, 2024
f8f3696
move yaml-validator out with changes (persistence not compatiable wit…
abaskk-msft Jul 22, 2024
dcfea66
update project name
abaskk-msft Jul 22, 2024
b39d57c
strongly sign vailidator and make it visible to tests
abaskk-msft Jul 23, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 0 additions & 11 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,6 @@ jobs:
- name: Test - PAModel
run: dotnet bin/Debug/PAModelTests/PAModelTests.dll

- name: Test - YamlValidator
run: dotnet bin/Debug/YamlValidator.Tests/YamlValidator.Tests.dll

- name: Test - Persistence.Tests Yaml Files (ubuntu & macos)
run: ./scripts/PersistenceValidate.sh
if: ${{ runner.os != 'Windows' }}

- name: Test - Persistence.Tests Yaml Files (windows)
run: ./scripts/PersistenceValidate.ps1
if: ${{ runner.os == 'Windows' }}

- name: Restore workloads for samples
run: dotnet workload restore samples/MauiMsApp/MauiMsApp.csproj

Expand Down
23 changes: 0 additions & 23 deletions scripts/PersistenceValidate.ps1

This file was deleted.

16 changes: 0 additions & 16 deletions scripts/PersistenceValidate.sh

This file was deleted.

6 changes: 5 additions & 1 deletion src/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
<EnforceCodeStyleInBuild>true</EnforceCodeStyleInBuild>
<EnableNETAnalyzers>true</EnableNETAnalyzers>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<!-- Warning treated as error for dotnet compiler, see Pr #660 and this for context
https://github.com/dotnet/core/blob/main/release-notes/8.0/known-issues.md#802xx-sdk-is-not-compatible-with-178-for-some-scenarios
abaskk-msft marked this conversation as resolved.
Show resolved Hide resolved
-->
<WarningsNotAsErrors>NU1603</WarningsNotAsErrors>
abaskk-msft marked this conversation as resolved.
Show resolved Hide resolved
<CodeAnalysisTreatWarningsAsErrors>true</CodeAnalysisTreatWarningsAsErrors>
</PropertyGroup>

Expand Down Expand Up @@ -92,4 +96,4 @@
<RestoreIgnoreFailedSource>true</RestoreIgnoreFailedSource>
<RestoreOutputPath>$(BaseIntermediateOutputPath)</RestoreOutputPath>
</PropertyGroup>
</Project>
</Project>
13 changes: 0 additions & 13 deletions src/PASopa.sln
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,6 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.PowerPlatform.Pow
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Persistence.Tests", "Persistence.Tests\Persistence.Tests.csproj", "{8AB1C901-FE5E-44BF-AA21-B8F20A9D7CDD}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "YamlValidator", "YamlValidator\YamlValidator.csproj", "{F0AD11CE-E634-4945-A6B1-7866CDE0059C}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "YamlValidator.Tests", "YamlValidator.Tests\YamlValidator.Tests.csproj", "{8BA5DD4B-9423-4827-AF37-540E0300DB9A}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{7361DB16-D534-4E0E-8597-BE22317DEF47}"
ProjectSection(SolutionItems) = preProject
Directory.Build.props = Directory.Build.props
Expand Down Expand Up @@ -56,22 +52,13 @@ Global
{8AB1C901-FE5E-44BF-AA21-B8F20A9D7CDD}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8AB1C901-FE5E-44BF-AA21-B8F20A9D7CDD}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8AB1C901-FE5E-44BF-AA21-B8F20A9D7CDD}.Release|Any CPU.Build.0 = Release|Any CPU
{F0AD11CE-E634-4945-A6B1-7866CDE0059C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F0AD11CE-E634-4945-A6B1-7866CDE0059C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F0AD11CE-E634-4945-A6B1-7866CDE0059C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F0AD11CE-E634-4945-A6B1-7866CDE0059C}.Release|Any CPU.Build.0 = Release|Any CPU
{8BA5DD4B-9423-4827-AF37-540E0300DB9A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{8BA5DD4B-9423-4827-AF37-540E0300DB9A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8BA5DD4B-9423-4827-AF37-540E0300DB9A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8BA5DD4B-9423-4827-AF37-540E0300DB9A}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{8AD94CC0-7330-4880-A8E0-177B37CDB27B} = {4993E606-484B-46D9-892E-7AE9CE8D4423}
{8AB1C901-FE5E-44BF-AA21-B8F20A9D7CDD} = {4993E606-484B-46D9-892E-7AE9CE8D4423}
{8BA5DD4B-9423-4827-AF37-540E0300DB9A} = {4993E606-484B-46D9-892E-7AE9CE8D4423}
{7361DB16-D534-4E0E-8597-BE22317DEF47} = {794D8C68-BF6F-49C8-BCA5-AA52D8F45EF4}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
Expand Down
1 change: 1 addition & 0 deletions src/Persistence.Tests/Persistence.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
<None Include="_TestData\**\*">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>

<None Include="..\schemas-tests\pa-yaml\v3.0\Examples\**\*.yaml" LinkBase="_TestData\SchemaV3_0\Examples\">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
Expand Down
20 changes: 20 additions & 0 deletions src/Persistence.Tests/YamlValidator/ValidatorFactoryTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using Microsoft.PowerPlatform.PowerApps.Persistence.YamlValidator;

namespace Persistence.Tests.YamlValidator;

[TestClass]
public class ValidatorFactoryTest
{
[TestMethod]
public void GetValidatorTest()
{
var factory = new ValidatorFactory();
var validator = factory.GetValidator();

Assert.IsNotNull(validator);
Assert.IsInstanceOfType(validator, typeof(Validator));
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using Json.Schema;
using Microsoft.PowerPlatform.PowerApps.Persistence.YamlValidator;

namespace Persistence.Tests.YamlValidator;
Expand All @@ -10,21 +9,18 @@ namespace Persistence.Tests.YamlValidator;
public class ValidatorTest
{

private static readonly string _validPath = Path.Combine(".", "_TestData", "ValidYaml") +
private static readonly string _validPath = Path.Combine(".", "_TestData", "ValidatorTests", "ValidYaml") +
Path.DirectorySeparatorChar;

private static readonly string _invalidPath = Path.Combine(".", "_TestData", "InvalidYaml") +
private static readonly string _invalidPath = Path.Combine(".", "_TestData", "ValidatorTests", "InvalidYaml") +
Path.DirectorySeparatorChar;

private readonly JsonSchema _schema;
private readonly Validator _yamlValidator;

public ValidatorTest()
{
var schemaFileLoader = new SchemaLoader();
_schema = schemaFileLoader.Load(Constants.DefaultSchemaPath);
var resultVerbosity = new VerbosityData(Constants.Verbose);
_yamlValidator = new Validator(resultVerbosity.EvalOptions, resultVerbosity.JsonOutputOptions);
var validatorFactory = new ValidatorFactory();
_yamlValidator = validatorFactory.GetValidator();
}

[TestMethod]
Expand All @@ -34,8 +30,8 @@ public ValidatorTest()

public void TestValidationValidYaml(string filename)
{
var rawYaml = Utility.ReadFileData($@"{_validPath}{filename}");
var result = _yamlValidator.Validate(_schema, rawYaml);
var rawYaml = File.ReadAllText($@"{_validPath}{filename}");
var result = _yamlValidator.Validate(rawYaml);
Assert.IsTrue(result.SchemaValid);
}

Expand All @@ -48,10 +44,11 @@ public void TestValidationValidYaml(string filename)
[DataRow("EmptyArray.yaml")]
[DataRow("Empty.yaml")]
[DataRow("NamelessObjectNoControl.yaml")]
[DataRow("NotYaml.yaml")]
public void TestValidationInvalidYaml(string filename)
{
var rawYaml = Utility.ReadFileData($@"{_invalidPath}{filename}");
var result = _yamlValidator.Validate(_schema, rawYaml);
var rawYaml = File.ReadAllText($@"{_invalidPath}{filename}");
var result = _yamlValidator.Validate(rawYaml);
Assert.IsFalse(result.SchemaValid);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
features => [
{
name => lorem ipsum,
points => [
"bullet 1",
"bullet 2"
]
},
{
name => lorem ipsum 2,
description => lorem ipsum 3
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@
<PublicSign>true</PublicSign>
<!-- Workaround for version range https://github.com/NuGet/Home/issues/11842 -->
<RestorePackagesWithLockFile>true</RestorePackagesWithLockFile>
<NoWarn>$(NoWarn);NU1601</NoWarn>
<!-- YamlValidator Supressions: CA1822, IDE0052 (unused json serializer) in YamlValidator -->
<NoWarn>$(NoWarn);NU1601;CA1822;IDE0052</NoWarn>
abaskk-msft marked this conversation as resolved.
Show resolved Hide resolved
</PropertyGroup>

<ItemGroup Label="Global usings">
Expand All @@ -43,10 +44,20 @@
<PackageReference Include="Microsoft.PowerFx.Core" Version="1.2.0" />
<PackageReference Include="YamlDotNet" Version="$(YamlDotNetVersion)" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="$(MicrosoftExtensionsLoggingVersion)" />
<PackageReference Include="JsonSchema.Net" Version="7.1.0" />
<PackageReference Include="Yaml2JsonNode" Version="2.1.0" />
</ItemGroup>

<!-- Link schemas and build them into the assembly for the YamlValidator -->
<ItemGroup>
<EmbeddedResource Include="..\..\docs\**\*.json" >
<Link>YamlValidator\schema\%(RecursiveDir)%(Filename)%(Extension)</Link>
</EmbeddedResource>
</ItemGroup>


<ItemGroup>
<InternalsVisibleTo Include="Persistence.Tests" Key="0024000004800000940000000602000000240000525341310004000001000100b5fc90e7027f67871e773a8fde8938c81dd402ba65b9201d60593e96c492651e889cc13f1415ebb53fac1131ae0bd333c5ee6021672d9718ea31a8aebd0da0072f25d87dba6fc90ffd598ed4da35e44c398c454307e8e33b8426143daec9f596836f97c8f74750e5975c64e2189f45def46b2a2b1247adc3652bf5c308055da9" />
abaskk-msft marked this conversation as resolved.
Show resolved Hide resolved
</ItemGroup>

</Project>
</Project>
15 changes: 15 additions & 0 deletions src/Persistence/YamlValidator/Constants.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

namespace Microsoft.PowerPlatform.PowerApps.Persistence.YamlValidator;

public static class Constants
{
public const string YamlFileExtension = ".pa.yaml";
public const string JsonFileExtension = ".json";

public const string notYamlError = "File is not YAML";
public const string emptyYamlError = "Empty YAML file";

public const string subNamespace = "YamlValidator";
}
49 changes: 49 additions & 0 deletions src/Persistence/YamlValidator/SchemaLoader.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using System.Reflection;
using Json.Schema;

namespace Microsoft.PowerPlatform.PowerApps.Persistence.YamlValidator;

public class SchemaLoader
{
private const string _schemaFolderPath = "schema";
private const string _subschemaFolderPath = "subschemas";

public JsonSchema Load()
{
var assembly = Assembly.GetExecutingAssembly();
abaskk-msft marked this conversation as resolved.
Show resolved Hide resolved
JsonSchema? node = null;
foreach (var file in assembly.GetManifestResourceNames())
{
abaskk-msft marked this conversation as resolved.
Show resolved Hide resolved
var fileStream = assembly.GetManifestResourceStream(file);
var assemblyName = Assembly.GetExecutingAssembly().GetName().Name;
if (fileStream == null)
{
throw new IOException($"Resource {file} could not found in assembly {assemblyName}");
}
using var streamReader = new StreamReader(fileStream);
var jsonSchemaString = streamReader.ReadToEnd();
var schema = JsonSchema.FromText(jsonSchemaString);

// assembly name is Microsoft.PowerPlatform.PowerApps.Persistence
// subNamespace is YamlValidator, schemas live in the linked schema folder
var rootFileName = $"{assemblyName}.{Constants.subNamespace}.{_schemaFolderPath}";

if (file.StartsWith($"{rootFileName}.{_subschemaFolderPath}.", StringComparison.Ordinal))
{
// these virtual uri's are used to resolve $ref's in the schema, they aren't
// represented like this in the dll
schema.BaseUri = new Uri($"file://{_schemaFolderPath}/{_subschemaFolderPath}/");
SchemaRegistry.Global.Register(schema);
continue;
}
schema.BaseUri = new Uri($"file://{_schemaFolderPath}");
node = schema;
}
if (node == null)
{
throw new InvalidDataException("Schema was not able to be read into memory");
}
return node;
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,6 @@ namespace Microsoft.PowerPlatform.PowerApps.Persistence.YamlValidator;

public class Utility
{
public static string ReadFileData(string filePath)
{
var yamlData = File.ReadAllText(filePath);
return yamlData;
}

public static YamlStream MakeYamlStream(string yamlString)
{
var stream = new YamlStream();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,40 +4,45 @@
using Json.Schema;
using Yaml2JsonNode;
using System.Text.Json;
using YamlDotNet.Core;
abaskk-msft marked this conversation as resolved.
Show resolved Hide resolved
using YamlDotNet.RepresentationModel;

namespace Microsoft.PowerPlatform.PowerApps.Persistence.YamlValidator;

public class Validator
{
private readonly EvaluationOptions _verbosityOptions;
private readonly JsonSerializerOptions _serializerOptions;

public Validator(EvaluationOptions options, JsonSerializerOptions resultSerializeOptions)
private readonly JsonSchema _schema;
public Validator(EvaluationOptions options, JsonSerializerOptions resultSerializeOptions, JsonSchema schema)
{
// to do: add verbosity flag and allow users to choose verbosity of evaluation
_verbosityOptions = options;
_serializerOptions = resultSerializeOptions;
_schema = schema;
}

public ValidatorResults Validate(JsonSchema schema, string yamlFileData)
public ValidatorResults Validate(string yamlFileData)
{
var yamlStream = Utility.MakeYamlStream(yamlFileData);
YamlStream yamlStream;
try
{
yamlStream = Utility.MakeYamlStream(yamlFileData);
}
catch (YamlException)
{
return new ValidatorResults(false, new List<ValidatorError> { new(Constants.notYamlError) });
}

var jsonData = yamlStream.Documents.Count > 0 ? yamlStream.Documents[0].ToJsonNode() : null;

// here we say that empty yaml is serialized as null json
if (jsonData == null)
{
return new ValidatorResults(false, new List<ValidatorError> { new("Empty YAML file") });
return new ValidatorResults(false, new List<ValidatorError> { new(Constants.emptyYamlError) });
}
var results = schema.Evaluate(jsonData, _verbosityOptions);

// not used but may help if we ever need to serialize the evaluation results into json format to feed into
// a VSCode extension or other tool
var output = JsonSerializer.Serialize(results, _serializerOptions);
var results = _schema.Evaluate(jsonData, _verbosityOptions);

var schemaValidity = results.IsValid;
// TBD: filter actual errors versus false positives
// we look for errors that are not valid, have errors, and have an instance location (i.e are not oneOf errors)
var yamlValidatorErrors = new List<ValidatorError>();
if (!schemaValidity)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Json.Schema;

namespace Microsoft.PowerPlatform.PowerApps.Persistence.YamlValidator;

public class ValidatorError
{
public string InstanceLocation { get; }
Expand Down
Loading
Loading