forked from datahub-project/datahub
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(gms): add rest.li validation in gms (datahub-project#2745)
- Loading branch information
1 parent
8fc1947
commit f0b4adc
Showing
4 changed files
with
87 additions
and
2 deletions.
There are no files selected for viewing
36 changes: 36 additions & 0 deletions
36
gms/impl/src/main/java/com/linkedin/metadata/resources/ResourceUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package com.linkedin.metadata.resources; | ||
|
||
import com.linkedin.common.urn.UrnValidator; | ||
import com.linkedin.data.schema.validation.ValidateDataAgainstSchema; | ||
import com.linkedin.data.schema.validation.ValidationOptions; | ||
import com.linkedin.data.schema.validation.ValidationResult; | ||
import com.linkedin.data.template.RecordTemplate; | ||
import com.linkedin.restli.common.HttpStatus; | ||
import com.linkedin.restli.server.RestLiServiceException; | ||
|
||
|
||
public class ResourceUtils { | ||
|
||
private static final ValidationOptions DEFAULT_VALIDATION_OPTIONS = new ValidationOptions(); | ||
private static final UrnValidator URN_VALIDATOR = new UrnValidator(); | ||
|
||
/** | ||
* Validates a {@link RecordTemplate} and throws {@link com.linkedin.restli.server.RestLiServiceException} | ||
* if validation fails. | ||
* | ||
* @param record record to be validated. | ||
* @param status the status code to return to the client on failure. | ||
*/ | ||
public static void validateRecord(RecordTemplate record, HttpStatus status) { | ||
final ValidationResult result = ValidateDataAgainstSchema.validate( | ||
record, | ||
DEFAULT_VALIDATION_OPTIONS, | ||
URN_VALIDATOR); | ||
if (!result.isValid()) { | ||
throw new RestLiServiceException(status, result.getMessages().toString()); | ||
} | ||
} | ||
|
||
private ResourceUtils() { } | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
li-utils/src/main/javaPegasus/com/linkedin/common/urn/UrnValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package com.linkedin.common.urn; | ||
|
||
import com.linkedin.data.message.Message; | ||
import com.linkedin.data.schema.DataSchema; | ||
import com.linkedin.data.schema.NamedDataSchema; | ||
import com.linkedin.data.schema.validator.Validator; | ||
import com.linkedin.data.schema.validator.ValidatorContext; | ||
import java.net.URISyntaxException; | ||
|
||
|
||
/** | ||
* Rest.li Validator responsible for ensuring that {@link Urn} objects are well-formed. | ||
* | ||
* Note that this validator does not validate the integrity of strongly typed urns, | ||
* or validate Urn objects against their associated key aspect. | ||
*/ | ||
public class UrnValidator implements Validator { | ||
@Override | ||
public void validate(ValidatorContext context) { | ||
if (DataSchema.Type.TYPEREF.equals(context.dataElement().getSchema().getType()) | ||
&& ((NamedDataSchema) context.dataElement().getSchema()).getName().endsWith("Urn")) { | ||
try { | ||
Urn.createFromString((String) context.dataElement().getValue()); | ||
} catch (URISyntaxException e) { | ||
context.addResult(new Message(context.dataElement().path(), "\"Provided urn %s\" is invalid", context.dataElement().getValue())); | ||
context.setHasFix(false); | ||
} | ||
} | ||
} | ||
} |