Skip to content

Commit

Permalink
Merge pull request #173 from keplr-team/do-not-mutate
Browse files Browse the repository at this point in the history
Don't mutate a schema with a transform keyword while compiling it.
  • Loading branch information
epoberezkin authored Nov 21, 2021
2 parents d53127e + 07a2c98 commit bc73a2d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
11 changes: 11 additions & 0 deletions spec/transform.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,4 +129,15 @@ describe('keyword "transform"', () => {
data.should.deep.equal(["ab"])
})
})

ajvs.forEach((ajv, i) => {
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"]}}}
ajv.validate(schema, data).should.equal(true)
data.should.deep.equal({p: "trimObject "})

schema.properties.p.transform.should.deep.equal(["trimLeft"])
})
})
})
2 changes: 1 addition & 1 deletion src/definitions/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ function _getDef(): CodeKeywordDefinition {
cfg = gen.scopeValue("obj", {ref: config, code: stringify(config)})
}
gen.if(_`typeof ${data} == "string" && ${parentData} !== undefined`, () => {
gen.assign(data, transformExpr(tNames))
gen.assign(data, transformExpr(tNames.slice()))
gen.assign(_`${parentData}[${parentDataProperty}]`, data)
})

Expand Down

0 comments on commit bc73a2d

Please sign in to comment.