-
Notifications
You must be signed in to change notification settings - Fork 1
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
Validation should not modify an object #5
Comments
I created a test project https://codesandbox.io/p/devbox/j2t48z to show the cases when the parameter is modified inside the function. In case schema is used with middy validator request.event is modified before being processed by handler. schema {
"type": "object",
"additionalProperties": false,
"required": [
"body"
],
"properties": {
"body": {
"type": "object",
"additionalProperties": false,
"required": [
"filter"
],
"properties": {
"filter":{
"type": "array",
"minItems": 1,
"uniqueItems": true,
"items": {
"oneOf": [
{
"type": "object",
"additionalProperties": false,
"required": [
"name", "condition", "value"
],
"properties": {
"name": {
"type": "string"
},
"condition": {
"type": "string",
"enum": ["eq", "ne", "gt", "lt", "gte", "lte"]
},
"value": {}
}
},
{
"type": "object",
"additionalProperties": false,
"required": [
"name", "condition", "value"
],
"properties": {
"name": {
"type": "string"
},
"condition": {
"type": "string",
"enum": ["in", "nin"]
},
"value": {
"type": "array",
"minItems": 1,
"uniqueItems": true,
"items": {}
}
}
}
]
}
}
}
}
}
} main import eventSchema from './schemas/event.js'
import util from 'node:util';
util.inspect.defaultOptions.depth = null;
const testData = [
{
body: {
filter: [{
name: "aaa",
condition: "eq",
value: "Test 1"
}]
}
},
{
body: {
filter: [{
name: "aaa",
condition: "eq",
value: 100
}]
}
},
{
body: {
filter: [{
name: "aaa",
condition: "eq",
value: true
}]
}
},
{
body: {
filter: [{
name: "aaa",
condition: "in",
value: ["test1", "test2"]
}]
}
},
{
body: {
filter: [{
name: "aaa",
condition: "in",
value: ["test1"]
}]
}
},
{
body: {
filter: [{
name: "aaa",
condition: "in",
value: [true]
}]
}
},
{
body: {
filter: [{
name: "aaa",
condition: "ne",
value: null
}]
}
},
]
const test = (data) => {
const original = JSON.stringify(data, null, 2);
const isValid = eventSchema(data);
const after = JSON.stringify(data, null, 2);
console.log({ isModified: original !== after, isValid, original: JSON.parse(original), after: JSON.parse(after), error: eventSchema.errors });
}
for (const data of testData) {
test(data);
} result
|
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
In case a schema has no validation item (anything is allowed), an object is modified after the validation
after validation obj is
{name: "test", value: ["v"]}
The text was updated successfully, but these errors were encountered: