Skip to content

Commit

Permalink
Support "constraints" with patch extends. (#103)
Browse files Browse the repository at this point in the history
Fix bug in validator2 where it incorrectly tries to return a compile regex instead of the original regex string, resulting in a JSON encoding error.

Fix runtime warning when loading dictionary page: change "conn.params()" to "conn.params" (use of parenthesis with struct .

Update Docker Elixir image to 1.17.2.

Minor dependency update: Jason library updated to 1.4.4.

Bump version to 2.72.0.
  • Loading branch information
rmouritzen-splunk authored Jul 31, 2024
1 parent 5d3527b commit 206a113
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 24 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ARG elixir_image=elixir:1.17.1-alpine
ARG elixir_image=elixir:1.17.2-alpine

FROM ${elixir_image} AS builder

Expand Down
5 changes: 5 additions & 0 deletions lib/schema.ex
Original file line number Diff line number Diff line change
Expand Up @@ -614,6 +614,11 @@ defmodule Schema do
Map.delete(data, :_links)
end

@spec deep_clean(map()) :: map()
def deep_clean(data) do
reduce_attributes(data)
end

def apply_profiles(types, _profiles, 0) do
Enum.into(types, %{}, fn {name, type} ->
remove_profiles(name, type)
Expand Down
29 changes: 21 additions & 8 deletions lib/schema/cache.ex
Original file line number Diff line number Diff line change
Expand Up @@ -978,14 +978,13 @@ defmodule Schema.Cache do
base
|> Map.put(:profiles, profiles)
|> Map.put(:attributes, attributes)

# Top-level observable.
# Only occurs in objects, but is safe to do for classes too.
patched_base = Utils.put_non_nil(patched_base, :observable, item[:observable])

# Top-level path-based observables.
# Only occurs in classes, but is safe to do for objects too.
patched_base = Utils.put_non_nil(patched_base, :observables, item[:observables])
# Top-level observable.
# Only occurs in objects, but is safe to do for classes too.
|> Utils.put_non_nil(:observable, item[:observable])
# Top-level path-based observables.
# Only occurs in classes, but is safe to do for objects too.
|> Utils.put_non_nil(:observables, item[:observables])
|> patch_constraints(item)

Map.put(acc, base_key, patched_base)
end
Expand All @@ -1005,6 +1004,20 @@ defmodule Schema.Cache do
patch_name(item) == item[:extends]
end

defp patch_constraints(base, item) do
if Map.has_key?(item, :constraints) do
constraints = item[:constraints]

if constraints != nil and !Enum.empty?(constraints) do
Map.put(base, :constraints, constraints)
else
Map.delete(base, :constraints)
end
else
base
end
end

defp resolve_extends(items) do
Enum.map(items, fn {item_key, item} -> {item_key, resolve_extends(items, item)} end)
end
Expand Down
22 changes: 11 additions & 11 deletions lib/schema/validator2.ex
Original file line number Diff line number Diff line change
Expand Up @@ -1591,11 +1591,11 @@ defmodule Schema.Validator2 do
cond do
Map.has_key?(dictionary_type, :regex) ->
# This is a primitive type or subtype with a range
pattern = dictionary_type[:regex]
regex = dictionary_type[:regex]

case Regex.compile(pattern) do
{:ok, regex} ->
if Regex.match?(regex, value) do
case Regex.compile(regex) do
{:ok, compiled_regex} ->
if Regex.match?(compiled_regex, value) do
response
else
add_error(
Expand Down Expand Up @@ -1628,7 +1628,7 @@ defmodule Schema.Validator2 do
attribute_path: attribute_path,
attribute: attribute_name,
type: attribute_type_key,
regex: pattern,
regex: regex,
regex_error_message: to_string(message),
regex_error_position: position
}
Expand All @@ -1641,11 +1641,11 @@ defmodule Schema.Validator2 do
super_type = dictionary_types[super_type_key]

if Map.has_key?(super_type, :regex) do
pattern = dictionary_type[:regex]
regex = dictionary_type[:regex]

case Regex.compile(pattern) do
{:ok, regex} ->
if Regex.match?(regex, value) do
case Regex.compile(regex) do
{:ok, compiled_regex} ->
if Regex.match?(compiled_regex, value) do
response
else
add_error(
Expand All @@ -1658,7 +1658,7 @@ defmodule Schema.Validator2 do
attribute: attribute_name,
super_type: super_type_key,
type: attribute_type_key,
regex: pattern,
regex: regex,
value: value
}
)
Expand All @@ -1681,7 +1681,7 @@ defmodule Schema.Validator2 do
attribute_path: attribute_path,
attribute: attribute_name,
type: super_type_key,
regex: pattern,
regex: regex,
regex_error_message: to_string(message),
regex_error_position: position
}
Expand Down
4 changes: 2 additions & 2 deletions lib/schema_web/views/page_view.ex
Original file line number Diff line number Diff line change
Expand Up @@ -646,7 +646,7 @@ defmodule SchemaWeb.PageView do
defp dictionary_links_class_to_html(_, _, nil), do: []

defp dictionary_links_class_to_html(conn, attribute_name, linked_classes) do
classes = SchemaController.classes(conn.params())
classes = SchemaController.classes(conn.params)
all_classes = Schema.all_classes()
attribute_key = Schema.Utils.descope_to_uid(attribute_name)

Expand Down Expand Up @@ -735,7 +735,7 @@ defmodule SchemaWeb.PageView do
defp dictionary_links_class_updated_to_html(_, _, nil), do: []

defp dictionary_links_class_updated_to_html(conn, attribute_name, linked_classes) do
classes = SchemaController.classes(conn.params())
classes = SchemaController.classes(conn.params)
all_classes = Schema.all_classes()
attribute_key = Schema.Utils.descope_to_uid(attribute_name)

Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
defmodule Schema.MixProject do
use Mix.Project

@version "2.71.1"
@version "2.72.0"

def project do
build = System.get_env("GITHUB_RUN_NUMBER") || "SNAPSHOT"
Expand Down
2 changes: 1 addition & 1 deletion mix.lock
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"ex_json_schema": {:hex, :ex_json_schema, "0.7.4", "09eb5b0c8184e5702bc89625a9d0c05c7a0a845d382e9f6f406a0fc1c9a8cc3f", [:mix], [], "hexpm", "45c67fa840f0d719a2b5578126dc29bcdc1f92499c0f61bcb8a3bcb5935f9684"},
"file_system": {:hex, :file_system, "1.0.0", "b689cc7dcee665f774de94b5a832e578bd7963c8e637ef940cd44327db7de2cd", [:mix], [], "hexpm", "6752092d66aec5a10e662aefeed8ddb9531d79db0bc145bb8c40325ca1d8536d"},
"html_entities": {:hex, :html_entities, "0.5.2", "9e47e70598da7de2a9ff6af8758399251db6dbb7eebe2b013f2bbd2515895c3c", [:mix], [], "hexpm", "c53ba390403485615623b9531e97696f076ed415e8d8058b1dbaa28181f4fdcc"},
"jason": {:hex, :jason, "1.4.3", "d3f984eeb96fe53b85d20e0b049f03e57d075b5acda3ac8d465c969a2536c17b", [:mix], [{:decimal, "~> 1.0 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "9a90e868927f7c777689baa16d86f4d0e086d968db5c05d917ccff6d443e58a3"},
"jason": {:hex, :jason, "1.4.4", "b9226785a9aa77b6857ca22832cffa5d5011a667207eb2a0ad56adb5db443b8a", [:mix], [{:decimal, "~> 1.0 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "c5eb0cab91f094599f94d55bc63409236a8ec69a21a67814529e8d5f6cc90b3b"},
"mime": {:hex, :mime, "2.0.6", "8f18486773d9b15f95f4f4f1e39b710045fa1de891fada4516559967276e4dc2", [:mix], [], "hexpm", "c9945363a6b26d747389aac3643f8e0e09d30499a138ad64fe8fd1d13d9b153e"},
"number": {:hex, :number, "1.0.5", "d92136f9b9382aeb50145782f116112078b3465b7be58df1f85952b8bb399b0f", [:mix], [{:decimal, "~> 1.5 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: false]}], "hexpm", "c0733a0a90773a66582b9e92a3f01290987f395c972cb7d685f51dd927cd5169"},
"phoenix": {:hex, :phoenix, "1.7.14", "a7d0b3f1bc95987044ddada111e77bd7f75646a08518942c72a8440278ae7825", [:mix], [{:castore, ">= 0.0.0", [hex: :castore, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:phoenix_pubsub, "~> 2.1", [hex: :phoenix_pubsub, repo: "hexpm", optional: false]}, {:phoenix_template, "~> 1.0", [hex: :phoenix_template, repo: "hexpm", optional: false]}, {:phoenix_view, "~> 2.0", [hex: :phoenix_view, repo: "hexpm", optional: true]}, {:plug, "~> 1.14", [hex: :plug, repo: "hexpm", optional: false]}, {:plug_cowboy, "~> 2.7", [hex: :plug_cowboy, repo: "hexpm", optional: true]}, {:plug_crypto, "~> 1.2 or ~> 2.0", [hex: :plug_crypto, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}, {:websock_adapter, "~> 0.5.3", [hex: :websock_adapter, repo: "hexpm", optional: false]}], "hexpm", "c7859bc56cc5dfef19ecfc240775dae358cbaa530231118a9e014df392ace61a"},
Expand Down

0 comments on commit 206a113

Please sign in to comment.