aboutsummaryrefslogtreecommitdiffstats
path: root/service/src/main/java/market
diff options
context:
space:
mode:
authorKostya <mail@sartin.in>2026-10-08 11:24:18 +0300
committerKostya <mail@sartin.in>2026-10-08 11:24:45 +0300
commit930eb953e8cb598a99002cb41f3b8f57d05f1fa7 (patch)
treeeab822cb5ce7eb4bc56908d944519c34981473a2 /service/src/main/java/market
parent1a8b75f96f0c6855581be3a38069f09eca37d1f5 (diff)
downloadguess-market-930eb953e8cb598a99002cb41f3b8f57d05f1fa7.tar.gz
guess-market-930eb953e8cb598a99002cb41f3b8f57d05f1fa7.tar.xz
guess-market-930eb953e8cb598a99002cb41f3b8f57d05f1fa7.zip
catalog: add v3 XML validator for cumulative uploads
Schema v3 drops event ids and users, and uploads now add to a live market instead of replacing it. Validate a v3 document against the Exercise 1 rules (commission 0-90%, positive b and d, non-negative initial inventory, a known trading method), require exactly two options per event, and treat the event name as its identity: reject names that repeat within the file or collide with an event already in the repository, ignoring case and surrounding whitespace. The validator works on the unmarshalled object graph only and never touches the repository beyond reading it, so a rejected upload leaves the market untouched.
Diffstat (limited to 'service/src/main/java/market')
-rw-r--r--service/src/main/java/market/guess/service/catalog/infrastructure/provider/v3/XMLValidatorV3.java135
1 files changed, 135 insertions, 0 deletions
diff --git a/service/src/main/java/market/guess/service/catalog/infrastructure/provider/v3/XMLValidatorV3.java b/service/src/main/java/market/guess/service/catalog/infrastructure/provider/v3/XMLValidatorV3.java
new file mode 100644
index 0000000..3a76448
--- /dev/null
+++ b/service/src/main/java/market/guess/service/catalog/infrastructure/provider/v3/XMLValidatorV3.java
@@ -0,0 +1,135 @@
1package market.guess.service.catalog.infrastructure.provider.v3;
2
3import java.util.HashSet;
4import market.guess.api.Result;
5import market.guess.service.catalog.infrastructure.repository.EventRepository;
6import market.guess.service.catalog.model.v3.GMEvent;
7import market.guess.service.catalog.model.v3.GMLMSR;
8import market.guess.service.catalog.model.v3.GMOrderBook;
9import market.guess.service.catalog.model.v3.GuessMarket;
10
11public final class XMLValidatorV3 {
12 private static final int MIN_COMMISSION = 0;
13 private static final int MAX_COMMISSION = 90;
14
15 public Result<Void> validate(GuessMarket seed, EventRepository existing) {
16 if (seed == null || seed.getGMEvents() == null) {
17 return Result.error("INVALID_XML", "The XML file contains no GM-events element.");
18 }
19
20 var events = seed.getGMEvents().getGMEvent();
21 if (events.isEmpty()) {
22 return Result.error("EMPTY_EVENTS", "The XML file does not contain any events.");
23 }
24
25 var seenNames = new HashSet<String>();
26
27 for (GMEvent e : events) {
28 if (e.getName() == null || e.getName().isBlank()) {
29 return Result.error("MISSING_NAME", "An event has an empty name.");
30 }
31 var name = e.getName().trim();
32 if (!seenNames.add(name.toLowerCase())) {
33 return Result.error(
34 "DUPLICATE_NAME",
35 "Event '" + name + "' appears more than once. Every event name must be unique.");
36 }
37 if (existing.getAll().stream().anyMatch(x -> x.getName().equalsIgnoreCase(name))) {
38 return Result.error(
39 "NAME_TAKEN", "An event named '" + name + "' already exists in the market.");
40 }
41
42 if (e.getDescription() == null || e.getDescription().isBlank()) {
43 return Result.error("MISSING_DESCRIPTION", "Event '" + name + "' has an empty description.");
44 }
45
46 if (e.getCommission() == null) {
47 return Result.error("MISSING_COMMISSION", "Event '" + name + "' is missing commission.");
48 }
49 var pct = e.getCommission().getValue();
50 if (pct < MIN_COMMISSION || pct > MAX_COMMISSION) {
51 return Result.error(
52 "COMMISSION_OUT_OF_RANGE",
53 "Event '"
54 + name
55 + "' commission is "
56 + pct
57 + "%. It must be between "
58 + MIN_COMMISSION
59 + "% and "
60 + MAX_COMMISSION
61 + "%.");
62 }
63
64 var type = e.getCommission().getType();
65 if (type == null
66 || (!type.equalsIgnoreCase("on-close") && !type.equalsIgnoreCase("on-purchase"))) {
67 return Result.error(
68 "INVALID_COMMISSION_TYPE",
69 "Event '"
70 + name
71 + "' has invalid commission type '"
72 + type
73 + "'. Must be 'on-close' or 'on-purchase'.");
74 }
75
76 if (e.getGMOptions() == null) {
77 return Result.error("MISSING_OPTIONS", "Event '" + name + "' is missing GM-options.");
78 }
79 var options = e.getGMOptions().getGMOption();
80 if (options.size() != 2) {
81 return Result.error(
82 "INVALID_OPTIONS_COUNT",
83 "Event '" + name + "' must have exactly 2 options, but found " + options.size() + ".");
84 }
85 var opt0 = options.get(0) == null ? "" : options.get(0).trim();
86 var opt1 = options.get(1) == null ? "" : options.get(1).trim();
87 if (opt0.isEmpty() || opt1.isEmpty()) {
88 return Result.error("EMPTY_OPTION", "Event '" + name + "' has an empty option name.");
89 }
90 if (opt0.equalsIgnoreCase(opt1)) {
91 return Result.error(
92 "DUPLICATE_OPTION", "Event '" + name + "' has duplicate option names: '" + opt0 + "'.");
93 }
94
95 if (e.getGMMethod() == null) {
96 return Result.error("MISSING_METHOD", "Event '" + name + "' is missing GM-method.");
97 }
98 if (e.getGMMethod().getGMLMSR() instanceof GMLMSR lmsr) {
99 if (lmsr.getB() <= 0) {
100 return Result.error(
101 "INVALID_LIQUIDITY",
102 "Event '"
103 + name
104 + "' has invalid liquidity b="
105 + lmsr.getB()
106 + ". b must be a positive integer (> 0).");
107 }
108 } else if (e.getGMMethod().getGMOrderBook() instanceof GMOrderBook ob) {
109 if (ob.getD() <= 0) {
110 return Result.error(
111 "INVALID_DENOMINATOR",
112 "Event '"
113 + name
114 + "' has invalid denominator d="
115 + ob.getD()
116 + ". d must be a positive integer (> 0).");
117 }
118 if (ob.getInitial() < 0) {
119 return Result.error(
120 "INVALID_INITIAL",
121 "Event '"
122 + name
123 + "' has invalid initial inventory: "
124 + ob.getInitial()
125 + ". Must be >= 0.");
126 }
127 } else {
128 return Result.error(
129 "INVALID_METHOD", "Event '" + name + "' has unknown or missing trading method.");
130 }
131 }
132
133 return Result.ok();
134 }
135}