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

Validation should not modify an object #5

Open
nrevutsky opened this issue Dec 22, 2024 · 1 comment
Open

Validation should not modify an object #5

nrevutsky opened this issue Dec 22, 2024 · 1 comment

Comments

@nrevutsky
Copy link

In case a schema has no validation item (anything is allowed), an object is modified after the validation

const schema ={
type: "object",
properties: {
name: { type: "string"}
value: {}
} 
obj = {name: "test", value: "v"}

after validation obj is
{name: "test", value: ["v"]}

@nrevutsky
Copy link
Author

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

{
  isModified: true,
  isValid: true,
  original: {
    body: { filter: [ { name: 'aaa', condition: 'eq', value: 'Test 1' } ] }
  },
  after: {
    body: {
      filter: [ { name: 'aaa', condition: 'eq', value: [ 'Test 1' ] } ]
    }
  },
  error: null
}
{
  isModified: true,
  isValid: true,
  original: {
    body: { filter: [ { name: 'aaa', condition: 'eq', value: 100 } ] }
  },
  after: {
    body: {
      filter: [ { name: 'aaa', condition: 'eq', value: [ 100 ] } ]
    }
  },
  error: null
}
{
  isModified: true,
  isValid: true,
  original: {
    body: { filter: [ { name: 'aaa', condition: 'eq', value: true } ] }
  },
  after: {
    body: {
      filter: [ { name: 'aaa', condition: 'eq', value: [ true ] } ]
    }
  },
  error: null
}
{
  isModified: false,
  isValid: true,
  original: {
    body: {
      filter: [ { name: 'aaa', condition: 'in', value: [ 'test1', 'test2' ] } ]
    }
  },
  after: {
    body: {
      filter: [ { name: 'aaa', condition: 'in', value: [ 'test1', 'test2' ] } ]
    }
  },
  error: null
}
{
  isModified: false,
  isValid: true,
  original: {
    body: {
      filter: [ { name: 'aaa', condition: 'in', value: [ 'test1' ] } ]
    }
  },
  after: {
    body: {
      filter: [ { name: 'aaa', condition: 'in', value: [ 'test1' ] } ]
    }
  },
  error: null
}
{
  isModified: false,
  isValid: true,
  original: {
    body: {
      filter: [ { name: 'aaa', condition: 'in', value: [ true ] } ]
    }
  },
  after: {
    body: {
      filter: [ { name: 'aaa', condition: 'in', value: [ true ] } ]
    }
  },
  error: null
}
{
  isModified: true,
  isValid: true,
  original: {
    body: { filter: [ { name: 'aaa', condition: 'ne', value: null } ] }
  },
  after: {
    body: {
      filter: [ { name: 'aaa', condition: 'ne', value: [ null ] } ]
    }
  },
  error: null
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant