Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 31 additions & 1 deletion src/definitions/_types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,36 @@
import type {KeywordDefinition} from "ajv"
import type {Transformation} from "./transform"

export interface DefinitionOptions {
export interface KeywordOptions {
/**
* Modifications to the "transform" keyword
*/
transform?: {
/**
* Override existing transformations or add additional (new)
* @example // overrides existing "trim" transformation, if "trim" didn't exist, it would have been added
* import { trim } from "@example-org/pkgName/example-trim"
*
* transform: {
* trim: {
* transformation: trim,
* modulePath: "@example-org/pkgName/example-trim",
* }
* }
*
* // module "example.trim.ts" ("@example-org/pkgName/example-trim") exports a function "trim"
* export const trim = (value: string) => value.trim()
* @end
*/
[key: string]: {
transformation: Transformation
modulePath: string
}
}
}
export type KeywordsWithCustomization = keyof KeywordOptions

export interface DefinitionOptions extends KeywordOptions {
defaultMeta?: string | boolean
}

Expand Down
44 changes: 30 additions & 14 deletions src/definitions/transform.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import type {CodeKeywordDefinition, AnySchemaObject, KeywordCxt, Code, Name} from "ajv"
import type {DefinitionOptions} from "./_types"
import {_, stringify, getProperty} from "ajv/dist/compile/codegen"

type TransformName =
type TransformationName =
| "trimStart"
| "trimEnd"
| "trimLeft"
Expand All @@ -15,9 +16,9 @@ interface TransformConfig {
hash: Record<string, string | undefined>
}

type Transform = (s: string, cfg?: TransformConfig) => string
export type Transformation = (s: string, cfg?: TransformConfig) => string

const transform: {[key in TransformName]: Transform} = {
const builtInTransformations: {[key in TransformationName]: Transformation} = {
trimStart: (s) => s.trimStart(),
trimEnd: (s) => s.trimEnd(),
trimLeft: (s) => s.trimStart(),
Expand All @@ -28,11 +29,13 @@ const transform: {[key in TransformName]: Transform} = {
toEnumCase: (s, cfg) => cfg?.hash[configKey(s)] || s,
}

const getDef: (() => CodeKeywordDefinition) & {
transform: typeof transform
} = Object.assign(_getDef, {transform})
function getDef(opts?: DefinitionOptions): CodeKeywordDefinition {
const customTransformations = opts?.transform || {}
const availableTransformations = [
...Object.keys(builtInTransformations),
...Object.keys(customTransformations),
]

function _getDef(): CodeKeywordDefinition {
return {
keyword: "transform",
schemaType: "array",
Expand All @@ -54,19 +57,32 @@ function _getDef(): CodeKeywordDefinition {

function transformExpr(ts: string[]): Code {
if (!ts.length) return data
const t = ts.pop() as string
if (!(t in transform)) throw new Error(`transform: unknown transformation ${t}`)
const func = gen.scopeValue("func", {
ref: transform[t as TransformName],
code: _`require("ajv-keywords/dist/definitions/transform").transform${getProperty(t)}`,
})
const t = ts.pop() as TransformationName | string
if (!availableTransformations.includes(t)) {
throw new Error(`transform: unknown transformation ${t}`)
}

const func = gen.scopeValue(
"func",
customTransformations[t] // eslint-disable-line @typescript-eslint/no-unnecessary-condition
? {
ref: customTransformations[t].transformation,
code: _`require("${customTransformations[t].modulePath}")${getProperty(t)}`,
}
: {
ref: builtInTransformations[t as TransformationName],
code: _`require("ajv-keywords/dist/definitions/transform").transform${getProperty(
t
)}`,
}
)
const arg = transformExpr(ts)
return cfg && t === "toEnumCase" ? _`${func}(${arg}, ${cfg})` : _`${func}(${arg})`
}
},
metaSchema: {
type: "array",
items: {type: "string", enum: Object.keys(transform)},
items: {type: "string", enum: availableTransformations},
},
}
}
Expand Down
34 changes: 26 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,48 @@
/* eslint-disable valid-jsdoc */ // this rule is deprecated and insists on adding type annotations that already exist in TypeScript
import type Ajv from "ajv"
import type {Plugin} from "ajv"
import plugins from "./keywords"
import type {DefinitionOptions} from "./definitions/_types"
import keywords from "./keywords"

export {AjvKeywordsError} from "./definitions"

const ajvKeywords: Plugin<string | string[]> = (ajv: Ajv, keyword?: string | string[]): Ajv => {
if (Array.isArray(keyword)) {
for (const k of keyword) get(k)(ajv)
/**
* @param ajv instance
* @param specificKeywords only these keywords to-be-added
* @param options modifications passed to keywords
* @returns ajv instance
*/
const ajvKeywords: Plugin<string | string[]> = (
ajv: Ajv,
specificKeywords?: string | string[],
options: DefinitionOptions = {}
): Ajv => {
if (Array.isArray(specificKeywords)) {
for (const k of specificKeywords) addKeyword(ajv, k, options)
return ajv
}
if (keyword) {
get(keyword)(ajv)
if (specificKeywords) {
addKeyword(ajv, specificKeywords, options)
return ajv
}
for (keyword in plugins) get(keyword)(ajv)

for (const keyword in keywords) addKeyword(ajv, keyword, options)
return ajv
}

ajvKeywords.get = get

function get(keyword: string): Plugin<any> {
const defFunc = plugins[keyword]
const defFunc = keywords[keyword]
if (!defFunc) throw new Error("Unknown keyword " + keyword)
return defFunc
}

function addKeyword(ajv: Ajv, keyword: string, options: DefinitionOptions): void {
const defFunc = get(keyword)
defFunc(ajv, options)
}

export default ajvKeywords
module.exports = ajvKeywords

Expand Down
3 changes: 2 additions & 1 deletion src/keywords/transform.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import type {Plugin} from "ajv"
import type {KeywordOptions} from "../definitions/_types"
import getDef from "../definitions/transform"

const transform: Plugin<undefined> = (ajv) => ajv.addKeyword(getDef())
const transform: Plugin<KeywordOptions["transform"]> = (ajv, opts) => ajv.addKeyword(getDef(opts))

export default transform
module.exports = transform