Created
January 11, 2024 17:26
-
-
Save Grantimus9/15712cbb9f99044eb49af5a87b7e77b8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# example custom_data with 2 keys; one the changeset and the other the manually jsonified errors key. | |
{ | |
"chgset": { | |
"action": "update", | |
"changes": { | |
"dataimport_id": 423 | |
}, | |
"constraints": [], | |
"data": { | |
"__meta__": { | |
"context": null, | |
"prefix": null, | |
"schema": "Elixir.Charityapi.Core.Organization", | |
"source": "core_organizations", | |
"state": "loaded" | |
}, | |
"acct_pd": 4, | |
"activity": 1005007, | |
"affiliation": 3, | |
"asset_amt": 0, | |
"asset_cd": 0, | |
"city": "BRANFORD", | |
"classification": 7000, | |
"dataimport": { | |
"__cardinality__": "one", | |
"__field__": "dataimport", | |
"__owner__": "Elixir.Charityapi.Core.Organization" | |
}, | |
"dataimport_id": 422, | |
"deductibility": 1, | |
"ein": "061005703", | |
"filing_req_cd": 6, | |
"first_seen_date": { | |
"calendar": "Elixir.Calendar.ISO", | |
"day": 19, | |
"month": 11, | |
"year": 2023 | |
}, | |
"foundation": 10, | |
"group": 0, | |
"has_website": true, | |
"ico": null, | |
"id": 304469, | |
"income_amt": 0, | |
"income_cd": 0, | |
"inserted_at": { | |
"calendar": "Elixir.Calendar.ISO", | |
"day": 10, | |
"hour": 5, | |
"microsecond": [ | |
0, | |
0 | |
], | |
"minute": 48, | |
"month": 1, | |
"second": 4, | |
"year": 2018 | |
}, | |
"last_indexed": { | |
"calendar": "Elixir.Calendar.ISO", | |
"day": 28, | |
"hour": 6, | |
"microsecond": [ | |
0, | |
0 | |
], | |
"minute": 5, | |
"month": 11, | |
"second": 16, | |
"year": 2023 | |
}, | |
"name": "CALVARY BIBLE FELLOWSHIP", | |
"ntee_cd": null, | |
"ntee_cd_definition": null, | |
"ntee_cd_title": null, | |
"organization": 5, | |
"pf_filing_req_cd": 0, | |
"revenue_amt": 0, | |
"ruling": 197909, | |
"searchable_document": [ | |
{ | |
"positions": [], | |
"word": "061005703" | |
}, | |
{ | |
"positions": [], | |
"word": "bibl" | |
}, | |
{ | |
"positions": [], | |
"word": "calvari" | |
}, | |
{ | |
"positions": [], | |
"word": "fellowship" | |
} | |
], | |
"searchable_fields": "calvary bible fellowship 061005703", | |
"sort_name": null, | |
"state": "CT", | |
"status": 1, | |
"street": "12 WILDWOOD DR", | |
"subsection": 3, | |
"tax_period": 0, | |
"updated_at": { | |
"calendar": "Elixir.Calendar.ISO", | |
"day": 28, | |
"hour": 6, | |
"microsecond": [ | |
0, | |
0 | |
], | |
"minute": 5, | |
"month": 11, | |
"second": 16, | |
"year": 2023 | |
}, | |
"website_url": null, | |
"website_url_last_updated": null, | |
"zip": "06405-3935" | |
}, | |
"empty_values": [ | |
"" | |
], | |
"errors": [], | |
"filters": {}, | |
"params": { | |
"__meta__": { | |
"context": null, | |
"prefix": null, | |
"schema": "Elixir.Charityapi.Core.Organization", | |
"source": "core_organizations", | |
"state": "built" | |
}, | |
"acct_pd": 4, | |
"activity": 1005007, | |
"affiliation": 3, | |
"asset_amt": 0, | |
"asset_cd": 0, | |
"city": "BRANFORD", | |
"classification": 7000, | |
"dataimport": { | |
"__cardinality__": "one", | |
"__field__": "dataimport", | |
"__owner__": "Elixir.Charityapi.Core.Organization" | |
}, | |
"dataimport_id": 423, | |
"deductibility": 1, | |
"ein": "061005703", | |
"filing_req_cd": 6, | |
"first_seen_date": null, | |
"foundation": 10, | |
"group": 0, | |
"has_website": true, | |
"ico": "", | |
"id": null, | |
"income_amt": 0, | |
"income_cd": 0, | |
"inserted_at": null, | |
"last_indexed": null, | |
"name": "CALVARY BIBLE FELLOWSHIP", | |
"ntee_cd": "", | |
"ntee_cd_definition": null, | |
"ntee_cd_title": null, | |
"organization": 5, | |
"pf_filing_req_cd": 0, | |
"revenue_amt": 0, | |
"ruling": 197909, | |
"searchable_document": null, | |
"searchable_fields": "", | |
"sort_name": "", | |
"state": "CT", | |
"status": 1, | |
"street": "12 WILDWOOD DR", | |
"subsection": 3, | |
"tax_period": 0, | |
"updated_at": null, | |
"website_url": null, | |
"website_url_last_updated": null, | |
"zip": "06405-3935" | |
}, | |
"prepare": [], | |
"repo": "Elixir.Charityapi.Repo", | |
"repo_opts": [], | |
"required": [ | |
"ein", | |
"name", | |
"dataimport_id", | |
"first_seen_date" | |
], | |
"types": { | |
"acct_pd": "integer", | |
"activity": "integer", | |
"affiliation": "integer", | |
"asset_amt": "integer", | |
"asset_cd": "integer", | |
"city": "string", | |
"classification": "integer", | |
"dataimport": [ | |
"assoc", | |
{ | |
"cardinality": "one", | |
"defaults": [], | |
"field": "dataimport", | |
"on_cast": null, | |
"on_replace": "raise", | |
"ordered": false, | |
"owner": "Elixir.Charityapi.Core.Organization", | |
"owner_key": "dataimport_id", | |
"queryable": "Elixir.Charityapi.Core.Dataimport", | |
"related": "Elixir.Charityapi.Core.Dataimport", | |
"related_key": "id", | |
"relationship": "parent", | |
"unique": true, | |
"where": [] | |
} | |
], | |
"dataimport_id": "id", | |
"deductibility": "integer", | |
"ein": "string", | |
"filing_req_cd": "integer", | |
"first_seen_date": "date", | |
"foundation": "integer", | |
"group": "integer", | |
"has_website": "boolean", | |
"ico": "string", | |
"id": "id", | |
"income_amt": "integer", | |
"income_cd": "integer", | |
"inserted_at": "naive_datetime", | |
"last_indexed": "naive_datetime", | |
"name": "string", | |
"ntee_cd": "string", | |
"ntee_cd_definition": "string", | |
"ntee_cd_title": "string", | |
"organization": "integer", | |
"pf_filing_req_cd": "integer", | |
"revenue_amt": "integer", | |
"ruling": "integer", | |
"searchable_document": "Elixir.Charityapi.Ecto.TSVectorType", | |
"searchable_fields": "string", | |
"sort_name": "string", | |
"state": "string", | |
"status": "integer", | |
"street": "string", | |
"subsection": "integer", | |
"tax_period": "integer", | |
"updated_at": "naive_datetime", | |
"website_url": "string", | |
"website_url_last_updated": "naive_datetime", | |
"zip": "string" | |
}, | |
"valid?": false, | |
"validations": [] | |
}, | |
"errors_json": { | |
"errors": { | |
"first_seen_date": [ | |
"can't be blank" | |
] | |
} | |
} | |
} | |
# Functions to convert changeset errors to json: | |
@doc """ | |
Renders changeset errors. | |
""" | |
def error(%{changeset: changeset}) do | |
# When encoded, the changeset returns its errors | |
# as a JSON object. So we just pass it forward. | |
%{errors: Ecto.Changeset.traverse_errors(changeset, &translate_error/1)} | |
end | |
defp translate_error({msg, opts}) do | |
# You can make use of gettext to translate error messages by | |
# uncommenting and adjusting the following code: | |
# if count = opts[:count] do | |
# Gettext.dngettext(CharityapiWeb.Gettext, "errors", msg, msg, count, opts) | |
# else | |
# Gettext.dgettext(CharityapiWeb.Gettext, "errors", msg, opts) | |
# end | |
Enum.reduce(opts, msg, fn {key, value}, acc -> | |
String.replace(acc, "%{#{key}}", fn _ -> to_string(value) end) | |
end) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment