public Properties(String aName, HashMap<String, Validator> aRuleset, PropRule... rules) { super(aName); for (PropRule rule : rules) { all.put(rule.getKey(), rule); if (!rule.isOptional()) required.add(rule); } }
public void validate(JSONValue aValue) throws ValidationException { // Only for objects. if (!aValue.isObject()) throw new ValidationException(String.format(PROP004, aValue.toString(), this.getName())); JSONObject lObj = (JSONObject) aValue; // First we check if required keys are there. for (PropRule aRequired : required) { if (!lObj.containsKey(aRequired.getKey())) throw new ValidationException( String.format(PROP002, aValue.toString(), aRequired.getKey(), this.getName())); } // Now we iterate over all keys in the object and lookup the spec. for (String lKey : lObj.getValue().keySet()) { if (!all.containsKey(lKey)) throw new ValidationException( String.format(PROP003, aValue.toString(), lKey, this.getName())); PropRule lRule = all.get(lKey); try { lRule.getRule().validate(lObj.get(lKey)); } catch (ValidationException e) { throw new ValidationException( String.format(PROP005, lKey, aValue.toString(), this.getName(), e.getMessage()), e); } } }