Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to update item with new fields in the default section #109

Open
Jackman3005 opened this issue Nov 21, 2024 · 2 comments
Open

Unable to update item with new fields in the default section #109

Jackman3005 opened this issue Nov 21, 2024 · 2 comments
Labels
bug Something isn't working

Comments

@Jackman3005
Copy link

Jackman3005 commented Nov 21, 2024

Scenario & Reproduction Steps

import {createClient, ItemFieldType} from '@1password/sdk';

const op = await createClient({
  auth: '...',
  integrationVersion: '1',
  integrationName: 'test-script',
});
const item = await op.items.get(VAULT_ID, matchingItem.id);
await op.items.put({
  ...item, 
  fields: [
    {
      id: genId(),
      title: itemField.fieldName,
      value: itemField.fieldValue,
      fieldType: ItemFieldType.Concealed,
      sectionId: undefined, // tried '', null, undefined, and omitting the sectionId field
    },
  ],
});

I've found this API tough to use... First, I have to create my own field id, which feels like none of my business to do. I'm just generating a 26 length random string of upper/lower/number characters (see below)... This however, does seem to satisfy the error I was getting when I didn't provide a field id: data did not match any variant of untagged enum Invocation.

Second and what is currently blocking me, is that the claim in the sectionId documentation does not seem to be working correctly. See ItemField definition:

export interface ItemField {
    /** The field's ID */
    id: string;
    /** The field's title */
    title: string;
    /** The ID of the section containing the field. Built-in fields such as usernames and passwords don't require a section. */
    sectionId?: string;
    /** The field's type */
    fieldType: ItemFieldType;
    /** The string representation of the field's value */
    value: string;
    /** Field type-specific attributes. */
    details?: ItemFieldDetails;
}

Reading this again now... maybe it is because it is not a Built-in field? However, I do not want these in a section, I want them at the same level as the built-in fields like I can create in the 1pass desktop client...

Actual Behavior

invalid user input: encountered the following errors: custom field with id 'nLs9cXU2bD0Xg1Z0vBWpBv4Yrb' must be part of a section

Expected Behavior

The field can be created when omitting the sectionId or when setting it to undefined.

SDK version

0.1.3

Additional information

Here is the code for genId in case others find it useful. Or maybe the sdk could provide a function to generate IDs? Or maybe they can just be omitted when creating fields. I know this is not cryptographically ideal, but I suppose it does not matter as much for IDs? Also just chose 26 as the length as I found another id was that long.

const chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890";
const genId = () =>
  new Array(26)
    .fill("")
    .map(() => {
      return chars[Math.floor(Math.random() * chars.length)];
    })
    .join("");
@Jackman3005 Jackman3005 added the bug Something isn't working label Nov 21, 2024
@Jackman3005
Copy link
Author

I've gotten around this by just printing out CLI commands to use instead...

op item edit --vault 'MY_VAULT' 'MY_ITEM_NAME' 'MY_FIELD_NAME=MY_FIELD_VALUE'

For now this is sufficient as I just needed to do a one-time import of existing development values in a .env file as we are switching to 1password security automation.

@Jackman3005
Copy link
Author

Just noticed when I retrieved an item that had non-builtin fields in what I refer to as the 'default' section, or just fields outside any section. The item provided a single section in the sections array which had '' for both the id and name...

Do I need to first create a section with an empty ID and/or name to replicate the behavior of adding a field to the default section in the Desktop App?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant