Return a random value to data in field value is blank - netlify-cms

I'm working on a redirect generator using 11ty and netlify-cms, and storing data in a shorturls.json file.
My config.yml looks like this:
collections:
- name: "config"
label: "Admin"
description: "Administer site settings"
create: false
editor:
preview: false
files:
- label: short urls
name: shorturls
file: "_src/_data/shorturls.json"
widget: "object"
fields:
- label: "Short urls"
label_singular: "Short url"
name: "go"
widget: "list"
fields:
- { label: "From", name: "from", widget: "string", required: false }
- { label: "To", name: "to", widget: "string" }
- { label: "Redirect type", name: "status", widget: "hidden", default: "301" }
My json structure looks like this:
{
"go": [
{
"from": "/here",
"to": "/there"
},
{
"to": "/from-random"
}
]
}
Is there a way to populate a random value to the from field? Is this going to need to be a custom widget?
The json output should then look something like this:
{
"go": [
{
"from": "/here",
"to": "/there"
},
{
"from": "/e6y0p13l",
"to": "/from-random"
}
]
}
Thanks

Related

Ansible: Merge dictionaries within a list adding values to a list

How can I merge dictionaries within the list with the some properties that are the same and have the values of the properties that are different added to a list value of merged dictionary?
[
{
"name": "samename",
"content": "content1"
},
{
"name": "samename",
"content": "content2"
},
{
"name": "differentname",
"content": "content3"
}
]
Desired output:
[
{
"name": "samename",
"content": ["content1", "content2"]
},
{
"name": "differentname",
"content": "content3"
}
]
Given the data
data:
- content: content1
name: samename
- content: content2
name: samename
- content: content3
name: differentname
Use the filter json_query to create lists from the attribute content. Then use the filter community.general.lists_mergeby to merge the items, e.g.
data_groups_query: '[].{name: name, content: [content]}'
data_groups: "{{ [data|json_query(data_groups_query), []]|
community.general.lists_mergeby('name', list_merge='append') }}"
gives what you want
data_groups:
- content: [content3]
name: differentname
- content: [content1, content2]
name: samename

How to create joi schema to validate object of objects

I tried to make schema to validate json such this:
{
"integration": { "module": [ "m" ] },
"tile": {
"title": "TTT",
"text": "ttt",
"icon": "./resources/main-icon.png",
"tags": [ "bbb", "vvvv"],
"orderNumber": 20
},
"steps": {
"order": [
"1",
"2",
"3"
],
"data": {
"1": {
"title": "tt1",
"description": "",
"screens": { "default": "true" }
},
"2": {
"title": "tt2",
"description": "",
"screens": { "default": "true" }
},
"3": {
"title": "tt3",
"description": "",
"screens": { "default": "true" }
}
}
}
};
Schema:
Joi.object({
integration: Joi.object({
module: Joi.array().items(Joi.string().valid('m').required())
}).required(),
tile: Joi.object({
title: Joi.string().required(),
text: Joi.string().required(),
icon: Joi.string().required(),
tags: Joi.array().items(Joi.string()).required(),
orderNumber: Joi.number().integer().min(1).max(255).required()
}).required(),
steps: Joi.object({
order: Joi.array().items(Joi.string()).required(),
data: Joi.object().keys({
title: Joi.string().required(),
description: Joi.string().required(),
screens: Joi.object({
default: Joi.string().valid('true', 'false').required()
}).required()
}).unknown(),
}).required()
});
But it generate error:
Validation Error: "steps.data.title" is required. "steps.data.description" is required. "steps.data.screens" is required
Please help. How can I make this schema?
Your data key is an object with keys 1, 2, and 3, each one is also an object with keys title, description, and screens.
But in your validation, your data key is an object with keys title, description, and screens, which is not correct.
You should change your steps.data validation to this:
data: Joi.object().pattern(
Joi.string().valid("1", "2", "3"),
Joi.object().keys({
title: Joi.string().required(),
description: Joi.string().required().allow(''),
screens: Joi.object({ default: Joi.string().valid('true', 'false').required() }),
})).unknown(),
}).required()
I used Joi.object().pattern to avoid duplicating the code since your object value is the same for each key.
I also changed your data.description, since you were not allowing empty strings, I just added .allow('').

Can I query objects based on SS in nested types in AppSync

