-
Notifications
You must be signed in to change notification settings - Fork 141
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
Migrate to regexp vs string type in pattern #819
Comments
It's not really clear to me what the import { inspect } from 'node:util'
import { type Static, Type } from '@sinclair/typebox'
import { TypeCompiler } from '@sinclair/typebox/compiler'
const Schema = Type.RegExp(/^[a-z]$/i)
// infers: type Schema = string
type Schema = Static<typeof Schema>
const typeCheck = TypeCompiler.Compile(Schema)
console.log(inspect(Schema))
console.log()
console.log(typeCheck.Code()) outputs: {
type: 'RegExp',
source: '^[a-z]$',
flags: 'i',
[Symbol(TypeBox.Kind)]: 'RegExp'
}
const local_0 = /^[a-z]$/i;
return function check(value) {
return (
(typeof value === 'string') &&
local_0.test(value)
)
} I would expect the opposite of all of these: console.log(typeCheck.Check('abCD')) // true
console.log(Value.Check(Schema, 'abCD')) // true
console.log(typeCheck.Check(/^[a-z]+$/i)) // false
console.log(Value.Check(Schema, /^[a-z]+$/i)) // false |
@FDiskas @ehaynes99 Hi, Apologies for delay in reply (have been recovering from a nasty flu :( )
So unfortunately, I can't really introduce this suggestion as JavaScript ECMA262 regular expressions are far more capable than what is possible to encode in JsonSchema string patterns. Some relevant documentation below with appropriate excerpt. https://json-schema.org/understanding-json-schema/reference/regular_expressions
So, because of the limitations here, the Type.RegExp was introduced (as a non-spec JavaScript type) that supports the full ECMA262 syntax + Unicode + the
You're right the RegExp type is a bit unusual. I'd drafted a little bit of information on the 0.32.0 change log about it here which makes note of the inferred type disparity when compared to the other non-standard types. Conceptually, the RegExp type is thought of as a extremely wide TemplateLiteral type whose expressions are non-visible to the type system. In this respect, the only possible type to infer would be Just on this, I am currently considering including a new Type.Refine() to the library. This has been implemented on the const T = Type.Refine(Type.String()).Check(value => /^[a-z]*$/i.test(value)).Done()
console.log(Value.Check(T, 'abcd')) // true This implementation is pending review and feedback, however it's introduction may enable TypeBox to potentially drop ALL extended JavaScript types in favor of refinements, as well as permit additional user defined schematics via a more ergonomic interface (better than TypeRegistry / FormatRegistry) // user defined byte schema + check logic
const UnsafeByte = Type.Unsafe<number>({ type: 'byte' })
const Byte = Type.Refine(UnsafeByte)
.Check(value => typeof value === 'number')
.Check(value => !isNaN(value))
.Check(value => value >= 0)
.Check(value => value < 256)
.Done()
console.log(Value.Check(Byte, 0)) // false
console.log(Value.Check(Byte, 255)) // false
console.log(Value.Check(Byte, -1)) // true
console.log(Value.Check(Byte, NaN)) // true Open to thoughts and feedback |
Had too much to say about that, so created a new issue. |
@FDiskas @ehaynes99 Heya Might close up this issue and track things over at #821. As noted above, the Cheers! |
As a developer, I would like to use the real type in
pattern
-RegExp
For me, it is the same as
instead of
Migrate the
pattern
value toRegExp
typeConfusions:
In case it is a string - the developer should add additional escape characters and also can not pass regexp flags
But if we change this to RegeXp instead of string - IDE tools and plugins will act accordingly because they will recognize this as regexp
The text was updated successfully, but these errors were encountered: