-
Notifications
You must be signed in to change notification settings - Fork 159
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
accept regex for pattern
option
#615
Comments
@boywithkeyboard Hi, You can use Type.RegExp() for this (which is just an alias type for Type.String()) import { Type } from '@sinclair/typebox'
// [JavaScript] Creates a String type from a Regular Expression
//
const T = Type.RegExp(/example/)
// Which is an alias for
const T = Type.String({ pattern: /example/.source }) Regular Expression ConsiderationsUnfortunately, I can't add regular expression support to Type.String() without running up against the specification (as Json Schema doesn't properly support the full ECMA262 regular expression syntax (with special note to flags (such as ignoreCasing) and Unicode support)). It's actually questionable if the Type.RegExp() type should exist as an alias to Type.String() (given it permits more than is possible in the specification). I have been considering changing Type.RegExp() to be a distinct type unto itself just to support the full ECMA262 syntax. For now though, implementations should try and limit to the following syntax. https://json-schema.org/understanding-json-schema/reference/regular_expressions Hope this helps |
Alright, thanks! |
Hello @sinclairzx81, what is the correct way to use |
@SleepWalker Hi, RegExp was moved to For the most part, you should be able to update the previous PatternUse the following if you need to publish the schematics and/or retain interoperability with Ajv and the Json Schema specification. This is approach is typically recommended and previous implementations using RegEx (pre-0.32.0) should be updated to use approach. const T = Type.String({ pattern: '^Option(A|B|C)$' })
const T = Type.String({ pattern: /^Option(A|B|C)$/.source }) // this also works
const R = Value.Check(T, 'OptionA') // true Template LiteralUse Template Literal if a regular expression can be encoded as a template. TypeBox will encode Template Literals as Json Schema compliant regular expressions and they also provide type inference. const T = Type.TemplateLiteral('Option${A|B|C}')
const T = Type.TemplateLiteral([ // can also be written this way
Type.Literal('Option'),
Type.Union([
Type.Literal('A'),
Type.Literal('B'),
Type.Literal('C'),
])
])
const R = Value.Check(T, 'OptionA') // true RegExpUse the RegExp if you need UTF-16 support and expressions above and beyond those supported by Json Schema. This type is non-standard and only works with TypeBox's TypeCompiler or Value modules. // emoji only expression
const T = Type.RegExp(/<a?:.+?:\d{18}>|\p{Extended_Pictographic}/gu)
const R = Value.Check(T, '♥️♦️♠️♣️') // true FormatIn cases where you need UTF-16 support and compatibility with Ajv, use string formats. Formats do require configuration. The following configures for TypeBox (information on Ajv configuration can be found here) import { FormatRegistry } from '@sinclair/typebox'
FormatRegistry.Set('emoji', value => /<a?:.+?:\d{18}>|\p{Extended_Pictographic}/gu.test(value))
const T = Type.String({ format: 'emoji' })
const R = Value.Check(T, '♥️♦️♠️♣️') // true Hope this helps |
Sorry I was insufficiently attentive reading the last changelog. I've thought there was just a new name for an old thing 😅 Thank you for the detailed explanation ❤️ |
It'd be awesome if TypeBox would accept a regular expression for the pattern option:
The regular expression could internally be converted to a string:
The text was updated successfully, but these errors were encountered: