|
| 1 | +package org.entangled.schema; |
| 2 | + |
| 3 | +import java.util.HashSet; |
| 4 | +import java.util.List; |
| 5 | +import java.util.Set; |
| 6 | +import org.entangled.DiagnosticCode; |
| 7 | +import org.entangled.RejectException; |
| 8 | +import org.entangled.json.JsonValue; |
| 9 | + |
| 10 | +/** |
| 11 | + * Block-grammar validation for the eleven block kinds, section 03. |
| 12 | + * |
| 13 | + * <p>Each block is validated against the closed schema for its declared |
| 14 | + * {@code kind}; an unknown kind is {@code E_SCHEMA_ENUM_VIOLATION}. A |
| 15 | + * {@code submit_form} block in a transaction document is |
| 16 | + * {@code E_SCHEMA_BLOCK_NOT_PERMITTED}. Inline content is validated by |
| 17 | + * {@link Inline}; per-block aggregate byte caps are enforced here. |
| 18 | + */ |
| 19 | +public final class Blocks { |
| 20 | + |
| 21 | + private static final Set<String> KNOWN_KINDS = Set.of( |
| 22 | + "paragraph", "heading", "code_block", "quote", "list", "divider", |
| 23 | + "image", "link", "submit_form", "feedback", "note"); |
| 24 | + |
| 25 | + private static final Set<String> FEEDBACK_VARIANTS = Set.of("success", "info", "warning", "error"); |
| 26 | + private static final Set<String> NOTE_VARIANTS = Set.of("info", "warning", "danger", "success"); |
| 27 | + private static final Set<String> MEDIA_TYPES = Set.of("image/png", "image/jpeg", "image/webp"); |
| 28 | + private static final Set<String> FIELD_KINDS = Set.of("text", "textarea", "select", "checkbox"); |
| 29 | + |
| 30 | + private Blocks() { |
| 31 | + } |
| 32 | + |
| 33 | + /** Validate one block. {@code transaction} selects document-kind permission rules. */ |
| 34 | + public static void validate(JsonValue blockValue, boolean transaction) { |
| 35 | + JsonValue.Obj b = Fields.obj(blockValue); |
| 36 | + String kind = Fields.str(Inline.require(b, "kind")); |
| 37 | + if (!KNOWN_KINDS.contains(kind)) { |
| 38 | + throw new RejectException(DiagnosticCode.E_SCHEMA_ENUM_VIOLATION); |
| 39 | + } |
| 40 | + if (transaction && kind.equals("submit_form")) { |
| 41 | + throw new RejectException(DiagnosticCode.E_SCHEMA_BLOCK_NOT_PERMITTED); |
| 42 | + } |
| 43 | + switch (kind) { |
| 44 | + case "paragraph" -> paragraph(b); |
| 45 | + case "heading" -> heading(b); |
| 46 | + case "code_block" -> codeBlock(b); |
| 47 | + case "quote" -> quote(b); |
| 48 | + case "list" -> list(b); |
| 49 | + case "divider" -> divider(b); |
| 50 | + case "image" -> image(b); |
| 51 | + case "link" -> link(b); |
| 52 | + case "submit_form" -> submitForm(b); |
| 53 | + case "feedback" -> feedback(b); |
| 54 | + case "note" -> note(b); |
| 55 | + default -> throw new RejectException(DiagnosticCode.E_SCHEMA_ENUM_VIOLATION); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + private static void paragraph(JsonValue.Obj b) { |
| 60 | + Closed.check(b, Set.of("kind", "content"), Set.of("kind", "content")); |
| 61 | + int bytes = Inline.validate(b.get("content"), true, true); |
| 62 | + cap(bytes, 8 * 1024); |
| 63 | + } |
| 64 | + |
| 65 | + private static void heading(JsonValue.Obj b) { |
| 66 | + Closed.check(b, Set.of("kind", "level", "content"), Set.of("kind", "level", "content")); |
| 67 | + Fields.integerInRange(b.get("level"), 1, 6); |
| 68 | + int bytes = Inline.validate(b.get("content"), true, true); |
| 69 | + cap(bytes, 200); |
| 70 | + } |
| 71 | + |
| 72 | + private static void codeBlock(JsonValue.Obj b) { |
| 73 | + Closed.check(b, Set.of("kind", "language", "content"), Set.of("kind", "language", "content")); |
| 74 | + slugLanguage(Fields.str(b.get("language"))); |
| 75 | + String content = Fields.str(b.get("content")); |
| 76 | + if (Fields.utf8Len(content) > 32 * 1024) { |
| 77 | + throw new RejectException(DiagnosticCode.E_SCHEMA_FIELD_LENGTH); |
| 78 | + } |
| 79 | + // Control chars other than line feed are forbidden; NFC required (section 04). |
| 80 | + Fields.noControlChars(content, true); |
| 81 | + Fields.requireNfc(content); |
| 82 | + } |
| 83 | + |
| 84 | + private static void quote(JsonValue.Obj b) { |
| 85 | + Closed.check(b, Set.of("kind", "content", "attribution"), Set.of("kind", "content")); |
| 86 | + int bytes = Inline.validate(b.get("content"), true, true); |
| 87 | + cap(bytes, 4 * 1024); |
| 88 | + if (b.has("attribution")) { |
| 89 | + int abytes = Inline.validate(b.get("attribution"), true, true); |
| 90 | + cap(abytes, 200); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + private static void list(JsonValue.Obj b) { |
| 95 | + Closed.check(b, Set.of("kind", "ordered", "items"), Set.of("kind", "ordered", "items")); |
| 96 | + Fields.bool(b.get("ordered")); |
| 97 | + List<JsonValue> items = Fields.arr(b.get("items")).elements(); |
| 98 | + if (items.isEmpty() || items.size() > 64) { |
| 99 | + throw new RejectException(DiagnosticCode.E_SCHEMA_FIELD_LENGTH); |
| 100 | + } |
| 101 | + int total = 0; |
| 102 | + for (JsonValue item : items) { |
| 103 | + total += Inline.validate(item, true, true); |
| 104 | + } |
| 105 | + cap(total, 8 * 1024); |
| 106 | + } |
| 107 | + |
| 108 | + private static void divider(JsonValue.Obj b) { |
| 109 | + Closed.check(b, Set.of("kind"), Set.of("kind")); |
| 110 | + } |
| 111 | + |
| 112 | + private static void image(JsonValue.Obj b) { |
| 113 | + Closed.check(b, |
| 114 | + Set.of("kind", "src", "sha256", "media_type", "width", "height", "alt", "caption"), |
| 115 | + Set.of("kind", "src", "sha256", "media_type", "width", "height", "alt")); |
| 116 | + Fields.path(Fields.str(b.get("src"))); |
| 117 | + sha256Field(Fields.str(b.get("sha256"))); |
| 118 | + Fields.inEnum(Fields.str(b.get("media_type")), MEDIA_TYPES); |
| 119 | + Fields.integerInRange(b.get("width"), 1, 4096); |
| 120 | + Fields.integerInRange(b.get("height"), 1, 4096); |
| 121 | + String alt = Fields.str(b.get("alt")); |
| 122 | + Fields.maxBytes(alt, 1024); |
| 123 | + Fields.noControlChars(alt, false); |
| 124 | + Fields.requireNfc(alt); |
| 125 | + if (b.has("caption")) { |
| 126 | + String caption = Fields.str(b.get("caption")); |
| 127 | + if (caption.isEmpty()) { |
| 128 | + // An empty caption must be omitted, not present as "". |
| 129 | + throw Fields.syntax(); |
| 130 | + } |
| 131 | + Fields.maxBytes(caption, 500); |
| 132 | + Fields.noControlChars(caption, false); |
| 133 | + Fields.requireNfc(caption); |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + private static void link(JsonValue.Obj b) { |
| 138 | + Closed.check(b, Set.of("kind", "label", "target"), Set.of("kind", "label", "target")); |
| 139 | + // link.label is inline content that MUST NOT contain link elements. |
| 140 | + int bytes = Inline.validate(b.get("label"), false, true); |
| 141 | + cap(bytes, 200); |
| 142 | + Inline.validateTarget(b.get("target")); |
| 143 | + } |
| 144 | + |
| 145 | + private static void submitForm(JsonValue.Obj b) { |
| 146 | + Closed.check(b, Set.of("kind", "label", "submit_to", "fields", "submit_label"), |
| 147 | + Set.of("kind", "label", "submit_to", "fields", "submit_label")); |
| 148 | + Inline.validate(b.get("label"), false, true); |
| 149 | + Fields.path(Fields.str(b.get("submit_to"))); |
| 150 | + List<JsonValue> fields = Fields.arr(b.get("fields")).elements(); |
| 151 | + if (fields.isEmpty() || fields.size() > 16) { |
| 152 | + throw new RejectException(DiagnosticCode.E_SCHEMA_FIELD_LENGTH); |
| 153 | + } |
| 154 | + Set<String> names = new HashSet<>(); |
| 155 | + for (JsonValue f : fields) { |
| 156 | + String name = formField(Fields.obj(f)); |
| 157 | + if (!names.add(name)) { |
| 158 | + throw new RejectException(DiagnosticCode.E_SCHEMA_DUPLICATE_ENTRY); |
| 159 | + } |
| 160 | + } |
| 161 | + String submitLabel = Fields.str(b.get("submit_label")); |
| 162 | + Fields.maxBytes(submitLabel, 100); |
| 163 | + Fields.noControlChars(submitLabel, false); |
| 164 | + } |
| 165 | + |
| 166 | + private static String formField(JsonValue.Obj f) { |
| 167 | + String kind = Fields.str(Inline.require(f, "kind")); |
| 168 | + Fields.inEnum(kind, FIELD_KINDS); |
| 169 | + String name; |
| 170 | + switch (kind) { |
| 171 | + case "text", "textarea" -> { |
| 172 | + Closed.check(f, Set.of("kind", "name", "label", "required", "max_length"), |
| 173 | + Set.of("kind", "name", "label", "required", "max_length")); |
| 174 | + name = sharedFormFields(f); |
| 175 | + Fields.integerInRange(f.get("max_length"), 1, 8192); |
| 176 | + } |
| 177 | + case "select" -> { |
| 178 | + Closed.check(f, Set.of("kind", "name", "label", "required", "options"), |
| 179 | + Set.of("kind", "name", "label", "required", "options")); |
| 180 | + name = sharedFormFields(f); |
| 181 | + selectOptions(f.get("options")); |
| 182 | + } |
| 183 | + case "checkbox" -> { |
| 184 | + Closed.check(f, Set.of("kind", "name", "label", "required"), |
| 185 | + Set.of("kind", "name", "label", "required")); |
| 186 | + name = sharedFormFields(f); |
| 187 | + } |
| 188 | + default -> throw new RejectException(DiagnosticCode.E_SCHEMA_ENUM_VIOLATION); |
| 189 | + } |
| 190 | + return name; |
| 191 | + } |
| 192 | + |
| 193 | + private static String sharedFormFields(JsonValue.Obj f) { |
| 194 | + String name = Fields.str(f.get("name")); |
| 195 | + Fields.slug(name, 64); |
| 196 | + String label = Fields.str(f.get("label")); |
| 197 | + Fields.maxBytes(label, 200); |
| 198 | + Fields.noControlChars(label, false); |
| 199 | + Fields.bool(f.get("required")); |
| 200 | + return name; |
| 201 | + } |
| 202 | + |
| 203 | + private static void selectOptions(JsonValue optionsValue) { |
| 204 | + List<JsonValue> options = Fields.arr(optionsValue).elements(); |
| 205 | + if (options.isEmpty() || options.size() > 32) { |
| 206 | + throw new RejectException(DiagnosticCode.E_SCHEMA_FIELD_LENGTH); |
| 207 | + } |
| 208 | + Set<String> values = new HashSet<>(); |
| 209 | + for (JsonValue o : options) { |
| 210 | + JsonValue.Obj opt = Fields.obj(o); |
| 211 | + Closed.check(opt, Set.of("value", "label"), Set.of("value", "label")); |
| 212 | + String value = Fields.str(opt.get("value")); |
| 213 | + Fields.slug(value, 64); |
| 214 | + if (!values.add(value)) { |
| 215 | + throw new RejectException(DiagnosticCode.E_SCHEMA_DUPLICATE_ENTRY); |
| 216 | + } |
| 217 | + String label = Fields.str(opt.get("label")); |
| 218 | + Fields.maxBytes(label, 200); |
| 219 | + Fields.noControlChars(label, false); |
| 220 | + } |
| 221 | + } |
| 222 | + |
| 223 | + private static void feedback(JsonValue.Obj b) { |
| 224 | + Closed.check(b, Set.of("kind", "variant", "content"), Set.of("kind", "variant", "content")); |
| 225 | + Fields.inEnum(Fields.str(b.get("variant")), FEEDBACK_VARIANTS); |
| 226 | + int bytes = Inline.validate(b.get("content"), true, true); |
| 227 | + cap(bytes, 2 * 1024); |
| 228 | + } |
| 229 | + |
| 230 | + private static void note(JsonValue.Obj b) { |
| 231 | + Closed.check(b, Set.of("kind", "variant", "title", "content"), Set.of("kind", "variant", "content")); |
| 232 | + Fields.inEnum(Fields.str(b.get("variant")), NOTE_VARIANTS); |
| 233 | + if (b.has("title")) { |
| 234 | + String title = Fields.str(b.get("title")); |
| 235 | + if (title.isEmpty()) { |
| 236 | + throw Fields.syntax(); |
| 237 | + } |
| 238 | + Fields.maxBytes(title, 200); |
| 239 | + Fields.noControlChars(title, false); |
| 240 | + Fields.requireNfc(title); |
| 241 | + } |
| 242 | + int bytes = Inline.validate(b.get("content"), true, true); |
| 243 | + cap(bytes, 4 * 1024); |
| 244 | + } |
| 245 | + |
| 246 | + private static void slugLanguage(String language) { |
| 247 | + // code_block.language: [a-z0-9_-], begins [a-z0-9], non-empty, <= 64. |
| 248 | + Fields.slug(language, 64); |
| 249 | + } |
| 250 | + |
| 251 | + private static void sha256Field(String s) { |
| 252 | + // "sha-256:" + 43 base64url chars = 51 chars total. |
| 253 | + if (s.length() != 51 || !s.startsWith("sha-256:")) { |
| 254 | + throw Fields.syntax(); |
| 255 | + } |
| 256 | + Fields.base64url(s.substring("sha-256:".length()), 32); |
| 257 | + } |
| 258 | + |
| 259 | + private static void cap(int bytes, int max) { |
| 260 | + if (bytes > max) { |
| 261 | + throw new RejectException(DiagnosticCode.E_SCHEMA_FIELD_LENGTH); |
| 262 | + } |
| 263 | + } |
| 264 | +} |
0 commit comments