MCPcopy Create free account
hub / github.com/Effect-TS/effect / rewriteNot

Function rewriteNot

packages/tools/ai-codegen/src/Generator.ts:178–212  ·  view source on GitHub ↗
(js: JsonSchema.JsonSchema)

Source from the content-addressed store, hash-verified

176
177/** Rewrites supported negations as positive constraints; leaves other forms for the importer to reject. */
178const rewriteNot = (js: JsonSchema.JsonSchema): JsonSchema.JsonSchema => {
179 const not = js.not
180 if (!Predicate.isObject(not)) return js
181 const { not: _, ...rest } = js
182
183 const type = typeComplement(not, rest.type)
184 if (type !== undefined) return { ...rest, type }
185
186 const pattern = negatedClassPattern(not)
187 if (pattern !== undefined) {
188 if (rest.pattern === undefined) return { ...rest, pattern }
189 // The existing pattern already restricts every character to the same class
190 if (rest.pattern === pattern || rest.pattern === pattern.replace(/\*\$$/, "+$")) return rest
191 return { ...rest, allOf: [...(Array.isArray(rest.allOf) ? rest.allOf : []), { pattern }] }
192 }
193
194 // `required` holds for any non-object, so its negation only has a key-absence equivalent on objects
195 if (!isStringArray(not.required) || rest.type !== "object") return js
196 const required = not.required
197 const notKeys = Object.keys(not)
198 if (notKeys.length === 1 && required.length === 2 && rest.oneOf === undefined && Array.isArray(rest.anyOf)) {
199 const keys = rest.anyOf.map(requiredKey)
200 if (keys.length === 2 && required.every((key) => keys.includes(key)) && keys[0] !== keys[1]) {
201 const { anyOf, ...base } = rest
202 return { ...base, oneOf: anyOf }
203 }
204 }
205 if (rest.anyOf !== undefined || rest.oneOf !== undefined) return js
206 if (notKeys.length === 1) return { ...rest, anyOf: required.map((key) => absent([key])) }
207 const alternatives = Array.isArray(not.anyOf) ? not.anyOf.map(requiredKey) : []
208 if (notKeys.length === 2 && alternatives.length > 0 && isStringArray(alternatives)) {
209 return { ...rest, anyOf: [...required.map((key) => absent([key])), absent(alternatives)] }
210 }
211 return js
212}
213
214/** Without `then`, `if`/`else` reduces to `if or else`. */
215const rewriteConditional = (js: JsonSchema.JsonSchema): JsonSchema.JsonSchema => {

Callers 1

prepareFunction · 0.85

Calls 7

typeComplementFunction · 0.85
negatedClassPatternFunction · 0.85
keysMethod · 0.80
isStringArrayFunction · 0.70
absentFunction · 0.70
replaceMethod · 0.45
mapMethod · 0.45

Tested by

no test coverage detected