Cloudron makes it easy to run web apps like WordPress, Nextcloud, GitLab on your server. Find out more or install now.


Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Bookmarks
  • Search
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Brand Logo

Cloudron Forum

Apps | Demo | Docs | Install
  1. Cloudron Forum
  2. N8N
  3. extending ajv with ajv-formats

extending ajv with ajv-formats

Scheduled Pinned Locked Moved Solved N8N
10 Posts 4 Posters 2.1k Views 4 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    dominikjannis
    wrote on last edited by dominikjannis
    #1

    Hi everyone,
    i need to do some json schema validation with ajv and would need the "ajv-formats" extension (https://ajv.js.org/packages/ajv-formats.html) for checking for email and date data types. since version 7 those are separated from the main ajv module. I think this library would also be very useful for "n8n as API" use cases, would you consider adding it? Or do i have to do and maintain this myself - if possible at all?

    Best,
    Dominik

    1 Reply Last reply
    1
    • nebulonN Offline
      nebulonN Offline
      nebulon
      Staff
      wrote on last edited by
      #2

      Is this some module for n8n or are you running custom code within the app?

      D 1 Reply Last reply
      0
      • nebulonN nebulon

        Is this some module for n8n or are you running custom code within the app?

        D Offline
        D Offline
        dominikjannis
        wrote on last edited by
        #3

        @nebulon i am running the validation logic in a "Code" node within the n8n app.

        nebulonN 1 Reply Last reply
        0
        • nebulonN Offline
          nebulonN Offline
          nebulon
          Staff
          wrote on last edited by
          #4

          Similar to the community modules, I am not sure if a Cloudron package can support custom dependencies currently.

          1 Reply Last reply
          0
          • robiR Offline
            robiR Offline
            robi
            wrote on last edited by
            #5

            @dominikjannis If you can install the extension anywhere else, then copy the relevant files where N8N would expect them to be, that is a way of inserting the extension.

            The next question is if the place where the extension folder is expected is on the read-only volume (/app/code) or the /app/data writable one.

            If needed, check if that directory is settable by environment variables to make it work.

            Conscious tech

            1 Reply Last reply
            0
            • girishG Offline
              girishG Offline
              girish
              Staff
              wrote on last edited by
              #6

              TIL about ajv , very nice. We use https://github.com/geraintluff/tv4 for this but that has not been in development for a while. We should probably move to ajv as well.

              1 Reply Last reply
              0
              • D dominikjannis

                @nebulon i am running the validation logic in a "Code" node within the n8n app.

                nebulonN Offline
                nebulonN Offline
                nebulon
                Staff
                wrote on last edited by nebulon
                #7

                @dominikjannis so do you need then the ajv or the ajv-formats node module to be installed or both? I am not 100% sure how they are to be used within n8n, but since they seems quite popular, we can add them or just one of them to the app package itself.

                Also it looks like in order to use node_modules within the code node one has to explicitly enable them via NODE_FUNCTION_ALLOW_EXTERNAL https://docs.n8n.io/hosting/configuration/#use-built-in-and-external-modules-in-the-code-node ?

                Also do you have a code snippet to be used within the code node to verify those modules work?

                D 1 Reply Last reply
                0
                • nebulonN nebulon marked this topic as a question on
                • nebulonN nebulon

                  @dominikjannis so do you need then the ajv or the ajv-formats node module to be installed or both? I am not 100% sure how they are to be used within n8n, but since they seems quite popular, we can add them or just one of them to the app package itself.

                  Also it looks like in order to use node_modules within the code node one has to explicitly enable them via NODE_FUNCTION_ALLOW_EXTERNAL https://docs.n8n.io/hosting/configuration/#use-built-in-and-external-modules-in-the-code-node ?

                  Also do you have a code snippet to be used within the code node to verify those modules work?

                  D Offline
                  D Offline
                  dominikjannis
                  wrote on last edited by
                  #8

                  @nebulon the ajv module itself is present, since standalone ajv works when enabled explicitly via the .env - just as you pointed it out.
                  we use it to check incoming data from other internal services for potentially malformed data structures before further processing. Checking for (custom) string formats would be handy 🙂

                  here a snippet, based off of the example from ajv for the code node:

                  const Ajv = require("ajv");
                  const addFormats = require("ajv-formats")
                  
                  const ajv = new Ajv();
                  addFormats(ajv);
                  
                  const schema = {
                    type: "object",
                    properties: {
                      foo: { type: "integer" },
                      bar: { type: "string", format: "email" },
                    },
                    required: ["foo"],
                    additionalProperties: false,
                  };
                  
                  const data = { foo: 1, bar: "no.email.com" };
                  
                  const valid = ajv.validate(schema, data);
                  if (!valid) {
                    console.log(ajv.errors);
                    return ajv.errors;
                  }
                  return data;
                  

                  this should result in this error, showing everything is working:

                  {
                      instancePath: '/bar',
                      schemaPath: '#/properties/bar/format',
                      keyword: 'format',
                      params: { format: 'email' },
                      message: 'must match format "email"'
                    }
                  
                  1 Reply Last reply
                  1
                  • nebulonN Offline
                    nebulonN Offline
                    nebulon
                    Staff
                    wrote on last edited by
                    #9

                    Thanks, the latest package released supports this now. The modules still have to be allowed via the mentioned env variable.

                    D 1 Reply Last reply
                    3
                    • girishG girish has marked this topic as solved on
                    • nebulonN nebulon

                      Thanks, the latest package released supports this now. The modules still have to be allowed via the mentioned env variable.

                      D Offline
                      D Offline
                      dominikjannis
                      wrote on last edited by
                      #10

                      @nebulon perfect, thanks a lot for this!

                      1 Reply Last reply
                      1
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Bookmarks
                      • Search