GraphQL: Validating array elements are in list - ruby

I have the following argument in my Ruby GQL:
argument :countryCode, [String], required: false, validates: {inclusion: {in: COUNTRY_CODES}}, prepare: :strip
What I want this to achieve is to allow an array of Strings to be used, and each value in the array to be one of COUNTRY_CODES
However, this returns the exception "is not included in the list". What is wrong here?

Better way for this case create enum with countries, but in this case client must send enum hardcoded country code, other way use validator like dry-validation of similar

Related

Access value from a Netsuite hash, Enumerator

Hi I am trying to extract a value from a Netsuite hash inside custom fields, and some others, which typically look like this - `
"custbody_delivery_ticket_number"=>
{
"script_id"=>"custbody_delivery_ticket_number",
"internal_id"=>"2701",
"type"=>"platformCore:DateCustomFieldRef",
"attributes"=> {
"value"=>"123abc"
}
}` and want the value of it inside of attributes.
Have tried many different ways, but one in particular -
delivery_ticket_number: "#{netsuite_sales_orders.custom_field_list.custom_fields.select['custbody_nef_meter_ticket_number']['attributes']['value']}",
throws error for class Enumerator, NoMethodError: undefined method `[]' for #Enumerator:0x00005589ec778730 which indicates may be getting close, but doing something wrong.
If anyone has any idea how to get values from these kind of hashes?
(Am told by the system admin that it is the correct custbody identifier)
Many Thanks
Eventually fixed this, grabbing Netsuite custom fields with a select of script_id by name,and map as below:
delivery_date:netsuite_sales_order.custom_fields_list.custom_fields.select { |field| field.script_id == 'custbody_delivery_date' }.map { |field| field.value }.first
First selecting the script_id by unique name, then mapping to the value. Was able to get any custom field like this, preferable as they can move and might not have the same index if use an index to grab them, fetching an incorrect value. This way ensures getting the correct data even if the item is moved up or down in the custom fields list.
Thanks for everyones help!

Fluent validation: either have both fields with value or both must be empty

I am trying to use fluent validation to validate the following rule:
Rule: Either both LocationLattitude and LocationLongitude must contain
a value or they must both be empty. You cannot have one field with
value and the other empty.
Can someone help me out with this?
This is the code I have written:
RuleFor(ar => ar.LocationLattitude)
.CasCade(CascadeMode.StopFirstFailure);
RuleFor(ar => ar.LocationLongitude)
.CasCade(CascadeMode.StopFirstFailure);

Powerautomate Parsing JSON Array

I've seen the JSON array questions here and I'm still a little lost, so could use some extra help.
Here's the setup:
My Flow calls a sproc on my DB and that sproc returns this JSON:
{
"ResultSets": {
"Table1": [
{
"OrderID": 9518338,
"BasketID": 9518338,
"RefID": 65178176,
"SiteConfigID": 237
}
]
},
"OutputParameters": {}
}
Then I use a PARSE JSON action to get what looks like the same result, but now I'm told it's parsed and I can call variables.
Issue is when I try to call just, say, SiteConfigID, I get "The output you selected is inside a collection and needs to be looped over to be accessed. This action cannot be inside a foreach."
After some research, I know what's going on here. Table1 is an Array, and I need to tell PowerAutomate to just grab the first record of that array so it knows it's working with just a record instead of a full array. Fair enough. So I spin up a "Return Values to Virtual Power Agents" action just to see my output. I know I'm supposed to use a 'first' expression or a 'get [0] from array expression here, but I can't seem to make them work. Below are what I've tried and the errors I get:
Tried:
first(body('Parse-Sproc')?['Table1/SiteConfigID'])
Got: InvalidTemplate. Unable to process template language expressions in action 'Return_value(s)_to_Power_Virtual_Agents' inputs at line '0' and column '0': 'The template language function 'first' expects its parameter be an array or a string. The provided value is of type 'Null'. Please see https://aka.ms/logicexpressions#first for usage details.'.
Also Tried:
body('Parse-Sproc')?['Table1/SiteconfigID']
which just returns a null valued variable
Finally I tried
outputs('Parse-Sproc')?['Table1']?['value'][0]?['SiteConfigID']
Which STILL gives me a null-valued variable. It's the worst.
In that last expression, I also switched the variable type in the return to pva action to a string instead of a number, no dice.
Also, changed 'outputs' in that expression for 'body' .. also no dice
Here is a screenie of the setup:
To be clear: the end result i'm looking for is for the system to just return "SiteConfigID" as a string or an int so that I can pipe that into a virtual agent.
I believe this is what you need as an expression ...
body('Parse-Sproc')?['ResultSets']['Table1'][0]?['SiteConfigID']
You can see I'm just traversing down to the object and through the array to get the value.
Naturally, I don't have your exact flow but if I use your JSON and load it up into Parse JSON step to get the schema, I am able to get the result. I do get a different schema to you though so will be interesting to see if it directly translates.

Return True if Array Contains a Specific String - JSONata

Is there a way in JSONata to have a function return TRUE if it finds a specific string within a provided array? For example I have an array of colors:
const myArray = [red,blue,green,pink]
I am trying to figure out an expression that would search that array for "blue" and return true if it finds the value.
On the JSONata documentation, I found a function called $boolean(arg)that I think I would need to use but I'm not sure how to implement it. The documentation shows an argument type option as "array: contains a member that casts to true", but I can't really tell how to implement it.
Would it be as simple as $boolean(myArray, "blue")?
The in operator is what you need. See https://docs.jsonata.org/comparison-operators#in-inclusion
In your case, the expression "blue" in myArray will return true. See https://try.jsonata.org/r0q7GnSOh
edit: Thought this was python, but maybe you could use something similar for JSONata
you can make a for loop with an if condition to check your condition
listOfStrings = ['red','green','blue']
for strings in listOfStrings:
if listOfStrings[strings] == 'blue':
return True

Parsing Grape params in an XML request with complex nested body including attributes

I'm trying to achieve this with Grape. I need to send relatively complex XML bodies including element attributes, something like shown in this sample:
<Travelers>
<Traveler>
<AnonymousTraveler>
<PTC Quantity="1">ADT</PTC>
</AnonymousTraveler>
</Traveler>
</Travelers>
And I'm trying to define nested params following a popular convention when converting XML to JSON (here just to ruby) which looks like this:
optional :Travelers, type: Array do
requires :Traveler, type: Hash do
optional :AnonymousTraveler, type: Hash do
requires :PTC, type: Hash do
requires :_Quantity, type: Integer
requires :__text, type: String
end
end
end
end
But obviously the XML parser is not following this underscores-based convention and throws the error messages:
[Travelers][Traveler][AnonymousTraveler][PTC][_Quantity] is missing
[Travelers][Traveler][AnonymousTraveler][PTC][__text] is missing
Any clue on how to intercept XML body parameters parser and include this convention logic?
Found out that I was not following the correct convention.
Nokogiri parser follows this one:
requires :PTC, type: Hash do
requires :Quantity, type: Integer
requires :__content__, type: String
end
This means attributes are parsed like plain children elements and element value or text is parsed as child element called __content__.
Hope it helps.

Resources