Optional locales appear not te be optional when using Node client

Hi all,

For a client we are working on an importer script to migrate the content from the clients website.

I have a model, lets call it Article. Article has a slug, body and title which are optionally translatable.
Via the web interface I can create an Article only in en or only in nl (one of the locales I’m using).
However when I’m trying to create a record via the Node.js client (datocms-client) I’m getting 422 INVALID_FIELD:

"ApiException: 422 INVALID_FIELD (details: {"field":"slug.en","field_label":"Slug","field_type":"slug","code":"VALIDATION_REQUIRED","options":{}})    at /importer/node_modules/datocms-client/lib/Client.js:135:33 ...)"

When I inspect the error it tells me "VALIDATION_REQUIRED".

{
  "code": "VALIDATION_REQUIRED",
  "field": "slug.en",
  "field_label": "Slug",
  "field_type": "slug"
}

Same error for the other translated fields.

From this file I learned that this means that the field is required. Which confuses me because I am providing values for all fields.
So I expect that the problem is related to me passing null for the unavailable locale like so:

slug: {
  nl: slug,
  en: null,
},
title: {
  nl: title,
  en: null,
},
// ...

When I’m not providing the other locale as null I’m getting MISSING_LOCALES errors.

This issue is blocking the import of the data and we cannot deliver to our client without the data imported.
I’m very eager to hear what the correct course of action would be in this scenario.

Furthermore I’m having a really hard time using the Node.js client due to incomplete documentation.
What I’m missing are:

  • an overview of all possible error codes and what they mean, right now I need to use the translations
  • documentation on required utils such as buildModularBlock which is only referenced in this product update
  • documentation on creating different types of modular content
  • Consistency in documentation

hello @ramon.gebben

you are right about your complaint and we are finalising a new revision of our docs that should clarify most of your points.

Regarding the main concern, yes if they are optional you should still send all the attributes with an empty value. If the locales are mandatory you should send all the locales as well.

Until there isn’t a better doc online, please try inspecting the calls that we do with our CMS as we are using the same REST API that we expose, so that should clarify. Sorry for this!

@mat_jack1 Because I’m using the Node.js client there is a serialisation step happening causing my input to differ from what you’re doing in the CMS.

I did have a look to see what you are posting to the REST API and looks like you’re completely omitting the “unavailable locale” which I am not allowed to do through the Node.js client.

image

Once I omit the other locales I get a MISSING_LOCALES error.

are you checking the new record creation or the update? on the update you can send only the changes instead.

We might change this in the future as it’s confusing :frowning:

This was a create. I’ve recreated the request and copied the cURL:

curl 'https://site-api.datocms.com/items' \
  -H 'authority: site-api.datocms.com' \
  -H 'pragma: no-cache' \
  -H 'cache-control: no-cache' \
  -H 'accept: application/vnd.api+json' \
  -H 'authorization: Bearer xxxI' \
  -H 'x-site-domain: xxx.admin.datocms.com' \
  -H 'x-api-version: 3' \
  -H 'user-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.122 Safari/537.36' \
  -H 'content-type: application/json' \
  -H 'origin: https://xxx.admin.datocms.com' \
  -H 'sec-fetch-site: same-site' \
  -H 'sec-fetch-mode: cors' \
  -H 'sec-fetch-dest: empty' \
  -H 'referer: https://xxx.admin.datocms.com/editor/item_types/211297/items/new' \
  -H 'accept-language: en-GB,en-US;q=0.9,en;q=0.8' \
  --data-binary '{"data":{"type":"item","attributes":{"title":{"en":"This is article"},"slug":{"en":"this-is-article"},"date":"2020-05-18T00:00:00+02:00","hero_image":{"upload_id":"1736778","alt":null,"title":null,"custom_data":{}},"intro":{"en":"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. "},"modular_content":{"en":[{"type":"item","attributes":{"body":"<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.&nbsp;</p>"},"relationships":{"item_type":{"data":{"type":"item_type","id":"234301"}}}},{"type":"item","attributes":{"image":{"upload_id":"1736827","alt":null,"title":null,"custom_data":{}}},"relationships":{"item_type":{"data":{"type":"item_type","id":"225725"}}}}]},"tag":"4709068","seo":{"en":null},"gatsbypreview":null},"relationships":{"item_type":{"data":{"type":"item_type","id":"211297"}}}}}' \
  --compressed

This is the body prettified:

{
  "data": {
    "type": "item",
    "attributes": {
      "title": {
        "en": "This is article"
      },
      "slug": {
        "en": "this-is-article"
      },
      "date": "2020-05-18T00:00:00+02:00",
      "hero_image": {
        "upload_id": "1736778",
        "alt": null,
        "title": null,
        "custom_data": {}
      },
      "intro": {
        "en": "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. "
      },
      "modular_content": {
        "en": [
          {
            "type": "item",
            "attributes": {
              "body": "<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.&nbsp;</p>"
            },
            "relationships": {
              "item_type": {
                "data": {
                  "type": "item_type",
                  "id": "234301"
                }
              }
            }
          },
          {
            "type": "item",
            "attributes": {
              "image": {
                "upload_id": "1736827",
                "alt": null,
                "title": null,
                "custom_data": {}
              }
            },
            "relationships": {
              "item_type": {
                "data": {
                  "type": "item_type",
                  "id": "225725"
                }
              }
            }
          }
        ]
      },
      "tag": "4709068",
      "seo": {
        "en": null
      },
      "gatsbypreview": null
    },
    "relationships": {
      "item_type": {
        "data": {
          "type": "item_type",
          "id": "211297"
        }
      }
    }
  }
}

Sorry but that cURL works, right? I’ve just tried and looks good to me. Do you have problems with the JS code instead?

Yes indeed when trying to create the same request using the client I get MISSING_LOCALES error.

Did you guys do an update since this morning?
I did not change anything to my importer script and when I ran it again to copy some of the output for you it started accepting my request. :confused:

Very confusing this.

No we didn’t! Not sure what happened, maybe you changed something on your model settings?

Not to my knowledge. I’ll check within the team.
Everything seems to work OK now. Thanks anyways for the support.

1 Like