Manually create a react element Object with streaming - reactjs

Is it possible to create a react element object manually without using react.createElement.
The idea is to create nested react elements tree while streaming from html. The problem is I am unaware of nested children during streaming.
My objective is to be able to create a react element from a nested object. ie:
{
   "tag": "div",
   "attribs": {},
   "children": [
      {
         "tag": "p",
         "attribs": {},
         "children": [],
         "text": "test"
      },
      {
         "tag": "div",
         "attribs": {},
         "children": [
            {
               "tag": "p",
               "attribs": {},
               "children": [],
               "text": "hi"
            }
         ]
      }
   ]
}

You can also create nested components by using React.createElement, but you need to follow a special syntax to achieve that.
tag needs to be type and attribs need to be props, but you may iterate over your object and rename those keys if needed, so that's not the problem.
Now having something like this:
var data = {
"type": "div",
"props": {anyProp: true},
"children": [
{
"type": "p",
"props": {},
"children": [],
"text": "test"
},
{
"type": "div",
"props": {},
"children": [
{
"type": "p",
"props": {},
"children": [],
"text": "hi"
}
]
}
]
}
You can just use React.createElement(data.type, data.props, data.children); to create a component including all children, properties et cetera.

Related

JSON schema for an unnamed array?

I need to create a JSON schema for data that comes as an array directly within the root object, unnamed. An MWE for this kind of JSON would be:
{
[
{
"veggieName": "potato",
"veggieLike": true
},
{
"veggieName": "broccoli",
"veggieLike": false
}
]
}
I have seen examples for schemas which validate such an array which is not nested in an object. I have also seen examples which work when the array is named, for example
{
vegetables : [
{
"veggieName": "potato",
"veggieLike": true
},
{
"veggieName": "broccoli",
"veggieLike": false
}
]
}
This second example can be validated by the schema
{
"$id": "https://example.com/arrays.schema.json",
"$schema": "http://json-schema.org/draft-07/schema#",
"description": "A representation of a person, company, organization, or place",
"type": "object",
"properties": {
"vegetables": {
"type": "array",
"items": { "$ref": "#/definitions/veggie" }
}
},
"definitions": {
"veggie": {
"type": "object",
"required": [ "veggieName", "veggieLike" ],
"properties": {
"veggieName": {
"type": "string",
"description": "The name of the vegetable."
},
"veggieLike": {
"type": "boolean",
"description": "Do I like this vegetable?"
}
}
}
}
}
But the problem is, as soon as the name "vegetables" is removed, I was not able to find a way to define a valid schema. How do I properly represent my data structure in a schema?
(MWEs derived from http://json-schema.org/learn/miscellaneous-examples.html).
The schema you are looking for is the following:
{
"$id":"https://example.com/arrays.schema.json",
"$schema":"http://json-schema.org/draft-07/schema#",
"description":"A representation of a person, company, organization, or place",
"type":"array",
"items":{
"type":"object",
"required":[
"veggieName",
"veggieLike"
],
"properties":{
"veggieName":{
"type":"string",
"description":"The name of the vegetable."
},
"veggieLike":{
"type":"boolean",
"description":"Do I like this vegetable?"
}
}
}
}
You also need to modify your base array instance, your original one (the "unnamed" array) was not valid JSON:
[
{
"veggieName":"potato",
"veggieLike":true
},
{
"veggieName":"broccoli",
"veggieLike":false
}
]
Unlike XML, where you are allowed a single root node per document only, in JSON you can have either a type or an array as a root type.

GetStream Activity component not rendering new lines properly

The stream activity component
https://getstream.github.io/react-activity-feed/#activity is not able to render new lines.
{
"actor": {
},
"attachments": {
"files": [],
"images": []
},
"foreign_id": "",
"id": "",
"latest_reactions": {},
"latest_reactions_extra": {},
"object": "Post\npost\npost\npost",
"origin": null,
"own_reactions": {},
"reaction_counts": {},
"target": "",
"text": "Post\npost\npost\npost",
"time": "2019-06-12T19:11:46.728935",
"verb": "post"
}
In the above data the text contains new lines "Post\npost\npost\npost" still it is coming in a single line as in the image below.
Is there any way this can be handled in the component.
You can fix this by setting the following css:
.raf-activity__content p {
white-space: pre-line
}
I have also included this in the latest version(0.9.16) of react-activity-feed so you won't have to do it yourself.

Swagger array of strings without name

Currently I am trying to create a swagger file for my software.
Now I would like to create a definition for a timeRange.
My problem is that this array looks like this:
timeRange: {
"2016-01-15T09:00:00.000Z", // this is the start date
"2017-01-15T09:00:00.000Z" // this is the end date
}
How can I create an example value that works out of the box?
It is an "array of strings" with a minimum of two.
"timeRange": {
"type": "array",
"items": {
"type": "string",
"example": "2017-01-15T09:00:00.000Z,2017-01-15T09:00:00.000Z"
}
}
This generates an example like this:
"timeRange": [
"2017-01-15T09:00:00.000Z,2017-01-15T09:00:00.000Z"
]
This example does not work, because it is an array and not an object.
All together:
How can I realize an example value that exists out of two different strings (without a name).
Hope you can help me!
Cheers!
timeRange: {
"2016-01-15T09:00:00.000Z", // this is the start date
"2017-01-15T09:00:00.000Z" // this is the end date
}
is not valid JSON – "timeRange" needs to be enclosed in quotes, and the object/array syntax should be different.
If using the object syntax {}, the values need to be named properties:
"timeRange": {
"start_date": "2016-01-15T09:00:00.000Z",
"end_date": "2017-01-15T09:00:00.000Z"
}
Otherwise timeRange needs to be an [] array:
"timeRange": [
"2016-01-15T09:00:00.000Z",
"2017-01-15T09:00:00.000Z"
]
In the first example ({} object), your Swagger would look as follows, with a separate example for each named property:
"timeRange": {
"type": "object",
"properties": {
"start_date": {
"type": "string",
"format": "date-time",
"example": "2016-01-15T09:00:00.000Z"
},
"end_date": {
"type": "string",
"format": "date-time",
"example": "2017-01-15T09:00:00.000Z"
}
},
"required": ["start_date", "end_date"]
}
In case of an [] array, you can specify an array-level example that is a multi-item array:
"timeRange": {
"type": "array",
"items": {
"type": "string",
"format": "date-time"
},
"example": [
"2016-01-15T09:00:00.000Z",
"2017-01-15T09:00:00.000Z"
]
}

empty array in angular-schema-form

I'm using angular-schema-form in my project and trying to add an empty array in my model.
I expected that array type schema form will not contain any items but it actually pushed one object in my array and showed it in from.
How can I init form with no items in array?
html
<div ng-app="app" ng-controller="Controller as ctr">
<form sf-schema="ctr.schema" sf-form="ctr.form" sf-model="ctr.model"></form>
{{ctr.model.comments}} - Where this object come from? This array was empty. Is it possible to keep it empty on load?
</div>
js
var myApp = angular.module('app', ['schemaForm'])
.controller("Controller", function() {
this.schema = {
"type": "object",
"title": "Comment",
"properties": {
"comments": {
"type": "array",
"items": {
"type": "object",
"properties": {
"name": {
"title": "Name",
"type": "string"
},
"comment": {
"title": "Comment",
"type": "string",
"maxLength": 20,
"validationMessage": "Don't be greedy!"
}
}
}
}
}
};
this.form = [{
"key": "comments",
"items": [
"comments[]"
]
}];
this.model = {
comments: [] // Empty array defined
};
});
Jsfiddle
The value you are looking for is startEmpty: true this will avoid pre-populating the array with an object.
Angular Schema Form: Array Documentation
The pre-population is defaulted to ensure that the form fields within an array are available when the form loads. The startEmpty: true value can override this behaviour.

Update field on nested array

i have a model of how this:
_id:ObjectId("5705005b240166e927f841cb")
figures: [
{
id: "5433234"
figure: [
{
"id":"figure_543",
"active": false,
"name": "Alvar"
},
{
"id":"figure_431",
"active": false,
"name": "Peter"
}
]
},
{
id: "1276544"
figure: [
{
"id":"figure_123",
"active": false,
"name": "Jhon"
}
]
},
{
id: "1231645"
figure: [
{
"id":"figure_154",
"active": false,
"name": "Luka"
}
]
}
]
I'am doing this:
db.collection.update({"_id": userId, "figures.figure.id": figureId}, {$set:{"figures.figure.$.active": true}})
userId and figureId are variables that come from req.body on expressjs.
But i see this:
The positional $ operator cannot be used for queries which traverse more than one array, such as queries that traverse arrays nested within other arrays, because the replacement for the $ placeholder is a single value
My question is, how can i update the field active of figure on nested array with mongoose?
Thank's!!!

Resources