Skip to content

Feature/data validation #47

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
10 changes: 6 additions & 4 deletions generate-validation.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import path from 'path'
import Ajv from 'ajv'
import Ajv from 'ajv/dist/2020'
import addFormats from 'ajv-formats'
import standaloneCode from 'ajv/dist/standalone'
import standaloneCode from "ajv/dist/standalone"

import fs from 'fs'

const ajv = new Ajv({
code: { source: true },
code: { source: true, esm: true },
strict: 'log',
allowMatchingProperties: true,
allowUnionTypes: true,
discriminator: true,
})
addFormats(ajv)
Expand All @@ -18,10 +20,10 @@ const files = fs.readdirSync('./json-schemas')
files.forEach(file => {
const schema = require(`./json-schemas/${file}`)
const [name] = file.split('.', 1)
console.log(file)
ajv.addSchema(schema, name + 'Schema')
})

ajv.getSchema("FieldTypeSchema")

let moduleCode = standaloneCode(ajv)
fs.writeFileSync(path.join(__dirname, 'src/validate.js'), moduleCode)
20 changes: 20 additions & 0 deletions json-schemas/AWSLambdaDestination.schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"$schema": "https://json-schema.org/draft/2020-12/schema#",
"description": "We recommend creating an Identify and Access Management (IAM) user with an `accessKey` and `accessSecret` pair, specifically for each Extension that only has the `lambda:InvokeFunction` permission on this function.",
"additionalProperties": false,
"title": "AWSLambdaDestination",
"type": "object",
"properties": {
"type": {"enum": ["AWSLambda"]},
"arn": {"type": "string"},
"accessKey": {"type": "string"},
"accessSecret": {"type": "string"}
},
"required": [
"type",
"arn",
"accessKey",
"accessSecret"
],
"$id": "https://api.commercetools.com/AWSLambdaDestination.schema.json"
}
12 changes: 10 additions & 2 deletions json-schemas/AccessDeniedError.schema.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"patternProperties": {"": {"type": [
"number",
"string",
"boolean",
"object",
"array",
"null"
]}},
"$schema": "https://json-schema.org/draft/2020-12/schema#",
"additionalProperties": false,
"title": "AccessDeniedError",
"type": "object",
Expand All @@ -11,5 +19,5 @@
"code",
"message"
],
"$id": "https://example.com/AccessDeniedError.schema.json"
"$id": "https://api.commercetools.com/AccessDeniedError.schema.json"
}
6 changes: 3 additions & 3 deletions json-schemas/Address.schema.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$schema": "https://json-schema.org/draft/2020-12/schema#",
"additionalProperties": false,
"title": "Address",
"type": "object",
"properties": {
"lastName": {"type": "string"},
"country": {"type": "string"},
"lastName": {"type": "string"},
"city": {"type": "string"},
"postalCode": {"type": "string"},
"title": {"type": "string"},
Expand All @@ -32,5 +32,5 @@
"apartment": {"type": "string"}
},
"required": ["country"],
"$id": "https://example.com/Address.schema.json"
"$id": "https://api.commercetools.com/Address.schema.json"
}
36 changes: 36 additions & 0 deletions json-schemas/AddressDraft.schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"$schema": "https://json-schema.org/draft/2020-12/schema#",
"additionalProperties": false,
"title": "AddressDraft",
"type": "object",
"properties": {
"country": {"type": "string"},
"lastName": {"type": "string"},
"city": {"type": "string"},
"postalCode": {"type": "string"},
"title": {"type": "string"},
"building": {"type": "string"},
"streetName": {"type": "string"},
"company": {"type": "string"},
"id": {"type": "string"},
"state": {"type": "string"},
"department": {"type": "string"},
"fax": {"type": "string"},
"key": {"type": "string"},
"email": {"type": "string"},
"pOBox": {"type": "string"},
"streetNumber": {"type": "string"},
"custom": {"$ref": "CustomFieldsDraft.schema.json"},
"mobile": {"type": "string"},
"externalId": {"type": "string"},
"additionalAddressInfo": {"type": "string"},
"firstName": {"type": "string"},
"phone": {"type": "string"},
"additionalStreetInfo": {"type": "string"},
"salutation": {"type": "string"},
"region": {"type": "string"},
"apartment": {"type": "string"}
},
"required": ["country"],
"$id": "https://api.commercetools.com/AddressDraft.schema.json"
}
4 changes: 2 additions & 2 deletions json-schemas/AnonymousCartSignInModeEnum.schema.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$schema": "https://json-schema.org/draft/2020-12/schema#",
"title": "AnonymousCartSignInMode",
"type": "string",
"enum": [
"MergeWithExistingCustomerCart",
"UseAsNewActiveCustomerCart"
],
"$id": "https://example.com/AnonymousCartSignInModeEnum.schema.json"
"$id": "https://api.commercetools.com/AnonymousCartSignInModeEnum.schema.json"
}
12 changes: 10 additions & 2 deletions json-schemas/AnonymousIdAlreadyInUseError.schema.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"patternProperties": {"": {"type": [
"number",
"string",
"boolean",
"object",
"array",
"null"
]}},
"$schema": "https://json-schema.org/draft/2020-12/schema#",
"additionalProperties": false,
"title": "AnonymousIdAlreadyInUseError",
"type": "object",
Expand All @@ -11,5 +19,5 @@
"code",
"message"
],
"$id": "https://example.com/AnonymousIdAlreadyInUseError.schema.json"
"$id": "https://api.commercetools.com/AnonymousIdAlreadyInUseError.schema.json"
}
8 changes: 5 additions & 3 deletions json-schemas/ApiClient.schema.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$schema": "https://json-schema.org/draft/2020-12/schema#",
"additionalProperties": false,
"title": "ApiClient",
"type": "object",
Expand All @@ -21,14 +21,16 @@
"format": "date-time"
},
"scope": {"type": "string"},
"accessTokenValiditySeconds": {"type": "number"},
"name": {"type": "string"},
"id": {"type": "string"},
"secret": {"type": "string"}
"secret": {"type": "string"},
"refreshTokenValiditySeconds": {"type": "number"}
},
"required": [
"id",
"name",
"scope"
],
"$id": "https://example.com/ApiClient.schema.json"
"$id": "https://api.commercetools.com/ApiClient.schema.json"
}
8 changes: 5 additions & 3 deletions json-schemas/ApiClientDraft.schema.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$schema": "https://json-schema.org/draft/2020-12/schema#",
"additionalProperties": false,
"title": "ApiClientDraft",
"type": "object",
"properties": {
"name": {"type": "string"},
"refreshTokenValiditySeconds": {"type": "number"},
"deleteDaysAfterCreation": {"type": "number"},
"scope": {"type": "string"}
"scope": {"type": "string"},
"accessTokenValiditySeconds": {"type": "number"}
},
"required": [
"name",
"scope"
],
"$id": "https://example.com/ApiClientDraft.schema.json"
"$id": "https://api.commercetools.com/ApiClientDraft.schema.json"
}
7 changes: 4 additions & 3 deletions json-schemas/ApiClientPagedQueryResponse.schema.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$schema": "https://json-schema.org/draft/2020-12/schema#",
"description": "[PagedQueryResult](/general-concepts#pagedqueryresult) with `results` containing an array of [APIClient](ctp:api:type:ApiClient).",
"additionalProperties": false,
"title": "ApiClientPagedQueryResponse",
"type": "object",
Expand All @@ -15,9 +16,9 @@
},
"required": [
"limit",
"count",
"offset",
"count",
"results"
],
"$id": "https://example.com/ApiClientPagedQueryResponse.schema.json"
"$id": "https://api.commercetools.com/ApiClientPagedQueryResponse.schema.json"
}
4 changes: 2 additions & 2 deletions json-schemas/Asset.schema.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$schema": "https://json-schema.org/draft/2020-12/schema#",
"additionalProperties": false,
"title": "Asset",
"type": "object",
Expand All @@ -23,5 +23,5 @@
"sources",
"name"
],
"$id": "https://example.com/Asset.schema.json"
"$id": "https://api.commercetools.com/Asset.schema.json"
}
5 changes: 3 additions & 2 deletions json-schemas/AssetDimensions.schema.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$schema": "https://json-schema.org/draft/2020-12/schema#",
"description": "Dimensions of the Asset source specified by the number of pixels.",
"additionalProperties": false,
"title": "AssetDimensions",
"type": "object",
Expand All @@ -11,5 +12,5 @@
"w",
"h"
],
"$id": "https://example.com/AssetDimensions.schema.json"
"$id": "https://api.commercetools.com/AssetDimensions.schema.json"
}
4 changes: 2 additions & 2 deletions json-schemas/AssetDraft.schema.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$schema": "https://json-schema.org/draft/2020-12/schema#",
"additionalProperties": false,
"title": "AssetDraft",
"type": "object",
Expand All @@ -21,5 +21,5 @@
"sources",
"name"
],
"$id": "https://example.com/AssetDraft.schema.json"
"$id": "https://api.commercetools.com/AssetDraft.schema.json"
}
5 changes: 3 additions & 2 deletions json-schemas/AssetSource.schema.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$schema": "https://json-schema.org/draft/2020-12/schema#",
"description": "Representation of an [Asset](#asset) in a specific format, for example a video in a certain encoding, or an image in a certain resolution.",
"additionalProperties": false,
"title": "AssetSource",
"type": "object",
Expand All @@ -10,5 +11,5 @@
"dimensions": {"$ref": "AssetDimensions.schema.json"}
},
"required": ["uri"],
"$id": "https://example.com/AssetSource.schema.json"
"$id": "https://api.commercetools.com/AssetSource.schema.json"
}
12 changes: 12 additions & 0 deletions json-schemas/AssignedProductReference.schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"$schema": "https://json-schema.org/draft/2020-12/schema#",
"additionalProperties": false,
"title": "AssignedProductReference",
"type": "object",
"properties": {
"variantSelection": {"$ref": "ProductVariantSelection.schema.json"},
"product": {"$ref": "ProductReference.schema.json"}
},
"required": ["product"],
"$id": "https://api.commercetools.com/AssignedProductReference.schema.json"
}
12 changes: 12 additions & 0 deletions json-schemas/AssignedProductSelection.schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"$schema": "https://json-schema.org/draft/2020-12/schema#",
"additionalProperties": false,
"title": "AssignedProductSelection",
"type": "object",
"properties": {
"variantSelection": {"$ref": "ProductVariantSelection.schema.json"},
"productSelection": {"$ref": "ProductSelectionReference.schema.json"}
},
"required": ["productSelection"],
"$id": "https://api.commercetools.com/AssignedProductSelection.schema.json"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"$schema": "https://json-schema.org/draft/2020-12/schema#",
"description": "[PagedQueryResult](/general-concepts#pagedqueryresult) containing an array of [AssignedProductSelection](ctp:api:type:AssignedProductSelection).",
"additionalProperties": false,
"title": "AssignedProductSelectionPagedQueryResponse",
"type": "object",
"properties": {
"limit": {"type": "number"},
"count": {"type": "number"},
"total": {"type": "number"},
"offset": {"type": "number"},
"results": {
"type": "array",
"items": {"$ref": "AssignedProductSelection.schema.json"}
}
},
"required": [
"limit",
"offset",
"count",
"results"
],
"$id": "https://api.commercetools.com/AssignedProductSelectionPagedQueryResponse.schema.json"
}
4 changes: 2 additions & 2 deletions json-schemas/Attribute.schema.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$schema": "https://json-schema.org/draft/2020-12/schema#",
"additionalProperties": false,
"title": "Attribute",
"type": "object",
Expand All @@ -18,5 +18,5 @@
"name",
"value"
],
"$id": "https://example.com/Attribute.schema.json"
"$id": "https://api.commercetools.com/Attribute.schema.json"
}
5 changes: 3 additions & 2 deletions json-schemas/AttributeBooleanType.schema.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$schema": "https://json-schema.org/draft/2020-12/schema#",
"description": "Attribute type for Boolean values. Valid values for the Attribute are `true` and `false` (JSON Boolean).",
"additionalProperties": false,
"title": "AttributeBooleanType",
"type": "object",
"properties": {"name": {"enum": ["boolean"]}},
"required": ["name"],
"$id": "https://example.com/AttributeBooleanType.schema.json"
"$id": "https://api.commercetools.com/AttributeBooleanType.schema.json"
}
4 changes: 2 additions & 2 deletions json-schemas/AttributeConstraintEnumDraftEnum.schema.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$schema": "https://json-schema.org/draft/2020-12/schema#",
"title": "AttributeConstraintEnumDraft",
"type": "string",
"enum": ["None"],
"$id": "https://example.com/AttributeConstraintEnumDraftEnum.schema.json"
"$id": "https://api.commercetools.com/AttributeConstraintEnumDraftEnum.schema.json"
}
5 changes: 3 additions & 2 deletions json-schemas/AttributeConstraintEnumEnum.schema.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"description": "Specifies how an Attribute (or a set of Attributes) should be validated across all variants of a Product:",
"$schema": "https://json-schema.org/draft/2020-12/schema#",
"title": "AttributeConstraintEnum",
"type": "string",
"enum": [
Expand All @@ -8,5 +9,5 @@
"CombinationUnique",
"SameForAll"
],
"$id": "https://example.com/AttributeConstraintEnumEnum.schema.json"
"$id": "https://api.commercetools.com/AttributeConstraintEnumEnum.schema.json"
}
Loading