_util.ts 666 B

12345678910111213141516171819202122
  1. import type {DefinitionOptions} from "./_types"
  2. import type {SchemaObject, KeywordCxt, Name} from "ajv"
  3. import {_} from "ajv/dist/compile/codegen"
  4. const META_SCHEMA_ID = "http://json-schema.org/schema"
  5. export function metaSchemaRef({defaultMeta}: DefinitionOptions = {}): SchemaObject {
  6. return defaultMeta === false ? {} : {$ref: defaultMeta || META_SCHEMA_ID}
  7. }
  8. export function usePattern(
  9. {gen, it: {opts}}: KeywordCxt,
  10. pattern: string,
  11. flags = opts.unicodeRegExp ? "u" : ""
  12. ): Name {
  13. const rx = new RegExp(pattern, flags)
  14. return gen.scopeValue("pattern", {
  15. key: rx.toString(),
  16. ref: rx,
  17. code: _`new RegExp(${pattern}, ${flags})`,
  18. })
  19. }