Event Pattern to match boolean values in Amazon EventBridge - json

I was wondering how we can match boolean type value of false instead of checking just exists?
I am unable to find anything for a boolean value in the documentation Content filtering in Amazon EventBridge event patterns
Thank you in advance
Sample Event:
"requestParameters": {
"publicAccessBlock": "",
"bucketName": "sri123publicaccess",
"PublicAccessBlockConfiguration": {
"xmlns": "http://s3.amazonaws.com/doc/2006-03-01/",
"RestrictPublicBuckets": true,
"BlockPublicPolicy": true,
"BlockPublicAcls": true,
"IgnorePublicAcls": true
},
"Host": "s3.amazonaws.com"
},
Event Pattern:
{
"source": ["aws.s3"],
"detail-type": ["AWS API Call via CloudTrail"],
"detail": {
"eventSource": ["s3.amazonaws.com"],
"eventName": ["PutBucketPublicAccessBlock", "DeleteBucketPublicAccessBlock"],
"$or": [{
"RestrictPublicBuckets": [{
"exists": false
}]
}, {
"BlockPublicPolicy": [{
"exists": false
}]
}, {
"BlockPublicAcls": [{
"exists": false
}]
}, {
"IgnorePublicAcls": [{
"exists": false
}]
}]
}
}

The pattern to match boolean values is the one you'd expect:
"RestrictPublicBuckets": [true]
Beyond that, for your pattern to match your event, you must also properly nest the attributes:
"detail": {
"requestParameters": {
"PublicAccessBlockConfiguration": {
"$or": [
{"RestrictPublicBuckets": [true]},
{"BlockPublicPolicy": [true]},
{"BlockPublicAcls": [true]},
{"IgnorePublicAcls": [true]},
]
}
},
}

Related

Get Names of Children in JSONPath

I wanna get children names of this JSON via JSONPath:
{
"status": "success",
"data": {
"blaze": {
"status": false
},
"fire": {
"status": true
},
"thunder": {
"status": false
}
}
}
and Output must be this:
["blaze", "fire", "thunder"]
but when i use data.*, i get incorrect and different response. I just want the correct parameter, because i'm using on several programming languages and i don't use the other referrences/functions.
Indeed data.* will return you the value of the objects inside the data object of your given array.
Your json -
{
"status": "success",
"data": {
"blaze": {
"status": false
},
"fire": {
"status": true
},
"thunder": {
"status": false
}
}
}
and the output of $.data.* expression -
[
{
"status": false
},
{
"status": true
},
{
"status": false
}
]
To just access the keys of data object you have to use ~ operator.
Using the expression $.data.*~ you will get the output as -
[
"blaze",
"fire",
"thunder"
]
Here you can test it. Credit to: This answer

How do you reference other property values in JSON schema?

