Repository: fastify/fast-json-stringify (JavaScript)

Issue:
Ajv validation inside allOf
### Prerequisites

- [X] I have written a descriptive issue title
- [X] I have searched existing issues to ensure the issue has not already been raised


### Issue

There is a fastify issue https://github.com/fastify/fastify/issues/4092
I reduced the test example.

```js
test('anyOf inside allOf', (t) => {
  t.plan(1)

  const schema = {
    anyOf: [
      {
        type: 'object',
        allOf: [
          {
            properties: {
              a: {
                anyOf: [
                  { const: 'A1' },
                  { const: 'A2' }
                ]
              }
            }
          },
          {
            properties: {
              b: { const: 'B' }
            }
          }
        ]
      }
    ]
  }

  const object = { a: 'A1', b: 'B' }
  const stringify = build(schema)
  const output = stringify(object)

  t.equal(output, JSON.stringify(object))
})
```

I'm working on that.


Discussion:
Will it be fixed by https://github.com/fastify/fast-json-stringify/pull/480
@climba03003 No, it's a different issue.
This problem happens when we add a schema that contains `allOf` property to the Ajv, and then we merge `allOf` and use json pointer that doesn't contain `allOf`.
@climba03003 It turns out that #480 fixed this, despite the fact that they are different problems. #480 has a small bug. I fixed it here #482.
FWIW, I think I encountered this issue too while using a schema similar to this (observed in 5.0.4, unrelated to #479):

```json
{
  "type": "object",
  "required": [
    "list"
  ],
  "properties": {
    "list": {
      "type": "array",
      "items": {
        "type": "object",
        "allOf": [
          {
            "type": "object",
            "required": [
              "value"
            ],
            "properties": {
              "value": {
                "anyOf": [
                  {
                    "type": "string",
                    "const": "one"
                  },
                  {
                    "type": "string",
                    "const": "two"
                  }
                ]
              }
            }
          }
        ]
      }
    }
  }
}
```

```txt
Error: no schema with key or ref "merged_d63d76d2-7f36-4b28-8997-579e7e46cfdb#/properties/list/items/properties/value/anyOf/0"
    at Ajv.validate (..../node_modules/ajv/lib/core.ts:359:21)
    at anonymous2 (eval at build (..../node_modules/fast-json-stringify/index.js:177:23), <anonymous>:30:20)
    at anonymous1 (eval at build (..../node_modules/fast-json-stringify/index.js:177:23), <anonymous>:64:17)
    at anonymous0 (eval at build (..../node_modules/fast-json-stringify/index.js:177:23), <anonymous>:92:15)
    at main (eval at build (..../node_modules/fast-json-stringify/index.js:177:23), <anonymous>:6:15)
    at Object.<anonymous> (..../src/index.ts:345:27)
    at Module._compile (node:internal/modules/cjs/loader:1105:14)
    at Module.m._compile (..../node_modules/ts-node/src/index.ts:1597:23)
    at Module._extensions..js (node:internal/modules/cjs/loader:1159:10)
    at Object.require.extensions.<computed> [as .ts] (..../node_modules/ts-node/src/index.ts:1600:12)
```
> FWIW, I think I encountered this issue too while using a schema similar to this (observed in 5.0.4, unrelated to https://github.com/fastify/fast-json-stringify/issues/479):

Does it work with this fix?
> Does it work with this fix?

Yep! #482 fixes it for me too.




Patch:
diff --git a/index.js b/index.js
index 11a67a11..083f0fc2 100644
--- a/index.js
+++ b/index.js
@@ -493,6 +493,7 @@ function mergeAllOfSchema (location, schema, mergedSchema) {
   mergedSchema.$id = `merged_${randomUUID()}`
   ajvInstance.addSchema(mergedSchema)
   location.schemaId = mergedSchema.$id
+  location.jsonPointer = '#'
 }
 
 function buildInnerObject (location) {


Test patch:
diff --git a/test/ref.test.js b/test/ref.test.js
index f9502c47..fce9d20e 100644
--- a/test/ref.test.js
+++ b/test/ref.test.js
@@ -1891,3 +1891,38 @@ test('input schema is not mutated', (t) => {
   t.equal(output, '{"obj":"test"}')
   t.same(schema, clonedSchema)
 })
+
+test('anyOf inside allOf', (t) => {
+  t.plan(1)
+
+  const schema = {
+    anyOf: [
+      {
+        type: 'object',
+        allOf: [
+          {
+            properties: {
+              a: {
+                anyOf: [
+                  { const: 'A1' },
+                  { const: 'A2' }
+                ]
+              }
+            }
+          },
+          {
+            properties: {
+              b: { const: 'B' }
+            }
+          }
+        ]
+      }
+    ]
+  }
+
+  const object = { a: 'A1', b: 'B' }
+  const stringify = build(schema)
+  const output = stringify(object)
+
+  t.equal(output, JSON.stringify(object))
+})