How can I define an array object property, that contains several items in a comma separated string in openapi-v3?
I want to validate a request body (not query parameter!) like this:
{
"friends": "Ann,Bob"
}
I am dreaming of an openApi v3 schema definition like this one:
"friends": {
"type": "array",
"items": {
"type": "string",
"enum": [
"Ann",
"Bob",
"Charlie"
]
},
"commaSeparation": ",", // does not exist
}
Is there a officially supported way to describe such string contents? If not: What could be a workaround, that still precisely defines and validates those texts?
No, there is not. Unfortunately to the parser friends is and will always be a string.
You could add a pattern regex to enforce the contents, something like:
"friends": {
"type": "string",
"pattern": "[Ann|Bob|Charlie],+" // some regex that enforces the allowed tokens and a trailing comma
}
If you want a real array, then you need to use an array type.
Related
I am using excel to generate a JSON file via a power automate flow. I have most of the functionality working but am stuck on a formatting issue where lists are being outputted as objects.
This is the format I need:
"_source" : { "title" : null, "first_name" : { "value" : "Tony", "source" : "48fa2a08-9137-49fa-8a7d-1d85570b7e5d" }, "last_name" : { "value" : "Stark", "source" : "48fa2a08-9137-49fa-8a7d-1d85570b7e5d" }, "full_name" : "Tony Stark",...
This is the format I am getting:
"_source": { "title": null, "first_name": [ { "value": "Tony", "source": "48fa2a08-9137-49fa-8a7d-1d85570b7e5d" } ], "last_name": [ { "value": "Stark", "source": "48fa2a08-9137-49fa-8a7d-1d85570b7e5d" } ], "full_name": ["Tony Stark"],...
I am composing this in the following way:
How do I get rid of the square brackets?
Any help would be greatly appreciated. TIA
The output result of your data operations for (e.g.) first_name is an array.
In order to remove the square brackets, you need to drill down and get the item you want from the array.
Typically, an array is an array because you potentially expect more than one item to exist but in your case, it looks like that’s all you expect, therefore, you need to use an expression that will get the first item in the array.
To do that, you can wrap the “Output” from the Data Operations step (whatever that action actually is) in a first() expression.
That will return the first object in your array and remove the square brackets.
Passing an object to an existing field in $set or $addFields merges objects rather than replaces them, e.g.
https://mongoplayground.net/p/aXe-rExjCXr
// Collection
[
{
"_id": "123",
"options": {
"size": "Large",
"color": "Red"
}
}
]
// Aggregate
db.collection.aggregate([
{
$set: {
options: {
size: "Small"
}
}
}
]);
// Expect
[
{
"_id": "123",
"options": {
"size": "Small"
}
}
]
// Actual
[
{
"_id": "123",
"options": {
"size": "Small",
"color": "Red" // <-- Not expected?
}
}
]
(It get's even weirder with arrays)
Is it possible to have it behave like non-object values and simply replace the field?
For context, I want to use this in an aggregate update pipeline.
This is the expected behaviour, and as far as i know there is not plan to change, as far as i remembered there was a jira with this, but they closed it, meaning that it will not change i think.
$set/$addFields replace always except
array field and i add document => array with all members that document
document field and i add document => merge documents (this is your case here)
$project replace always except
array field and i add document => array with all members that document
Solutions
You can override this "weird" behaviour especially in case of
arrays, by $unset the old field first for example, and then $set
Based on the jira in the comment bellow, we can also use $literal to avoid this, but when we use $literal we have to be sure that we dont use expressions because they will not be evaluated.
(expressions like path references, variables, operators etc)
I'm trying to standartize a property in a json-ld document. A simple example:
json-ld
{
"#context": {
"rdfs": "http://www.w3.org/2000/01/rdf-schema#",
"dcterms": "http://purl.org/dc/terms/"
},
"#graph": [
{
"#id": "1",
"rdfs:label": "A title"
},
{
"#id": "2",
"dcterms:title": "Another title"
}
]
}
frame (failing attempt)
{
"type": "array",
"items": {
"title": ["rdfs:label", "dcterms:title"]
}
}
This produces an empty graph, instead of this:
desired output
[{
"title": "A title"
},
{
"title": "Another title"
}]
The documentation at https://json-ld.org/primer/latest/#framing seems to be work in progress and there is really not a lot of examples or tutorials covering json-ld framing.
Playground example
Framing is used to shape the data in a JSON-LD document, using an example frame document which is used to both match the flattened data and show an example of how the resulting data should be shaped
https://json-ld.org/spec/latest/json-ld-framing/#framing
This beeing said, re-shaping data does not mean you can change the semantics. rdfs:label and dcterms:title are different things in the source data and will be different things in the result, you can not merge them to a "title" property that expands to only one URI (which one?). If that were the case, the result would have different semantics than the source, but framing is only meant to change the structure.
I've been playing with the Entity system in Draft.js. One limitation I see is that entities have to correspond with a range of text in the content they are inserted into. I was hoping I could make a zero-length entity which would have a display based on the data in the entity rather than the text-content in the block. Is this possible?
This is possible when you have a whole block. As you can see in the code example this serialised blockMap contains a block containing no text, but the character list has one entry with an entity attached to it. There is also some discussion going on regarding adding meta-data to a block. see https://github.com/facebook/draft-js/issues/129
"blockMap": {
"80sam": {
"key": "80sam",
"type": "sticker",
"text": "",
"characterList": [
{
"style": [],
"entity": "1"
}
],
"depth": 0
},
},
Not to confuse anybody, I'll start with validating arrays...
Regarding arrays, JSON Schema can check whether elements of an (((...)sub)sub)array conform to a structure:
"type": "array",
"items": {
...
}
When validating objects, I know I can pass certain keys with their corresponding value types, such as:
"type": "object",
"properties": {
// key-value pairs, might also define subschemas
}
But what if I've got an object which I want to use to validate values only (without keys)?
My real-case example is that I'm configuring buttons: there might be edit, delete, add buttons and so on. They all have specific, rigid structure, which I do have JSON schema for. But I don't want to limit myself to ['edit', 'delete', 'add'] only, there might be publish or print in the future. But I know they all will conform to my subschema.
Each button is:
BUTTON = {
"routing": "...",
"params": { ... },
"className": "...",
"i18nLabel": "..."
}
And I've got an object (not an array) of buttons:
{
"edit": BUTTON,
"delete": BUTTON,
...
}
How can I write such JSON schema? Is there any way of combining object with items (I know there are object-properties and array-items relations).
You can use additionalProperties for this. If you set additionalProperties to a schema instead of a boolean, then any properties that aren't explicitly declared using the properties or patternProperties keywords must match the given schema.
{
"type": "object",
"additionalProperties": {
... BUTTON SCHEMA ...
}
}
http://json-schema.org/latest/json-schema-validation.html#anchor64