Mukle 4 : RAML : how to define the schema of a POST BODY request in a RAML file? - mule4

A POST REST request having 3 body params as follows:
{
"name" : "ABC",
"age": 34,
"uniqueID": "12345sdfgh"
}
My requirement is to define constraints (type, maxlength, min length, regex, etc.) for each field name, age and unique id.
How can I define that?

There are some different ways to define it. The 'pure' RAML way it is to define a data type fragment for the data object using RAML definitions for types. Those should cover all your needs.
Example:
dataType.raml
#%RAML 1.0 DataType
type: object
displayName: Booking
properties:
BookingDetail:
type: object
required: true
displayName: "BookingDetail"
description: "BookingDetail"
properties:
Name:
type: string
required: true
displayName: "Name"
description: "Name"
example: "John"
NumberOfDays:
type: integer
required: true
minimum: 1
maximum: 10
API:
#%RAML 1.0
title: so-type
/bookings:
post:
body:
application/json:
type: !include dataType.raml
You can also use JSON schemas if you prefer:
/orders:
post:
body:
application/json:
type: !include schemas/OrdersSchema.json

One more thing, I think. To require input to comply with a regex, you might do this:
properties:
Name:
type: string
required: true
displayName: "Name"
description: "Name"
pattern: ^[-A-Za-z ]+$
example: "John"
That pattern is overly restrictive, but does match many Western traditional names. Your own regex is presumably more carefully constructed.

Related

How do I convert part of a Terraform schema provider from interface {}([]interface {}) to an array of strings

