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

Improve record handling #25

Closed
sebastianwessel opened this issue Jun 22, 2024 · 3 comments · Fixed by #34
Closed

Improve record handling #25

sebastianwessel opened this issue Jun 22, 2024 · 3 comments · Fixed by #34
Assignees
Labels
bug Something isn't working

Comments

@sebastianwessel
Copy link
Owner

Improve handling of record links

@sebastianwessel sebastianwessel self-assigned this Jun 22, 2024
@CiskaLV
Copy link
Contributor

CiskaLV commented Jul 9, 2024

You could try making a RecordId Custom schema for zod i have been using this one i found

export const ZRecordIdInstanceOf = z.instanceof(RecordId);

export function recordId<Table extends string = string>(table?: Table) {
  return z.custom<RecordId<`${Table}`>>(
    val => {
      const instanceOfCheck = ZRecordIdInstanceOf.safeParse(val);
      const tableCheck = table ? val?.tb === table : true;
      return instanceOfCheck.success && tableCheck;
    },
    val => {
      let msgArray: string[] = [];
      const instanceOfCheck = ZRecordIdInstanceOf.safeParse(val);
      if (!instanceOfCheck.success) msgArray.push(`Must be a RecordId class`);

      const tableCheck = table ? val?.tb === table : true;
      if (!tableCheck) msgArray.push(`RecordId must be of type '${table}', not '${val?.tb}'`);

      return { message: msgArray.join("; ") };
    }
  );
}

its from the SurrealDb discord link to message by Chris Rudman

@sebastianwessel sebastianwessel added the bug Something isn't working label Jul 9, 2024
@sebastianwessel
Copy link
Owner Author

Thx @CiskaLV ! This looks good.
For sure 100% better than the current invalid generated schema

@sebastianwessel sebastianwessel linked a pull request Jul 9, 2024 that will close this issue
sebastianwessel added a commit that referenced this issue Jul 9, 2024
@sebastianwessel
Copy link
Owner Author

sebastianwessel commented Jul 9, 2024

Version 2.4.0 has been released, adopting this approach.

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

Successfully merging a pull request may close this issue.

2 participants