-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
pulling in AjvUtilities from Docs.json.ts
- Loading branch information
1 parent
d540ae3
commit 4d16604
Showing
8 changed files
with
329 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
{ | ||
"name": "nodejs template", | ||
"name": "Ajv utilities", | ||
"build": { | ||
"dockerfile": "Dockerfile" | ||
}, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
[![Coverage Status](https://coveralls.io/repos/github/satisfactory-dev/ajv-utilities/badge.svg?branch=main)](https://coveralls.io/github/satisfactory-dev/ajv-utilities?branch=main) | ||
[![Workflow Status](https://github.com/satisfactory-dev/ajv-utilities/actions/workflows/node.js.yml/badge.svg?branch=main)](https://github.com/satisfactory-dev/ajv-utilities/actions/workflows/node.js.yml?query=branch%3Amain) | ||
|
||
# Installation | ||
|
||
`npm install --save-dev @satisfactory-dev/ajv-utlities` | ||
|
||
# Usage | ||
|
||
## `compile` | ||
|
||
```ts | ||
import Ajv from 'ajv/dist/2020'; | ||
import {compile, FailedToCompileSchema} from '@satisfactory-dev/ajv-utlities'; | ||
|
||
const schema = {type: 'string'}; | ||
|
||
try { | ||
const check = compile(new Ajv(), schema); | ||
} catch (err) { | ||
if (err instanceof FailedToCompileSchema) { | ||
console.error('failed to compile', err); | ||
} else { | ||
console.error('unknown error', err); | ||
} | ||
} | ||
``` | ||
|
||
## `esmify` | ||
|
||
```ts | ||
import Ajv from 'ajv/dist/2020'; | ||
import standalone from 'ajv/dist/standalone'; | ||
import {compile, esmify} from '@satisfactory-dev/ajv-utlities'; | ||
|
||
const ajv = new Ajv(); | ||
|
||
await writeFile( | ||
'./standalone-validator.mjs', | ||
esmify(standalone(ajv, compile(ajv, schema))) | ||
); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './src/AjvUtilities'; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import Ajv, { | ||
SchemaObject, | ||
ValidateFunction, | ||
} from 'ajv/dist/2020'; | ||
|
||
const cache = new WeakMap<SchemaObject, ValidateFunction>(); | ||
|
||
const duplicate_compile_check: {[key: string]: ValidateFunction} = {}; | ||
|
||
export class FailedToCompileSchema extends Error | ||
{ | ||
readonly err:unknown; | ||
readonly schema:SchemaObject; | ||
|
||
constructor( | ||
schema:SchemaObject, | ||
err:unknown, | ||
message = 'Failed to compile schema' | ||
) { | ||
super(message); | ||
this.schema = schema; | ||
this.err = err; | ||
} | ||
} | ||
|
||
export function compile<T>( | ||
ajv:Ajv, | ||
schema:SchemaObject, | ||
) : ValidateFunction<T> { | ||
try { | ||
performance.mark('ajv compile start'); | ||
|
||
if (cache.has(schema)) { | ||
const result = cache.get(schema) as ValidateFunction<T>; | ||
|
||
performance.measure('ajv compile cached', 'ajv compile start'); | ||
|
||
return result; | ||
} | ||
|
||
const key = JSON.stringify(schema); | ||
if (key in duplicate_compile_check) { | ||
performance.measure('ajv compile duplicate', 'ajv compile start'); | ||
|
||
return duplicate_compile_check[key] as ValidateFunction<T>; | ||
} | ||
|
||
const result = ajv.compile<T>(schema); | ||
cache.set(schema, result); | ||
duplicate_compile_check[key] = result; | ||
|
||
performance.measure('ajv compile', 'ajv compile start'); | ||
|
||
return result; | ||
} catch (err) { | ||
throw new FailedToCompileSchema(schema, err); | ||
} | ||
} | ||
|
||
export function esmify(code:string): string | ||
{ | ||
const replacements:{[key: string]: [string, string]} = { | ||
'require("ajv/dist/runtime/equal").default': [ | ||
'fast_deep_equal', | ||
`import fast_deep_equal from 'fast-deep-equal';`, | ||
], | ||
'require("ajv/dist/runtime/ucs2length").default': [ | ||
'ucs2length.default', | ||
`import ucs2length from 'ajv/dist/runtime/ucs2length';`, | ||
], | ||
}; | ||
|
||
const import_these:string[] = []; | ||
|
||
for (const entry of Object.entries(replacements)) { | ||
const [look_for, [replace_with, add_to_import]] = entry; | ||
|
||
if (code.includes(look_for)) { | ||
code = code.replace(look_for, replace_with); | ||
import_these.push(add_to_import); | ||
} | ||
} | ||
|
||
if (import_these.length) { | ||
code = code.replace(/^"use strict";/, [ | ||
'"use strict";', | ||
...import_these, | ||
].join('')); | ||
} | ||
|
||
return code; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.