I am creating a new resource in the aws-terraform-provider. In my schema, I have the following vpc options which is a complex type.
...
"vpc_options": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"subnets": {
Type: schema.TypeList,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"security_groups": {
Type: schema.TypeList,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
},
},
},
...
In hashicorp configuration language i am using this resource like this
vpc_options {
5 subnets = ["subnet-0be8cde92b74efcc8", "subnet-03a31237a28404445"]
6 security_groups = ["sg-0d3c85573d69473fb"]
7 }
I want to be able to use this resource. In order for me to use these fields effectively, I think I need the resource to be in the form of strings that I can use with the aws api calls for creation, deletion, update, and read. Even though I need these values to be strings, when I read the vpc_config in the debugger, I see that they are of type
interface {}([]interface {}) [map[string]interface {} ["subnets": *(*interface {})(0xc0063a8088), "security_groups": *(*interface {})(0xc0063a8098), ]]
Here is a picture of me inspecting the vpc_config in my debugger.
How should i go about converting this to an array of subnet strings and an array of security_groups strings. Then update the schema with d.Set("subnets",.... and d.Set("security_groups",....

Retrieving attributes from a resource group with a tempate composition

I want to create multiple copies of some resources defined in a template. How do I retrieve the attributes of the resources created in this fashion.
To illustrate, here is a template to create a random string (I call word.yaml):
heat_template_version: rocky
resources:
word:
type: OS::Heat::RandomString
Because I want a list of such random strings, I use an OS::Heat::ResourceGroup calling the template from word.yaml:
heat_template_version: rocky
resources:
composition:
type: OS::Heat::ResourceGroup
properties:
count: 2
resource_def:
type: word.yaml
outputs:
composition:
value: {get_attr: [composition, resource.word]}
Rubbing together the explanation of OS::Heat::ResourceGroup (ResourceGroup) and the template composition documentation (nested attributes), I expected a list of strings in my output, yet I get:
{
"outputs": [
{
"output_key": "composition",
"description": "No description given",
"output_error": "Member 'word' not found in group resource 'composition'.",
"output_value": null
}
]
}
What am I missing about the interaction of the resource group and the template composition?
Found the answer: I need to specify the attribute to retrieve as outpus of the composition. So the word.yaml would look like this:
heat_template_version: rocky
resources:
word:
type: OS::Heat::RandomString
outputs:
word:
description: the word
value: {get_attr: [word]}
And the composition, like so:
heat_template_version: rocky
resources:
composition:
type: OS::Heat::ResourceGroup
properties:
count: 2
resource_def:
type: word.yaml
outputs:
composition:
value: {get_attr: [composition, word]}
Yielding the expected output:
{
"output_key": "composition",
"description": "No description given",
"output_value": [
{
"value": "gB7ZcXgNjJlEtf8N47yFCOQyH0a1bb9c"
},
{
"value": "wXN4jAKgPqbf5i3GDR6qMkzfC7jF0xdC"
}
]
}

serverless step functions: Getting error when passing more than one fields in the payload for lambda

Error: Invalid State Machine Definition: 'SCHEMA_VALIDATION_FAILED: The value for the field 'Date.$' must be a valid JSONPath at /States/Insert Data Dynamodb/Parameters' (Service: AWSStepFunctions; Status Code: 400; Error Code: InvalidDefinition;
below is the corresponding serverless.yaml code.
I tried wrapping the two parameters into encoded json string and passed it as single payload field and it resulted in the same error but when there is only one plain field in the payload this code deployed successfully
Any suggestions on how to pass two parameters?
service: service-name
frameworkVersion: '2'
provider:
name: aws
runtime: go1.x
lambdaHashingVersion: 20201221
stage: ${opt:stage, self:custom.defaultStage}
region: us-east-1
tags: ${self:custom.tagsObject}
logRetentionInDays: 1
timeout: 10
deploymentBucket: lambda-repository
memorySize: 128
tracing:
lambda: true
plugins:
- serverless-step-functions
configValidationMode: error
stepFunctions:
stateMachines:
sortData:
name: datasorting-dev
type: STANDARD
role: ${self:custom.datasorting.${self:provider.stage}.iam}
definition:
Comment: "Data Sort"
StartAt: Query Data
States:
Query Data:
Type: Task
Resource: arn:aws:states:::athena:startQueryExecution.sync
Parameters:
QueryString: >-
select * from table.data
WorkGroup: primary
ResultConfiguration:
OutputLocation: s3://output/location
Next: Insert Data Dynamodb
Insert Data Dynamodb:
Type: Task
Resource: arn:aws:states:::lambda:invoke
Parameters:
FunctionName: arn:aws:lambda:us-east-1:${account-id}:function:name
Payload:
OutputLocation.$: $.QueryExecution.ResultConfiguration.OutputLocation
Date.$: ${self:custom.dates.year}${self:custom.dates.month}${self:custom.dates.day}
End: true
Your Date.$ property has value of ${self:custom.dates.year}${self:custom.dates.month}${self:custom.dates.day}. Let's assume that:
const dates = {
"year": "2000",
"month": "01",
"day": "20"
}
The result will be Date.$: "20000120" which is not a valid JSON Path.
JSON Path needs to start with a $ sign and each level is divided by ..
Do you want to achieve something like this? $.2000.01.20?
As you see, the issue is not with passing 2 parameters but with wrong string JSON Path created by string interpolation for Date.$.
Some useful links:
https://github.com/json-path/JsonPath
https://docs.aws.amazon.com/step-functions/latest/dg/amazon-states-language-paths.html

Polymorphism in Swagger produces strange error message on discriminator

I'm writing a service with a GET that can return one of five different but closely related types. Since the user wants the option of searching through all five types at once, it has to be a single get call. I'm returning JSON, which can easily handle any type.
I'm trying to do this in Swagger, using their polymorphism feature, which I've never tried before. I'm doing it just like in the example, except under "definitions" instead of "components/schemas". But I'm getting a strange error message that I can't understand. The swagger file is below. The error says this:
Schema error at definitions['Event'].discriminator should be string
It gives this on line 49, which says discriminator:
So, my two questions are: How can I fix it? And will this even give me what I need?
swagger: '2.0'
info:
description: RESTful API to retrieve Titles Metadata
version: 1.0.0
title: Swagger Mystery
schemes:
- https
paths:
/event:
get:
operationId: getEvent
summary: searches names
description: |
Search by names, across all types, or by a specific type.
produces:
- application/json
parameters:
- in: query
name: title
description: name to search for
required: true
type: string
- in: query
name: start
required: false
type: boolean
- in: query
name: type
required: false
type: string
description: |
May be "contest", "partner", "sponsor", or "dancer". If missing, will search for all types.
responses:
'200':
description: search results
# I also don't know why I need to comment these out.
# content:
# application/json:
# schema:
# type: array
# items:
# $ref: '#/definitions/Event'
'400':
description: bad input parameter
definitions:
Event:
type: object
discriminator:
propertyName: eventType
properties:
eventType:
type: string
id:
type: integer
format: int64
name:
type: string
description:
type: string
contests:
type: array
items:
$ref: '#/definitions/Contest'
required:
- id
- name
- description
- contests
- eventType
Contest:
allOf:
- $ref: '#/definitions/Event'
- type: object
properties:
parentEvent:
type: string
venue:
type: string
required:
- parentEvent
- venue
Dancer:
allOf:
- $ref: '#/definitions/Event'
- type: object
properties:
eventInvitationDate:
type: string
format: date
venue:
type: string
required:
- eventInvitationDate
- venue
# Sponsor:
# allOf:
# - $ref: '#/definitions/Event'
# - type: object
# properties:
# invitationDate:
# type: string
# format: date
# parentEvent:
# type: string
# partners:
# type: array
# items:
# $ref: '#/definitions/Partner'
Partner:
allOf:
- $ref: '#/definitions/Event'
- type: object
properties:
invitationDate:
type: string
format: date
parentEvent:
type: string
venue:
type: string
required:
- invitationDate
- parentEvent
- venue
# two problems:
# 1. Schema error at definitions['Event'].discriminator
# should be string on line 49 (discriminator:)
# 2. Resolver error:
# e is undefined
# (no line number)
# (This error goes away when I comment out Sponsor.)
The error occurs because you are mixing OpenAPI 2.0 and 3.0 syntax.
Your spec is swagger: '2.0' but the following is 3.0 syntax:
discriminator:
propertyName: eventType
In OpenAPI 2.0, the value of discriminator is the property name:
discriminator: eventType
Also, OpenAPI 2.0 assumes that the possible values of the discriminator property (in this case eventType) are exactly the same as the model names in definitions. That is:
If eventType can be event, partner etc. in lowercase, then the model names must also be in lowercase – event, not Event.
If eventType is some code like e, p, d etc., the model names must be e, p, d etc. instead of Event, Partner etc.
Check out questions for more examples of discriminator usage in OpenAPI 2.0:
Swagger Inheritance and Composition
“discriminator” in polymorphism, OpenAPI 2.0 (Swagger 2.0)
Swagger: variant schema shape dependant on field value

swagger editor reference definition inside another definition

Inside my definitions I'm using inheritance. In the example below, the PERSON-PATCH properties are coming in without issue. Now I want to take the PERSON-BIO properties and show that's a sub-object inside the PersonGet. I can't figure out the syntax to do that.
PersonBio:
type: object
properties: &PERSON-BIO
nickname:
type: string
description: The nickname for the Person
... other properties chopped out ...
minProperties: 1
PersonGet:
type: object
properties:
<<: *PERSON-PATCH
ident:
type: integer
format: int32
description: The SQL ident of the Person
bio:
<<: *PERSON-BIO
OK, just figured this out;
PersonGet:
type: object
properties:
<<: *PERSON-PATCH
bio:
$ref: '#/definitions/PersonBio'

Resources