Compare commits
No commits in common. "6318497294389e9c647091144ed6ac88f89e7185" and "3566cb61b6c114677dbce270043dd2b06408b5b5" have entirely different histories.
6318497294
...
3566cb61b6
@ -23,7 +23,6 @@ import java.util.List;
|
|||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
import java.util.function.BiConsumer;
|
import java.util.function.BiConsumer;
|
||||||
|
|
||||||
import static net.hostsharing.hsadminng.hs.hosting.asset.validators.HsHostingAssetEntityValidatorRegistry.cleanup;
|
|
||||||
import static net.hostsharing.hsadminng.hs.hosting.asset.validators.HsHostingAssetEntityValidatorRegistry.validated;
|
import static net.hostsharing.hsadminng.hs.hosting.asset.validators.HsHostingAssetEntityValidatorRegistry.validated;
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
@ -72,7 +71,7 @@ public class HsHostingAssetController implements HsHostingAssetsApi {
|
|||||||
|
|
||||||
final var entityToSave = mapper.map(body, HsHostingAssetEntity.class, RESOURCE_TO_ENTITY_POSTMAPPER);
|
final var entityToSave = mapper.map(body, HsHostingAssetEntity.class, RESOURCE_TO_ENTITY_POSTMAPPER);
|
||||||
|
|
||||||
final var saved = validated(assetRepo.save(entityToSave));
|
final var saved = saveAndValidate(entityToSave);
|
||||||
|
|
||||||
final var uri =
|
final var uri =
|
||||||
MvcUriComponentsBuilder.fromController(getClass())
|
MvcUriComponentsBuilder.fromController(getClass())
|
||||||
@ -127,8 +126,8 @@ public class HsHostingAssetController implements HsHostingAssetsApi {
|
|||||||
|
|
||||||
new HsHostingAssetEntityPatcher(em, current).apply(body);
|
new HsHostingAssetEntityPatcher(em, current).apply(body);
|
||||||
|
|
||||||
final var saved = validated(assetRepo.save(current));
|
final var saved = saveAndValidate(current);
|
||||||
final var mapped = mapper.map(saved, HsHostingAssetResource.class, ENTITY_TO_RESOURCE_POSTMAPPER);
|
final var mapped = mapper.map(saved, HsHostingAssetResource.class);
|
||||||
return ResponseEntity.ok(mapped);
|
return ResponseEntity.ok(mapped);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -146,7 +145,11 @@ public class HsHostingAssetController implements HsHostingAssetsApi {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
final BiConsumer<HsHostingAssetEntity, HsHostingAssetResource> ENTITY_TO_RESOURCE_POSTMAPPER = (entity, resource) -> {
|
HsHostingAssetEntity saveAndValidate(final HsHostingAssetEntity entity) {
|
||||||
cleanup(entity, resource);
|
final var saved = assetRepo.save(entity);
|
||||||
};
|
// FIXME: this is hacky, better remove the properties from the mapped resource object
|
||||||
|
em.flush();
|
||||||
|
em.detach(saved); // validated(...) is going to remove writeOnly properties
|
||||||
|
return validated(saved);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,7 +2,6 @@ package net.hostsharing.hsadminng.hs.hosting.asset.validators;
|
|||||||
|
|
||||||
import net.hostsharing.hsadminng.hs.hosting.asset.HsHostingAssetEntity;
|
import net.hostsharing.hsadminng.hs.hosting.asset.HsHostingAssetEntity;
|
||||||
import net.hostsharing.hsadminng.hs.hosting.asset.HsHostingAssetType;
|
import net.hostsharing.hsadminng.hs.hosting.asset.HsHostingAssetType;
|
||||||
import net.hostsharing.hsadminng.hs.hosting.generated.api.v1.model.HsHostingAssetResource;
|
|
||||||
import net.hostsharing.hsadminng.hs.validation.HsEntityValidator;
|
import net.hostsharing.hsadminng.hs.validation.HsEntityValidator;
|
||||||
import net.hostsharing.hsadminng.errors.MultiValidationException;
|
import net.hostsharing.hsadminng.errors.MultiValidationException;
|
||||||
|
|
||||||
@ -42,7 +41,12 @@ public class HsHostingAssetEntityValidatorRegistry {
|
|||||||
|
|
||||||
public static List<String> doValidate(final HsHostingAssetEntity hostingAsset) {
|
public static List<String> doValidate(final HsHostingAssetEntity hostingAsset) {
|
||||||
final var validator = HsHostingAssetEntityValidatorRegistry.forType(hostingAsset.getType());
|
final var validator = HsHostingAssetEntityValidatorRegistry.forType(hostingAsset.getType());
|
||||||
return validator.validate(hostingAsset);
|
final var validated = validator.validate(hostingAsset);
|
||||||
|
|
||||||
|
//validator.cleanup()
|
||||||
|
hostingAsset.getConfig().remove("password"); // FIXME
|
||||||
|
hostingAsset.getConfig().remove("totpKey"); // FIXME
|
||||||
|
return validated;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static HsHostingAssetEntity validated(final HsHostingAssetEntity entityToSave) {
|
public static HsHostingAssetEntity validated(final HsHostingAssetEntity entityToSave) {
|
||||||
@ -50,18 +54,4 @@ public class HsHostingAssetEntityValidatorRegistry {
|
|||||||
return entityToSave;
|
return entityToSave;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void cleanup(final HsHostingAssetEntity entity, final HsHostingAssetResource resource) {
|
|
||||||
final var validator = HsHostingAssetEntityValidatorRegistry.forType(entity.getType());
|
|
||||||
final var config = validator.cleanup(asMap(resource));
|
|
||||||
resource.setConfig(config);
|
|
||||||
}
|
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
|
||||||
private static Map<String, Object> asMap(final HsHostingAssetResource resource) {
|
|
||||||
if (resource.getConfig() instanceof Map map) {
|
|
||||||
return map;
|
|
||||||
}
|
|
||||||
throw new IllegalArgumentException("expected a Map, but got a " + resource.getConfig().getClass());
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -3,7 +3,6 @@ package net.hostsharing.hsadminng.hs.validation;
|
|||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.function.Supplier;
|
import java.util.function.Supplier;
|
||||||
@ -87,10 +86,4 @@ public abstract class HsEntityValidator<E> {
|
|||||||
}
|
}
|
||||||
throw new IllegalArgumentException("Integer value (or null) expected, but got " + value);
|
throw new IllegalArgumentException("Integer value (or null) expected, but got " + value);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Map<String, Object> cleanup(final Map<String, Object> config) {
|
|
||||||
final var copy = new HashMap<>(config);
|
|
||||||
stream(propertyValidators).filter(p -> p.writeOnly).forEach(p -> copy.remove(p.propertyName));
|
|
||||||
return copy;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -17,6 +17,8 @@ public class StringProperty extends ValidatableProperty<String> {
|
|||||||
private Pattern regExPattern;
|
private Pattern regExPattern;
|
||||||
private Integer minLength;
|
private Integer minLength;
|
||||||
private Integer maxLength;
|
private Integer maxLength;
|
||||||
|
private boolean writeOnly;
|
||||||
|
private boolean readOnly;
|
||||||
private boolean hidden;
|
private boolean hidden;
|
||||||
|
|
||||||
protected StringProperty(final String propertyName) {
|
protected StringProperty(final String propertyName) {
|
||||||
@ -47,6 +49,18 @@ public class StringProperty extends ValidatableProperty<String> {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public StringProperty writeOnly() {
|
||||||
|
this.writeOnly = true;
|
||||||
|
super.optional();
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public StringProperty readOnly() {
|
||||||
|
this.readOnly = true;
|
||||||
|
super.optional();
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void validate(final List<String> result, final String propValue, final PropertiesProvider propProvider) {
|
protected void validate(final List<String> result, final String propValue, final PropertiesProvider propProvider) {
|
||||||
if (minLength != null && propValue.length()<minLength) {
|
if (minLength != null && propValue.length()<minLength) {
|
||||||
|
@ -32,9 +32,6 @@ public abstract class ValidatableProperty<T> {
|
|||||||
private final String[] keyOrder;
|
private final String[] keyOrder;
|
||||||
private Boolean required;
|
private Boolean required;
|
||||||
private T defaultValue;
|
private T defaultValue;
|
||||||
protected boolean readOnly;
|
|
||||||
protected boolean writeOnly;
|
|
||||||
|
|
||||||
protected Function<ValidatableProperty<?>[], T[]> deferredInit;
|
protected Function<ValidatableProperty<?>[], T[]> deferredInit;
|
||||||
private boolean isTotalsValidator = false;
|
private boolean isTotalsValidator = false;
|
||||||
@JsonIgnore
|
@JsonIgnore
|
||||||
@ -46,20 +43,6 @@ public abstract class ValidatableProperty<T> {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public ValidatableProperty<T> writeOnly() {
|
|
||||||
this.writeOnly = true;
|
|
||||||
optional();
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public ValidatableProperty<T> readOnly() {
|
|
||||||
this.readOnly = true;
|
|
||||||
optional();
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
public ValidatableProperty<T> required() {
|
public ValidatableProperty<T> required() {
|
||||||
required = TRUE;
|
required = TRUE;
|
||||||
return this;
|
return this;
|
||||||
|
Loading…
Reference in New Issue
Block a user