Usage:
pnpm saleor-schema-compiler compile ./src/schemas
With external definitions.json file:
pnpm saleor-schema-compiler compile ./src/schemas --definitions ./src/schemas/definitions.json
It's a CLI tool that takes a directory and recursively transforms all JSON schemas it found into two files: .d.mts
and .mjs
. The first one contains types and the second one is a JS standalone validator:
![Screenshot 2023-06-22 at 14 58 14](https://private-user-images.githubusercontent.com/1338731/247937305-f5d4504b-fff0-49a3-9e94-c1e36bd37b95.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjQzNjM4ODIsIm5iZiI6MTcyNDM2MzU4MiwicGF0aCI6Ii8xMzM4NzMxLzI0NzkzNzMwNS1mNWQ0NTA0Yi1mZmYwLTQ5YTMtOWU5NC1jMWUzNmJkMzdiOTUucG5nP1gtQW16LUFsZ29yaXRobT1BV1M0LUhNQUMtU0hBMjU2JlgtQW16LUNyZWRlbnRpYWw9QUtJQVZDT0RZTFNBNTNQUUs0WkElMkYyMDI0MDgyMiUyRnVzLWVhc3QtMSUyRnMzJTJGYXdzNF9yZXF1ZXN0JlgtQW16LURhdGU9MjAyNDA4MjJUMjE1MzAyWiZYLUFtei1FeHBpcmVzPTMwMCZYLUFtei1TaWduYXR1cmU9N2VlZmM3MjdmMmUzYmFjNTdlYzZmYjZkMmVlMmFjZTNiY2M4OTdmZjA4NzljZTUzMzY2NDM0ODAzZmE3ZThiMSZYLUFtei1TaWduZWRIZWFkZXJzPWhvc3QmYWN0b3JfaWQ9MCZrZXlfaWQ9MCZyZXBvX2lkPTAifQ.Cz3g_nsdAgEQj9OvzkLn5BzbbE0oHKRloZOLPBamTuM)
{
"$schema": "http://json-schema.org/draft-07/schema",
"type": "object",
"properties": {
"pspReference": { "type": "string" },
"result": {
"$ref": "definitions.json#/definitions/TransactionCancelationRequestedAllowedResult"
},
"amount": { "$ref": "definitions.json#/definitions/PositiveDecimal" },
"time": { "$ref": "definitions.json#/definitions/DateTime" },
"externalUrl": { "type": "string" },
"message": { "type": "string" }
},
"additionalProperties": false,
"required": ["pspReference"]
}
{
"$schema": "http://json-schema.org/draft-07/schema",
"$id": "definitions.json",
"definitions": {
"PositiveDecimal": { "type": "number", "minimum": 0 },
"DateTime": { "type": "string", "format": "date-time" },
"TransactionCancelationRequestedAllowedResult": {
"anyOf": [
{ "type": "string", "const": "CANCEL_SUCCESS" },
{ "type": "string", "const": "CANCEL_FAILURE" }
]
}
}
}
pnpm saleor-schema-compiler compile ./src/schemas --definitions ./src/schemas/definitions.json
import type { ValidateFunction } from "ajv";
import type { JSONObject } from '../../types';
/* eslint-disable */
/**
* This file was automatically generated by json-schema-to-typescript.
* DO NOT MODIFY IT BY HAND. Instead, modify the source JSONSchema file,
* and run json-schema-to-typescript to regenerate this file.
*/
export interface TransactionCancelationRequestedResponse {
pspReference: string;
result?: "CANCEL_SUCCESS" | "CANCEL_FAILURE";
amount?: number;
time?: string;
externalUrl?: string;
message?: string;
}
declare const ValidateTransactionCancelationRequestedResponse: ValidateFunction<TransactionCancelationRequestedResponse>;
export default ValidateTransactionCancelationRequestedResponse;
Contains a standalone validator function. Exports default
function that a ValidateFunction
from Ajv library.
Generated code can be used as follows:
// As a type:
import { type TransactionCancelationRequestedResponse } from "./src/schemas/TransactionCancelationRequested/TransactionCancelationRequestedResponse.mjs";
// As a validator:
import ValidateTransactionCancelationRequestedResponse from "./src/schemas/TransactionCancelationRequested/TransactionCancelationRequestedResponse.mjs";
const data: unknown = { /* … */ };
const isDataValid = ValidateTransactionCancelationRequestedResponse(data); // true | false
if (isDataValid) {
data // data has proper type here! TransactionCancelationRequestedResponse
}