I'm using AWS appsync along with DynamoDB for my project, and I have the following schema:
type List {
id: String!
title: String!
items: [String!]! ## it's of String Set (SS) type on DynamoDB
}
type Item {
id: String!
name: String!
}
I want to get a specific list along with their items. the ids of these items are in the List object. e.g
e.g:
List
{
id: "list0001",
title: "My First list",
items: ["item_001", "item_002"]
}
Item
{
id: "item_001",
name: "Item 001"
}
I want to have the following result when querying list0001
{
id: "list0001",
title: "My First list",
items: [
{
id: "item_001",
name: "Item 001"
},
{
id: "item_002",
name: "Item 002"
}
]
}
I know that I can have the list id on Item type and then I use that id to fetch the items but I want to have it as described above by getting the items from the set of the string in List type. I want to know whether it's feasible. if so, what are the mapping templates for both queries.
N.B: I'm using serverless for my project with serverless-appsync-plugin plugin.
You could set this up with two tables, ListTable and ItemTable.
The ListTable would store the information about lists. An example entry would look like:
{
"id": "list_0000",
"title": "List0"
}
The ItemTable would be used to to relate Items to the List that they belong to. An example entry would look like:
{
"id": "item_0001",
"name": "item1",
"listId": "list_0000"
}
You would need to modify your schema as follows:
type List {
id: String!
title: String!
items: [Item!]! ## A List of Items
}
type Item {
id: String!
name: String!
}
type Query {
getList(listId: ID!): List
}
This setup would request setting up 2 resolvers, 1 on getList and 1 on the field items of the type List.
Your request mapping template for getList would look like:
{
"version": "2017-02-28",
"operation": "GetItem",
"key": {
"id": $util.dynamodb.toDynamoDBJson($ctx.args.listId),
}
}
The response mapping template would be:
$util.toJson($ctx.result)
Your request mapping template for items of the type List would look like:
{
"version" : "2018-05-29",
"operation" : "Query",
"query" : {
"expression": "listId = :listId",
"expressionValues" : {
":listId" : { "S": "$ctx.source.id" }
}
}
}
The response mapping template would be:
$util.toJson($ctx.result.items)
Running the query:
query {
getList(listId: "list_0000") {
id
title
items {
id
name
}
}
}
Would have a result like:
{
"data": {
"getList": {
"id": "list_0000",
"title": "List0",
"items": [
{
"id": "item_0001",
"name": "item1"
}
]
}
}
}

netlify gatsby - markdownRemark on a list widget

I've added a new page to the config.yml file from the gatsby netlify started repo:
- name: "pages"
label: "Pages"
files:
- file: "src/pages/CV/index.md"
label: "CV"
name: "CV"
fields:
- {
label: "Template Key",
name: "templateKey",
widget: "hidden",
default: "cv-page",
}
- { label: "Name", name: "name", widget: "string" }
- { label: "Portrait", name: "portrait", widget: "image" }
- label: "Categories"
name: "categories"
widget: "list"
fields:
- { label: Title, name: title, widget: string }
- { label: "Body", name: "body", widget: "markdown" }
And then I query for the data in my cv-page component:
export const cvPageQuery = graphql`
query CVPage($id: String!) {
markdownRemark(id: { eq: $id }) {
frontmatter {
name
portrait
categories {
title
body
}
}
}
}
`;
now I would like gatsby-transformer-remark to parse the categories body from markdown to html - but the query is just returning a markdown string (for example body: "* one↵* two↵* three↵* four").
Before when I had the markdown widget directly on the page as a field, I would just query for html outside of frontmatter and the data would be there. Why is this not working with the widget being nested in a list?
Thanks for the help.
EDIT: repo with my code for reference
The gatsby-transformer-remark transform will only transform the markdown within the body of your .md file. It does not know to transform fields within your frontmatter.
pages/CV/index.md
---
templateKey: cv-page
name: Miha Šušteršič
portrait: /img/headshot.png
categories:
- body: |-
* one
* two
* three
* four
title: Work experience
- body: |-
* one
* two
* three
* four
title: Education and training
---
From query:
{
"markdownRemark": {
"html": "",
"frontmatter": {
"name": "Miha Šušteršič",
"portrait": "/img/headshot.png",
"categories": [{
"title": "Work experience",
"body": "* one\n* two\n* three\n* four"
},
{
"title": "Education and training",
"body": "* one\n* two \n* three\n* four"
}
]
}
}
}
As you can see from the query results above, your html is empty because the body is empty.

kendo ui data grid - firebase

I'm using kendo ui data grid with a firebase (rest json response). The structure can contain multiple objects. However, these objects are not in a standard array format. See my json file below:
{
"users": {
"contactdetails": {
"email": "johnlittle#email.com"
},
"firstname": "John",
"id": 1,
"surname": "Little"
}
}
I am able to read firstname and surname onto the grids column but cannot get to the email object.
This is my schema definition:
schema: {
model: {
fields: {
id: {type: "number"},
firstname: {type: "string"},
surname: {type: "string"},
email: {type: "string"}
}
}
}
As far I know, u can not specify nested object to schema model definition. One way is you can use column template for email column.
columns: [
{ field: "firstname", title: "FirstName" },
{ field: "surname", title: "Surename" },
{ title: "Email", template: "#= data.contactdetails.email #" },
],

Resources