| 4394 | * @since 3.10.0 |
| 4395 | */ |
| 4396 | export const pattern = <S extends Schema.Any>( |
| 4397 | regex: RegExp, |
| 4398 | annotations?: Annotations.Filter<Schema.Type<S>> |
| 4399 | ) => |
| 4400 | <A extends string>(self: S & Schema<A, Schema.Encoded<S>, Schema.Context<S>>): filter<S> => { |
| 4401 | const source = regex.source |
| 4402 | return self.pipe( |
| 4403 | filter( |
| 4404 | (a) => { |
| 4405 | // The following line ensures that `lastIndex` is reset to `0` in case the user has specified the `g` flag |
| 4406 | regex.lastIndex = 0 |
| 4407 | return regex.test(a) |
| 4408 | }, |
| 4409 | { |
| 4410 | schemaId: PatternSchemaId, |
| 4411 | [PatternSchemaId]: { regex }, |
| 4412 | // title: `pattern(/${source}/)`, // avoiding this because it can be very long |
| 4413 | description: `a string matching the pattern ${source}`, |
| 4414 | jsonSchema: { pattern: source }, |
| 4415 | ...annotations |
| 4416 | } |
| 4417 | ) |
| 4418 | ) |
| 4419 | } |
| 4420 | |
| 4421 | /** |
| 4422 | * @category schema id |