sinclairzx81 / typebox

Json Schema Type Builder with Static Type Resolution for TypeScript
Other
4.56k stars 148 forks source link

UUID not accpeted as string #875

Closed Dhruv-Garg79 closed 1 month ago

Dhruv-Garg79 commented 1 month ago

I am trying this

typeschema = Type.Object({id: Type.String()});
this.typeSchema.Check({"id":"f00b12d9-b2af-48ab-8b00-25070afbe307"});

But keep getting this error for some reason

[{"type":54,"schema":{"format":"uuid","type":"string"},"path":"/id","value":"f00b12d9-b2af-48ab-8b00-25070afbe307","message":"Expected string"}]

I got this from the examples

const Uuid = /^(?:urn:uuid:)?[0-9a-f]{8}-(?:[0-9a-f]{4}-){3}[0-9a-f]{12}$/i;

/**
 * `[ajv-formats]` A Universally Unique Identifier as defined by [RFC 4122](https://datatracker.ietf.org/doc/html/rfc4122).
 * @example `9aa8a673-8590-4db2-9830-01755844f7c1`
 */
export function IsUuid(value: string): boolean {
    return Uuid.test(value);
}

FormatRegistry.Set('uuid', value => IsUuid(value));
sinclairzx81 commented 1 month ago

@Dhruv-Garg79 Hi, That's a strange error.

I don't seem to be able to replicate the issue locally... here's the code I'm using (you can run this locally to repro)

import { TypeCompiler } from '@sinclair/typebox/compiler'
import { Type, FormatRegistry } from '@sinclair/typebox'
import { Value } from '@sinclair/typebox/value'

FormatRegistry.Set('uuid', value => IsUuid(value))

const Uuid = /^(?:urn:uuid:)?[0-9a-f]{8}-(?:[0-9a-f]{4}-){3}[0-9a-f]{12}$/i

export function IsUuid(value: string): boolean {
  return Uuid.test(value)
}
// ...Test

const T = Type.Object({ id: Type.String({ format: 'uuid' }) })

const A = TypeCompiler.Compile(T).Check({ id: 'f00b12d9-b2af-48ab-8b00-25070afbe307' })

const B = Value.Check(T, { id: String('f00b12d9-b2af-48ab-8b00-25070afbe307') })

const C = [...Value.Errors(T, { id: String('f00b12d9-b2af-48ab-8b00-25070afbe307') })]

console.log(A, B, C) // true, true, [] - ok

So the above is fine, however the error you've posted is somewhat puzzling as the /id value passed on the error certainly appears to be a string. I'm not sure how this could happen...breakdown of the error below...

[{
  "type":54,                                         // ok:  error type string
  "schema": { "format":"uuid", "type":"string" },    // ok: a string with format
  "path": "/id",                                     // ok: path looks correct
  "value": "f00b12d9-b2af-48ab-8b00-25070afbe307",   // puzzle: but the value is string?
  "message": "Expected string"                       // ok: 
}]

Could you run a quick test on the id value to see what type it is....

console.log(typeof value.id === 'string') // should print true

... the only thing I can think of is that id somehow isn't a string....

const A = String('hello world')

const B = Object.assign(A, {}) // is it possible the id might be assigned things?

console.log(typeof A) // string - ok

console.log(typeof B) // object - not a string - but still string-like

Outside of the above guess, there's not too much else to go on. Just check there's nothing unusual about the id (i.e. some database drivers may create specialized string values for identifiers). It's a curious error, but need more information to provide additional help.

Let me know how you go. Cheers S

Dhruv-Garg79 commented 1 month ago

Hey @sinclairzx81, I was able to figure it out later on. The issue was, that the UUID from Scylla was getting passed on as int array buffer, but in the error message of typebox, it was coming as a string value, which led to the confusion.

Even in the logs it was coming up at us string value since I was using Pino logger. Until I checked with console.log, I wasn't able to figure it out.

it's working as expected now.