webroo / dummy-json

Generates random dummy JSON data in Node.js
MIT License
380 stars 61 forks source link

Missing commas #40

Closed jade290 closed 3 years ago

jade290 commented 3 years ago

Hi, I love this project. I'm using the template below and noticed that for some of my fields, the comma is omitted after running the dummy-json. Fields "farm_number" and "activity" do not have trailing commas and this causes an issue with the json being valid. Why does dummy-json ignore the commas here?

{ "object_type": { {{#repeat min=0 max=1}} "fsa_access_level": {{int 1 10}} , {{/repeat}} "internal_view_only_doc":true , "is_ptfm_auto_upload":false , "is_allow_multiple":false , "object_type_name": "AD-1026" , "edition_date": "2012-02-06" , "title": "GENERATED Highly Erodible Land" , }, "object_specific": { "affiliated_persons": "{{firstName}} {{lastName}}, {{firstName}} {{lastName}}" , "farm_tract_number": {{int 0 100}} , {{#repeat min=0 max=1}} "farm_number": {{int 10000 10000000}} , {{/repeat}} "county_name": "{{city}}" , {{#repeat min=0 max=1}} "activity": "{{random "Growing" "Harvesting" "Fishing" "Resting"}}" , {{/repeat}} "crop_year": "{{date "1900" "2021" "YYYY"}}" , } }

webroo commented 3 years ago

There's two problems with your template, the first is simple: there's trailing commas at the end of the property list in the object_type and object_specific objects, you can simply remove those.

The second is happening due to the way you're using the #repeat helper. I've not actually seen it used like this before - I assume you're trying to randomly add/remove properties like fsa_access_level? It's a clever idea!

The problem is the repeat helper is trying to magically add/remove commas based on how many items it repeats. One of its rules is to always remove the comma if it's the last item, but it also removes the comma if there's only one item, which unfortunately is true in your case. I can try and write a fix for this use case but in the meantime you can workaround it by using the comma=false attribute to disable the magic comma code. For example: {{#repeat min=0 max=1 comma=false}}

Here's a version of your template with both the fixes applied, it should generate valid JSON:

{ "object_type": { {{#repeat min=0 max=1 comma=false}} "fsa_access_level": {{int 1 10}} , {{/repeat}} "internal_view_only_doc":true , "is_ptfm_auto_upload":false , "is_allow_multiple":false , "object_type_name": "AD-1026" , "edition_date": "2012-02-06" , "title": "GENERATED Highly Erodible Land" }, "object_specific": { "affiliated_persons": "{{firstName}} {{lastName}}, {{firstName}} {{lastName}}" , "farm_tract_number": {{int 0 100}} , {{#repeat min=0 max=1 comma=false}} "farm_number": {{int 10000 10000000}} , {{/repeat}} "county_name": "{{city}}" , {{#repeat min=0 max=1 comma=false}} "activity": "{{random "Growing" "Harvesting" "Fishing" "Resting"}}" , {{/repeat}} "crop_year": "{{date "1900" "2021" "YYYY"}}" } }

jade290 commented 3 years ago

This is an excellent solution! Thank you!

Yes, I had a requirement to show entire properties randomly. So, I had to build a string json object and a switch statement to insert string properties at certain indices and determine if the field is required or optional, then determine if it goes into object_type or object_specific then handle nested fields. I couldn't have done it without this. This library is AWESOME!