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

Literal types in SDK give Typescript errors on createItem and no validation on updateItem #21599

Open
martijn-dev opened this issue Feb 27, 2024 · 0 comments

Comments

@martijn-dev
Copy link

martijn-dev commented Feb 27, 2024

Describe the Bug

Up until directus SDK V14 I had the following situation/Schema:

export interface Users {
  id: string;
  name: string;
  endvisit_at?: string | null;
  created_at: string;
  properties: any;
}

export interface CustomDirectusSchema {
  users: Users[];
}

const client = createDirectus<CustomDirectusSchema>(
      process.env.DIRECTUS_URL
    ).with(rest());

client.request(
      readItems('users', {
        filter: {
          created_at: {
            _lte: `$NOW`
          }
        }
      })
    );

But since SDK V15 the filter checks on string fields became more strict so this _lte on a string field is not allowed anymore.

So I dived into the docs and found out there are literal types now datetime, csv and json.

So I changed my Schema to the following:

export interface Users {
  id: string;
  name: string;
  endvisit_at?: 'datetime' | null;
  created_at: 'datetime';
  properties: 'json';
}

export interface CustomDirectusSchema {
  users: Users[];
}

Victory! Now I'm allowed to use the _lte in the filter query again.

Let me know if the implementation of this Schema is actually correct 😅

But now the issue, the typing on a createItem is expecting a 'datetime' or 'json' type instead of an actual string/object:

   client.request(
      createItem('users', {
        endvisit_at: '2024-02-27T11:27:25+00:00',
        properties: {
          test: 'object'
        }
      })
    );

The above gives the following errors
Type '"test"' is not assignable to type '"datetime"'.
Type '{ test: string; }' is not assignable to type '"json"'.

Next to that, if I do an updateItem action, there is no validation at all and I'm allowed to do the following (which is not correct):

  client.request(
      updateItem('users', 'test', {
        endvisit_at: { wrong: 'input' },
        properties: 'not a json field'
      })
    );

To Reproduce

Setup the following Schema with a minimal Directus SDK instance:

export interface Users {
  id: string;
  name: string;
  endvisit_at?: 'datetime' | null;
  created_at: 'datetime';
  properties: 'json';
}

export interface CustomDirectusSchema {
  users: Users[];
}

const client = createDirectus<CustomDirectusSchema>(
      process.env.DIRECTUS_URL
    ).with(rest());

Try:

   client.request(
      createItem('users', {
        endvisit_at: '2024-02-27T11:27:25+00:00',
        properties: {
          test: 'object'
        }
      })
    );

which is giving Typescript errors, while the input is correct.

and

  client.request(
      updateItem('users', 'test', {
        endvisit_at: { wrong: 'input' },
        properties: 'not a json field'
      })
    );

which is not giving any errors, while the input is incorrect.

Let me know if any more clarification is needed!

Directus Version

@directus/sdk@15.0.1

Hosting Strategy

Self-Hosted (Docker Image)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: 📋 Backlog
Development

No branches or pull requests

3 participants