PathHelpers.cs 899 B

12345678910111213141516171819202122232425262728293031
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT license.
  3. using System;
  4. using Microsoft.AspNetCore.JsonPatch.Exceptions;
  5. namespace Microsoft.AspNetCore.JsonPatch.Internal;
  6. internal static class PathHelpers
  7. {
  8. internal static string ValidateAndNormalizePath(string path)
  9. {
  10. // check for most common path errors on create. This is not
  11. // absolutely necessary, but it allows us to already catch mistakes
  12. // on creation of the patch document rather than on execute.
  13. if (path.Contains("//"))
  14. {
  15. throw new JsonPatchException(Resources.FormatInvalidValueForPath(path), null);
  16. }
  17. if (!path.StartsWith("/", StringComparison.Ordinal))
  18. {
  19. return "/" + path;
  20. }
  21. else
  22. {
  23. return path;
  24. }
  25. }
  26. }