Payload validation with AMF
You can use AMF to validate incoming payloads to an API already deployed and running. To do so, you need to:
- Get the AMF
Shape
to use for validation - Create an
AMFShapePayloadValidator
for that shape- with the
AMFElementClient
spayloadValidatorFor()
method
- with the
- Validate payloads
Get the AMF Shape
to use for validation
When AMF parses an API it returns a Document
that encodes a WebApi
, which is the AMF API Contract Model based on the original API:
You can obtain the specific Shape
you want from this object.
The AMF API Contract Model documentation has more information about the WebApi object.
Create a AMFShapePayloadValidator
for a specific Shape
You can create an AMFElementClient
from any AMFConfiguration
simply by doing configuration.elementClient()
.
All AMFElementClient
s have the methods payloadValidatorFor(shape, mediatype, mode)
or payloadValidatorFor(shape, fragment)
that return an AMFShapePayloadValidator
:
trait AMFShapePayloadValidator {
def validate(payload: String): Future[AMFValidationReport]
def validate(payloadFragment: PayloadFragment): Future[AMFValidationReport]
def syncValidate(payload: String): AMFValidationReport
}
Validate the payloads
To validate payloads use the AMFShapePayloadValidator
methods:
validate(payload: String): Future[AMFValidationReport]
- validates a given payload
- Returns a completable future of the
AMFValidationReport
object containing detailed information about any errors
validate(payloadFragment: PayloadFragment): Future[AMFValidationReport]
- Validates a payload from an AMF PayloadFragment
syncValidate(payload: String): AMFValidationReport
- Validates a payload synchronously
- Returns the
AMFValidationReport
directly
Example code for payload validation
For example, consider a RAML API that declares:
- A
User
schema withfirstname
,lastname
, andage
properties - A
/users
endpoint with an HTTP POST method that requires aUser
object as body parameter
#%RAML 1.0
title: API with Types
types:
User:
type: object
properties:
firstname: string
lastname: string
age: number
/users:
post:
body:
application/json:
type: User
To confirm that a given payload is a valid User
, you can use AMF payload validation following the previous steps:
- Get the AMF
Shape
to use for validation- The User schema we want can be obtained from the
Request
sPayload
object
- The User schema we want can be obtained from the
- Create an
AMFShapePayloadValidator
for that shape- with the
AMFElementClient
spayloadValidatorFor()
method
- with the
- Validate payloads
- with the
AMFShapePayloadValidator
svalidate()
methods
- with the
- Scala
- Java
- TypeScript
Code extracted from the examples GitHub repository.
Code extracted from the examples GitHub repository.
Code extracted from the examples GitHub repository.
Custom payload validation plugin
AMF lets you create a payload validation plugin by extending the AMFShapePayloadValidationPlugin
in Scala or Java, or
implementing the JsAMFPayloadValidationPlugin
in Javascript or Typescript.
For example: given the following implementations of payload validation plugins:
- Scala
- Java
You can now use these to validate payloads:
- Scala
- Java
- TypeScript
Code extracted from the examples GitHub repository.
Code extracted from the examples GitHub repository.
Code extracted from the examples GitHub repository.