Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/json-schema/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export interface JsonSchemaString extends JsonSchemaGenericKeywords {
type: 'string';
const?: string;
format?: string;
pattern?: string;
minLength?: number;
maxLength?: number;
}
Expand Down
11 changes: 11 additions & 0 deletions src/schema/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,21 @@ export interface NumberSchema extends TType<number>, WithValidator {
export interface StringSchema extends TType<string>, WithValidator {
kind: 'str';

/**
* String format specification. When set, the string value will be validated
* according to the specified format for maximum performance.
*
* - "ascii" - Only ASCII characters (0-127) are allowed
* - "utf8" - Valid UTF-8 encoded strings are allowed
*/
format?: 'ascii' | 'utf8';

/**
* When set to true, means that the string can contain only ASCII characters.
* This enables a range of optimizations, such as using a faster JSON
* serialization, faster binary serialization.
*
* @deprecated Use `format: 'ascii'` instead.
*/
ascii?: boolean;

Expand Down
259 changes: 130 additions & 129 deletions src/type/__tests__/getJsonSchema.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,103 +41,86 @@ test('can print a type', () => {
.options({unknownFields: true});
// console.log(JSON.stringify(type.toJsonSchema(), null, 2));
expect(type.toJsonSchema()).toMatchInlineSnapshot(`
{
"properties": {
"arrayProperty": {
"items": {
"type": [
"string",
"number",
"boolean",
"null",
"array",
"object",
],
},
"type": "array",
},
"binaryOperation": {
"type": "binary",
{
"properties": {
"arrayProperty": {
"items": {
"type": [
"string",
"number",
"boolean",
"null",
"array",
"object",
],
},
"type": "array",
},
"binaryOperation": {
"type": "binary",
},
"binaryProperty": {
"type": "binary",
},
"booleanProperty": {
"type": "boolean",
},
"enumAsConst": {
"anyOf": [
{
"const": "a",
"type": "string",
},
"binaryProperty": {
"type": "binary",
{
"const": "b",
"type": "string",
},
"booleanProperty": {
"type": "boolean",
{
"const": "c",
"type": "string",
},
"enumAsConst": {
"anyOf": [
{
"const": "a",
"type": "string",
},
{
"const": "b",
"type": "string",
},
{
"const": "c",
"type": "string",
},
],
],
},
"id": {
"type": "string",
},
"map": {
"patternProperties": {
".*": {
"type": "string",
},
},
"type": "object",
},
"numberProperty": {
"exclusiveMinimum": 3.14,
"type": "number",
},
"objectProperty": {
"properties": {
"id": {
"maxLength": 128,
"minLength": 3,
"pattern": "^[\\x00-\\x7F]*$",
"type": "string",
},
"map": {
"patternProperties": {
".*": {
"type": "string",
},
},
"type": "object",
},
"numberProperty": {
"exclusiveMinimum": 3.14,
"type": "number",
},
"objectProperty": {
"properties": {
"id": {
"maxLength": 128,
"minLength": 3,
"type": "string",
},
},
"required": [
"id",
],
"type": "object",
},
"required": [
"id",
],
"type": "object",
},
"operation": {
"properties": {
"path": {
"type": "string",
},
"operation": {
"properties": {
"path": {
"type": "string",
},
"type": {
"const": "replace",
"title": "Always use replace",
"type": "string",
},
"value": {
"type": [
"string",
"number",
"boolean",
"null",
"array",
"object",
],
},
},
"required": [
"type",
"path",
"value",
],
"type": "object",
"type": {
"const": "replace",
"title": "Always use replace",
"type": "string",
},
"optional": {
"value": {
"type": [
"string",
"number",
Expand All @@ -147,51 +130,69 @@ test('can print a type', () => {
"object",
],
},
"refField": {
"$ref": "#/$defs/refId",
},
"tags": {
"items": {
"type": "string",
},
"title": "Tags",
"type": "array",
},
"und": {
"const": undefined,
"type": "undefined",
},
"unionProperty": {
"anyOf": [
{
"type": "string",
},
{
"type": "number",
},
{
"const": null,
"type": "object",
},
],
},
},
"required": [
"id",
"tags",
"booleanProperty",
"numberProperty",
"binaryProperty",
"arrayProperty",
"objectProperty",
"unionProperty",
"operation",
"binaryOperation",
"map",
"type",
"path",
"value",
],
"type": "object",
}
`);
},
"optional": {
"type": [
"string",
"number",
"boolean",
"null",
"array",
"object",
],
},
"refField": {
"$ref": "#/$defs/refId",
},
"tags": {
"items": {
"type": "string",
},
"title": "Tags",
"type": "array",
},
"und": {
"const": undefined,
"type": "undefined",
},
"unionProperty": {
"anyOf": [
{
"type": "string",
},
{
"type": "number",
},
{
"const": null,
"type": "object",
},
],
},
},
"required": [
"id",
"tags",
"booleanProperty",
"numberProperty",
"binaryProperty",
"arrayProperty",
"objectProperty",
"unionProperty",
"operation",
"binaryOperation",
"map",
],
"type": "object",
}
`);
});

test('exports "ref" type to JSON Schema "$defs"', () => {
Expand Down
Loading