cypress-io / schema-tools

Validate, sanitize and document JSON schemas

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Error "format1.test is not a function" is returned when validating json schema with custom format

nhantrantrong opened this issue · comments

Summary:
Error message format1.test is not a function is thrown when assertSchema with a custom format. So could you please help me to check if there is an issue with this or it is a bug? I am using Cypress with JavaScript

Environment:
Cypress: 8.7.0
@cypress/schema-tools: 4.7.8
ts-node: 5.0.1,
typescript: 4.4.3

Steps to Reproduce

  • Create an index.ts file under formats folder as below:
// single custom format
import { CustomFormat, CustomFormats } from '@cypress/schema-tools'

const uuid: CustomFormat = {
  name: 'uuid', // the name
  description: 'GUID used through the system',
  // regular expression to use to validate value
  detect: /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/
}

// export all custom formats, in our case just 1
export const formats: CustomFormats = { uuid }
  • Create a temp-response-schema.ts as below
import { ObjectSchema, SchemaCollection, combineSchemas, VersionedSchema, versionSchemas } from '@cypress/schema-tools'
const tempResponse100: ObjectSchema = {
    version: {
        major: 1,
        minor: 0,
        patch: 0
    },
    schema: {
        type: "object",
        title: "TempTestResponse",
        additionalProperties: false,
        properties: {
            "id": {
                "type": "string",
                "format": "uuid"
            }
        },
        "required": [
            "id"
        ]
    },

    example: {
        id: '97235a5f-5c5d-422b-a386-a1d7b53c5a6a'
    }
}

const tempReponsesVersions: VersionedSchema = versionSchemas(tempResponse100)

export const tempResponseSchemas: SchemaCollection = combineSchemas(
    tempReponsesVersions
)
  • Implement a test script as below:
import { formats } from '../../../fixtures/schemas/formats';
import { assertSchema } from '@cypress/schema-tools';

context('Sample Test', () => {

    it.only('Test api response with format', () => {
        const o = {
            id: '97235a5f-5c5d-422b-a386-a1d7b53c5a6a'
        }

        try {
            assertSchema(tempResponseSchemas, formats)('TempTestResponse', '1.0.0')(o)
            expect(true, 'test').to.true
        } catch (err) {
            cy.log(`error: ${err.message}`)
        }
    })
})
  • Execute the above test

Actual Result:

  • Error message format1.test is not a function. Please find following capture for the error
    image

I have the same issue.