From 930eb953e8cb598a99002cb41f3b8f57d05f1fa7 Mon Sep 17 00:00:00 2001 From: Kostya Date: Thu, 8 Oct 2026 11:24:18 +0300 Subject: 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. --- .../infrastructure/provider/v3/XMLValidatorV3.java | 135 +++++++++++++++++++++ 1 file changed, 135 insertions(+) create mode 100644 service/src/main/java/market/guess/service/catalog/infrastructure/provider/v3/XMLValidatorV3.java (limited to 'service/src/main/java/market/guess') 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 @@ +package market.guess.service.catalog.infrastructure.provider.v3; + +import java.util.HashSet; +import market.guess.api.Result; +import market.guess.service.catalog.infrastructure.repository.EventRepository; +import market.guess.service.catalog.model.v3.GMEvent; +import market.guess.service.catalog.model.v3.GMLMSR; +import market.guess.service.catalog.model.v3.GMOrderBook; +import market.guess.service.catalog.model.v3.GuessMarket; + +public final class XMLValidatorV3 { + private static final int MIN_COMMISSION = 0; + private static final int MAX_COMMISSION = 90; + + public Result validate(GuessMarket seed, EventRepository existing) { + if (seed == null || seed.getGMEvents() == null) { + return Result.error("INVALID_XML", "The XML file contains no GM-events element."); + } + + var events = seed.getGMEvents().getGMEvent(); + if (events.isEmpty()) { + return Result.error("EMPTY_EVENTS", "The XML file does not contain any events."); + } + + var seenNames = new HashSet(); + + for (GMEvent e : events) { + if (e.getName() == null || e.getName().isBlank()) { + return Result.error("MISSING_NAME", "An event has an empty name."); + } + var name = e.getName().trim(); + if (!seenNames.add(name.toLowerCase())) { + return Result.error( + "DUPLICATE_NAME", + "Event '" + name + "' appears more than once. Every event name must be unique."); + } + if (existing.getAll().stream().anyMatch(x -> x.getName().equalsIgnoreCase(name))) { + return Result.error( + "NAME_TAKEN", "An event named '" + name + "' already exists in the market."); + } + + if (e.getDescription() == null || e.getDescription().isBlank()) { + return Result.error("MISSING_DESCRIPTION", "Event '" + name + "' has an empty description."); + } + + if (e.getCommission() == null) { + return Result.error("MISSING_COMMISSION", "Event '" + name + "' is missing commission."); + } + var pct = e.getCommission().getValue(); + if (pct < MIN_COMMISSION || pct > MAX_COMMISSION) { + return Result.error( + "COMMISSION_OUT_OF_RANGE", + "Event '" + + name + + "' commission is " + + pct + + "%. It must be between " + + MIN_COMMISSION + + "% and " + + MAX_COMMISSION + + "%."); + } + + var type = e.getCommission().getType(); + if (type == null + || (!type.equalsIgnoreCase("on-close") && !type.equalsIgnoreCase("on-purchase"))) { + return Result.error( + "INVALID_COMMISSION_TYPE", + "Event '" + + name + + "' has invalid commission type '" + + type + + "'. Must be 'on-close' or 'on-purchase'."); + } + + if (e.getGMOptions() == null) { + return Result.error("MISSING_OPTIONS", "Event '" + name + "' is missing GM-options."); + } + var options = e.getGMOptions().getGMOption(); + if (options.size() != 2) { + return Result.error( + "INVALID_OPTIONS_COUNT", + "Event '" + name + "' must have exactly 2 options, but found " + options.size() + "."); + } + var opt0 = options.get(0) == null ? "" : options.get(0).trim(); + var opt1 = options.get(1) == null ? "" : options.get(1).trim(); + if (opt0.isEmpty() || opt1.isEmpty()) { + return Result.error("EMPTY_OPTION", "Event '" + name + "' has an empty option name."); + } + if (opt0.equalsIgnoreCase(opt1)) { + return Result.error( + "DUPLICATE_OPTION", "Event '" + name + "' has duplicate option names: '" + opt0 + "'."); + } + + if (e.getGMMethod() == null) { + return Result.error("MISSING_METHOD", "Event '" + name + "' is missing GM-method."); + } + if (e.getGMMethod().getGMLMSR() instanceof GMLMSR lmsr) { + if (lmsr.getB() <= 0) { + return Result.error( + "INVALID_LIQUIDITY", + "Event '" + + name + + "' has invalid liquidity b=" + + lmsr.getB() + + ". b must be a positive integer (> 0)."); + } + } else if (e.getGMMethod().getGMOrderBook() instanceof GMOrderBook ob) { + if (ob.getD() <= 0) { + return Result.error( + "INVALID_DENOMINATOR", + "Event '" + + name + + "' has invalid denominator d=" + + ob.getD() + + ". d must be a positive integer (> 0)."); + } + if (ob.getInitial() < 0) { + return Result.error( + "INVALID_INITIAL", + "Event '" + + name + + "' has invalid initial inventory: " + + ob.getInitial() + + ". Must be >= 0."); + } + } else { + return Result.error( + "INVALID_METHOD", "Event '" + name + "' has unknown or missing trading method."); + } + } + + return Result.ok(); + } +} -- cgit v1.2.3