Skip to content
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

added trimInner transform option #218

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ A standalone string cannot be modified, i.e. `data = 'a'; ajv.validate(schema, d
- `toLowerCase`: convert to lower case
- `toUpperCase`: convert to upper case
- `toEnumCase`: change string case to be equal to one of `enum` values in the schema
- `trimInner`: remove all white spaces only inside/within string

Transformations are applied in the order they are listed.

Expand Down
41 changes: 41 additions & 0 deletions spec/transform.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,47 @@ describe('keyword "transform"', () => {
})

ajvs.forEach((ajv, i) => {
it(`should transform trimInner #${i}`, () => {
let schema, data

data = [" trimInner object to test "]
schema = {type: "array", items: {type: "string", transform: ["trimInner"]}}
ajv.validate(schema, data).should.equal(true)
data.should.deep.equal([" trimInnerobjecttotest "])

data = [
`
trimInner
multiple spaces
`,
]
schema = {type: "array", items: {type: "string", transform: ["trimInner"]}}
ajv.validate(schema, data).should.equal(true)
data.should.deep.equal([
`
trimInnermultiplespaces
`,
])

data = ["trimInner \twith\t\t\ttabs\t\t\tinside"]
schema = {type: "array", items: {type: "string", transform: ["trimInner"]}}
ajv.validate(schema, data).should.equal(true)
data.should.deep.equal(["trimInnerwithtabsinside"])

data = [" trimInner object to test with multiple \t\t spaces \t"]
schema = {type: "array", items: {type: "string", transform: ["trimInner"]}}
ajv.validate(schema, data).should.equal(true)
data.should.deep.equal([" trimInnerobjecttotestwithmultiplespaces \t"])

data = [
`A tab\t\tanother multiple tabs\t\t\t\tnew line
multiple new lines
Multiple spaces end`,
]
schema = {type: "array", items: {type: "string", transform: ["trimInner"]}}
ajv.validate(schema, data).should.equal(true)
data.should.deep.equal(["AtabanothermultipletabsnewlinemultiplenewlinesMultiplespacesend"])

it(`shouldn't mutate the transform array of the schema while compiling it #${i}`, () => {
const data = {p: " trimObject "}
const schema = {type: "object", properties: {p: {type: "string", transform: ["trimLeft"]}}}
Expand Down
5 changes: 5 additions & 0 deletions src/definitions/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ type TransformName =
| "toLowerCase"
| "toUpperCase"
| "toEnumCase"
| "trimInner"

interface TransformConfig {
hash: Record<string, string | undefined>
Expand All @@ -26,6 +27,10 @@ const transform: {[key in TransformName]: Transform} = {
toLowerCase: (s) => s.toLowerCase(),
toUpperCase: (s) => s.toUpperCase(),
toEnumCase: (s, cfg) => cfg?.hash[configKey(s)] || s,
trimInner: (s) =>
(/^[\s\t\n]+/.exec(s)?.shift() || "") +
s.trim().replace(/[\s\t\n]+/g, "") +
(/[\s\t\n]+$/.exec(s)?.shift() || ""),
}

const getDef: (() => CodeKeywordDefinition) & {
Expand Down