|
| 1 | +defmodule ValueFormatters.Schemas do |
| 2 | + if Code.ensure_loaded?(JSV) do |
| 3 | + def format do |
| 4 | + %{ |
| 5 | + type: :object, |
| 6 | + description: "Formats for value formatting", |
| 7 | + properties: %{ |
| 8 | + oneOf: [ |
| 9 | + %{ |
| 10 | + type: :string, |
| 11 | + description: "A shorthand representation of the format", |
| 12 | + enum: [ |
| 13 | + "number", |
| 14 | + "string", |
| 15 | + "date", |
| 16 | + "date_relative", |
| 17 | + "date_iso", |
| 18 | + "date_unix", |
| 19 | + "coordinates" |
| 20 | + ] |
| 21 | + }, |
| 22 | + available_formats_schema() |
| 23 | + ] |
| 24 | + } |
| 25 | + } |
| 26 | + end |
| 27 | + |
| 28 | + def default_formats() do |
| 29 | + %{ |
| 30 | + type: :object, |
| 31 | + description: "Default formats for value formatting", |
| 32 | + properties: available_formats_schema() |
| 33 | + } |
| 34 | + end |
| 35 | + |
| 36 | + defp available_formats_schema() do |
| 37 | + %{ |
| 38 | + number: %{ |
| 39 | + type: :object, |
| 40 | + description: |
| 41 | + "Use to display numeric values and format them according to the user's locale.", |
| 42 | + properties: %{ |
| 43 | + precision: %{type: :number, description: "Number of decimal places"}, |
| 44 | + unit: %{ |
| 45 | + type: :string, |
| 46 | + description: "If set, the formatter appends ' ' + unit to the display value" |
| 47 | + } |
| 48 | + } |
| 49 | + }, |
| 50 | + string: %{ |
| 51 | + type: :object, |
| 52 | + description: |
| 53 | + "Use to explicitly disable any kind of formatting that would otherwise take place.", |
| 54 | + properties: %{} |
| 55 | + }, |
| 56 | + date: %{ |
| 57 | + type: :object, |
| 58 | + description: |
| 59 | + "Use to to display date-time values and format them according to the user's locale.", |
| 60 | + properties: %{ |
| 61 | + date_display: %{ |
| 62 | + type: :string, |
| 63 | + description: """ |
| 64 | + How the formatter should display the date portion: |
| 65 | +
|
| 66 | + - `full`: Wednesday, November 29, 2023 |
| 67 | +
|
| 68 | + - `long`: November 29, 2023 |
| 69 | +
|
| 70 | + - `medium`: Nov 29, 2023 |
| 71 | +
|
| 72 | + - `short`: 11/29/23 |
| 73 | +
|
| 74 | + - `none`: Don't display date |
| 75 | + """, |
| 76 | + enum: ["full", "long", "medium", "short", "none"], |
| 77 | + default: "medium" |
| 78 | + }, |
| 79 | + time_display: %{ |
| 80 | + type: :string, |
| 81 | + description: """ |
| 82 | + How the formatter should display the time portion: |
| 83 | +
|
| 84 | + - `full`: 3:44:28 PM GMT |
| 85 | +
|
| 86 | + - `long`: 3:44:28 PM UTC |
| 87 | +
|
| 88 | + - `medium`: 3:44:28 PM |
| 89 | +
|
| 90 | + - `short`: 3:44 PM |
| 91 | +
|
| 92 | + - `none`: Don't display time |
| 93 | + """, |
| 94 | + enum: ["full", "long", "medium", "short", "none"], |
| 95 | + default: "medium" |
| 96 | + } |
| 97 | + } |
| 98 | + }, |
| 99 | + date_relative: %{ |
| 100 | + type: :object, |
| 101 | + description: """ |
| 102 | + Use format: "date_relative" to display a relative date string (e.g. “2 days ago”) by comparing the given value against the current date & time. Only the largest sensible unit is displayed, e.g. the formatter will only display “days” even when other components such as hours, minutes etc. aren't equal to zero. |
| 103 | +
|
| 104 | + The implementation can choose to update the displayed value in appropriate intervals. Also, it can choose to display the absolute date on user interaction, e.g. in a tooltip. |
| 105 | +
|
| 106 | + This format currently doesn't support any options. |
| 107 | + """, |
| 108 | + properties: %{} |
| 109 | + }, |
| 110 | + date_iso: %{ |
| 111 | + type: :object, |
| 112 | + description: "Use to display date-time values in ISO 8601 extended format.", |
| 113 | + properties: %{} |
| 114 | + }, |
| 115 | + date_unix: %{ |
| 116 | + type: :object, |
| 117 | + description: "Use to display date-time values in seconds since unix epoch.", |
| 118 | + properties: %{ |
| 119 | + milliseconds: %{ |
| 120 | + type: :boolean, |
| 121 | + default: false, |
| 122 | + description: |
| 123 | + "Whether the formatter should output the values milliseconds (instead of seconds)." |
| 124 | + } |
| 125 | + } |
| 126 | + }, |
| 127 | + coordinates: %{ |
| 128 | + type: :object, |
| 129 | + description: "Use to display latitude & longitude information.", |
| 130 | + properties: %{ |
| 131 | + radius_display: %{ |
| 132 | + type: :boolean, |
| 133 | + default: true, |
| 134 | + description: |
| 135 | + "Whether the formatter should include the radius/accuracy information (if present)." |
| 136 | + } |
| 137 | + } |
| 138 | + } |
| 139 | + } |
| 140 | + end |
| 141 | + else |
| 142 | + def schema do |
| 143 | + raise "JSV is not available. Please add it to your dependencies." |
| 144 | + end |
| 145 | + end |
| 146 | +end |
0 commit comments