I have a JSON schema with 2 properties, minimumTolerance and maximumTolerance. I need to make sure that the value of maximumTolerance is not smaller than minimumTolerance & vice versa. Is this possible in JSON schema?
Here is an example of what I'd like to be able to do:
{
"$schema": "http://json-schema.org/draft-06/schema#",
"title": "MinMax",
"description": "Minum & Maximum",
"type": "object",
"properties": {
"minimumTolerance":{
"type": "number"
"maximum":{
"$ref":"maximumTolerance"
}
}
"maximumTolerance":{
"type": "number"
"minumum": {
"$ref":"minimumTolerance"
}
}
}
As of Draft-7 of the specification there is no way to do this with JSON Schema.
If you are using AJV You can do this using $data and relative JSON pointers. Example:
low: {
type: 'integer',
maximum: {
$data: '1/high',
},
exclusiveMaximum: true,
},
high: {
type: 'integer',
minimum: {
$data: '1/low',
},
exclusiveMinimum: true,
},
Yes, but it may not be the dynamic answer you are looking for...put in the values of min and max for the range expected:
"MinimumTolerance": {
"type": "number",
"minimum": 0,
"maximum": 6000,
},
"MaximumTolerance": {
"type": "number",
"minimum": 6001,
},

Using JSON API Serializer to create more complicated JSON

The examples here don't go nearly far enough in explaining how to produce a more complicated structure...
If I want to end up with something like:
{
"data": {
"type": "mobile_screens",
"id": "1",
"attributes": {
"title": "Watch"
},
"relationships": {
"mobile_screen_components": {
"data": [
{
"id": "1_1",
"type": "mobile_screen_components"
},
{
"id": "1_2",
"type": "mobile_screen_components"
},
...
]
}
}
},
"included": [
{
"id": "1_1",
"type": "mobile_screen_components",
"attributes": {
"title": "Featured Playlist",
"display_type": "shelf"
},
"relationships": {
"playlist": {
"data": {
"id": "938973798001",
"type": "playlists"
}
}
}
},
{
"id": "938973798001",
"type": "playlists",
"relationships": {
"videos": {
"data": [
{
"id": "5536725488001",
"type": "videos"
},
{
"id": "5535943875001",
"type": "videos"
}
]
}
}
},
{
"id": "5536725488001",
"type": "videos",
"attributes": {
"duration": 78321,
"live_stream": false,
"thumbnail": {
"width": 1280,
"url":
"http://xxx.jpg?pubId=694940094001",
"height": 720
},
"last_published_date": "2017-08-09T18:26:04.899Z",
"streams": [
{
"url":
"http://xxx.m3u8",
"mime_type": "MP4"
}
],
"last_modified_date": "2017-08-09T18:26:27.621Z",
"description": "xxx",
"fn__media_tags": [
"weather",
"personality"
],
"created_date": "2017-08-09T18:23:16.830Z",
"title": "NOAA predicts most active hurricane season since 2010",
"fn__tve_authentication_required": false
}
},
...,
]
}
what is the most simple data structure and serializer I can set up?
I get stumped after something like:
const mobile_screen_components = responses.map((currentValue, index) => {
id[`id_${index}`];
});
const dataSet = {
id: 1,
title: 'Watch',
mobile_screen_components,
};
const ScreenSerializer = new JSONAPISerializer('mobile_screens', {
attributes: ['title', 'mobile_screen_components'],
mobile_screen_components: {
ref: 'id',
}
});
Which only gives me:
{
"data": {
"type": "mobile_screens",
"id": "1",
"attributes": { "title": "Watch" },
"relationships": {
"mobile-screen-components": {
"data": [
{ "type": "mobile_screen_components", "id": "1_0" },
{ "type": "mobile_screen_components", "id": "1_1" },
{ "type": "mobile_screen_components", "id": "1_2" },
{ "type": "mobile_screen_components", "id": "1_3" },
{ "type": "mobile_screen_components", "id": "1_4" },
{ "type": "mobile_screen_components", "id": "1_5" }
]
}
}
}
}
I have no idea how to get the "included" sibling to "data." etc.
So, the question is:
what is the most simple data structure and serializer I can set up?
Below is the simplest object that can be converted to JSON similar to JSON in the question using jsonapi-serializer:
let dataSet = {
id: '1',
title: 'Watch',
mobile_screen_components: [
{
id: '1_1',
title: 'Featured Playlists',
display_type: 'shelf',
playlists: {
id: 938973798001,
videos: [
{
id: 5536725488001,
duration: 78321,
live_stream: false
},
{
id: 5535943875001,
duration: 52621,
live_stream: true
}
]
}
}
]
};
To serialize this object to JSON API, I used the following code:
let json = new JSONAPISerializer('mobile_screen', {
attributes: ['id', 'title', 'mobile_screen_components'],
mobile_screen_components: {
ref: 'id',
attributes: ['id', 'title', 'display_type', 'playlists'],
playlists: {
ref: 'id',
attributes: ['id', 'videos'],
videos: {
ref: 'id',
attributes: ['id', 'duration', 'live_stream']
}
}
}
}).serialize(dataSet);
console.log(JSON.stringify(json, null, 2));
The first parameter of JSONAPISerializer constructor is the resource type.
The second parameter is the serialization options.
Each level of the options equals to the level of the nested object in serialized object.
ref - if present, it's considered as a relationships.
attributes - an array of attributes to show.
Introduction
First of all we have to understand the JSON API document data structure
[0.1] Refering to the top level (object root keys) :
A document MUST contain at least one of the following top-level
members:
data: the document’s “primary data”
errors: an array of error objects
meta: a meta object that contains non-standard meta-information.
A document MAY contain any of these top-level members:
jsonapi: an object describing the server’s implementation
links: a links object related to the primary data.
included: an array of resource objects that are related to the primary data and/or each other (“included resources”).
[0.2]
The document’s “primary data” is a representation of the resource or
collection of resources targeted by a request.
Primary data MUST be either:
a single resource identifier object, or
null, for requests that target single resources
an array of resource identifier
objects, or an empty array ([]), for reqs. that target
collections
Example
The following primary data is a single resource object:
{
"data": {
"type": "articles",
"id": "1",
"attributes": {
// ... this article's attributes
},
"relationships": {
// ... this article's relationships
}
}
}
In the (jsonapi-serializer) documentation : Available serialization option (opts argument)
So in order to add the included (top-level member) I performed the following test :
var JsonApiSerializer = require('jsonapi-serializer').Serializer;
const DATASET = {
id:23,title:'Lifestyle',slug:'lifestyle',
subcategories: [
{description:'Practices for becoming 31337.',id:1337,title:'Elite'},
{description:'Practices for health.',id:69,title:'Vitality'}
]
}
const TEMPLATE = {
topLevelLinks:{self:'http://example.com'},
dataLinks:{self:function(collection){return 'http://example.com/'+collection.id}},
attributes:['title','slug','subcategories'],
subcategories:{ref:'id',attributes:['id','title','description']}
}
let SERIALIZER = new JsonApiSerializer('pratices', DATASET, TEMPLATE)
console.log(SERIALIZER)
With the following output :
{ links: { self: 'http://example.com' },
included:
[ { type: 'subcategories', id: '1337', attributes: [Object] },
{ type: 'subcategories', id: '69', attributes: [Object] } ],
data:
{ type: 'pratices',
id: '23',
links: { self: 'http://example.com/23' },
attributes: { title: 'Lifestyle', slug: 'lifestyle' },
relationships: { subcategories: [Object] } } }
As you may observe, the included is correctly populated.
NOTE : If you need more help with your dataSet, edit your question with the original data.

How to add tag inside a nest depending on a variable while keeping rest of json

I'm failry new to jq and I've learned to do most things on my own but I'm hitting my head on my keyboard for this one. Look at the following json
JSON:
{
"importType": "Upsert",
"immediateDeployment": false,
"isIgnoreNulls": false,
"isOverwriteNullsOnly": false,
"isPreferredandSync": false,
"outputLayout": {
"fields": [
{
"name": "TEMP_KEY",
"type": "String",
"length": "2000",
"displayName": "TEMP_KEY"
},
{
"name": "LoadSeqNum",
"type": "String",
"length": "2000",
"displayName": "LoadSeqNum"
}
]
}
}
What I'm trying to do is inside of .outputLayout.fields[] I want to create a new pair called "isIdentifier" where it is true if the .outputLayout.fields[].name is LoadSeqNum and false if it's not but I need to keep the rest of the json just as it is. So target should look as following:
Goal:
{
"importType": "Upsert",
"immediateDeployment": false,
"isIgnoreNulls": false,
"isOverwriteNullsOnly": false,
"isPreferredandSync": false,
"outputLayout": {
"fields": [
{
"name": "TEMP_KEY",
"type": "String",
"length": "2000",
"displayName": "TEMP_KEY"
"isIdentifier": false
},
{
"name": "LoadSeqNum",
"type": "String",
"length": "2000",
"displayName": "LoadSeqNum"
"isIdentifier": true
}
]
}
}
I tried this:
jq '.outputLayout.fields[] | . + {"isIdentifier": (if (.name)=="LoadSeqNum" then true else false end)}'
But of course I'm missing all the higher level things. When I try to do:
.outputLayout.fields[].isIdentifier=(if (.outputLayout.fields[].name)=="LoadSeqNum" then true else false end)
I get the whole thing twice, once with both true and the other one with both false. I understand why it's doing that but I'm having a tough time figuring out what would work. Any help or point in right direction?
.outputLayout.fields[] |= (.isIdentifier = (.displayName == "LoadSeqNum") )
Or equivalently but perhaps a little less cryptically:
.outputLayout.fields |= map( .isIdentifier = (.displayName == "LoadSeqNum") )

Make an optional option required when another one is defined in JSON Schema

I would like to define certificate and privateKey required when secure flag is set to true. Is this possible to achieve?
{
type: 'object',
properties: {
'secure': {
title: 'Serve files over HTTPS',
description: 'Flag telling whether to serve contents over HTTPS and WSS',
type: 'boolean',
default: false
},
'certificate': {
title: 'Certificate file',
description: 'Location of the certificate file',
type: 'string'
},
'privateKey': {
title: 'Private key file',
description: 'Location of the private key file',
type: 'string'
}
}
You can use 'dependencies' keyword.
{
dependencies: {
secure: ['certificate', 'privateKey']
}
}
You can even specify the schema the data should match when secure is present:
{
dependencies: {
secure: {
properties: {
certificate: {
type: 'string'
}
privateKey: {
type: 'string'
}
},
required: ['certificate', 'privateKey']
}
}
}
Are you asking to have the schema content-dependent? I mean "what the schema allows depends on what's in the target (json) content"?
You can't do that.
There is a way, but it isn't pretty. You need to use the anyOf keyword to define how you want it to validate when secure is true and how you want it to validate when secure is false.
{
"type": "object",
"properties": {
"secure": {
"title": "Serve files over HTTPS",
"description": "Flag telling whether to serve contents over HTTPS and WSS",
"type": "boolean"
}
},
"anyOf": [
{
"type": "object",
"properties": {
"secure": {
"enum": [true]
},
"certificate": {
"title": "Certificate file",
"description": "Location of the certificate file",
"type": "string"
},
"privateKey": {
"title": "Private key file",
"description": "Location of the private key file",
"type": "string"
}
},
"required": ["certificate", "privateKey"]
},
{
"type": "object",
"properties": {
"secure": {
"enum": [false]
}
}
}
]
}