2020-04-09 11:13:19 +02:00
|
|
|
import { testSchema } from "./testSchema.mjs"
|
|
|
|
import { validateRecord } from "../src/records/validateRecord.mjs"
|
|
|
|
import { getNewRecord } from "../src/records/getNewRecord.mjs"
|
2020-04-08 18:00:27 +02:00
|
|
|
|
2020-04-09 17:42:55 +02:00
|
|
|
describe("validateRecord", () => {
|
2020-04-09 11:13:19 +02:00
|
|
|
it("should return errors when any fields do not parse", () => {
|
|
|
|
const schema = testSchema()
|
|
|
|
const record = getNewRecord(schema, "Contact")
|
2020-04-08 18:00:27 +02:00
|
|
|
|
2020-04-09 11:13:19 +02:00
|
|
|
record.Name = "Ledog"
|
|
|
|
record["Is Active"] = "hello"
|
|
|
|
record.Created = "not a date"
|
2020-04-08 18:00:27 +02:00
|
|
|
|
2020-04-09 11:13:19 +02:00
|
|
|
const validationResult = validateRecord(schema, record)
|
2020-04-08 18:00:27 +02:00
|
|
|
|
|
|
|
expect(validationResult.isValid).toBe(false)
|
2020-04-09 11:13:19 +02:00
|
|
|
expect(validationResult.errors.length).toBe(2)
|
2020-04-08 18:00:27 +02:00
|
|
|
})
|
|
|
|
|
2020-04-09 11:13:19 +02:00
|
|
|
it("should return errors when mandatory field is empty", () => {
|
|
|
|
const schema = testSchema()
|
|
|
|
const record = getNewRecord(schema, "Contact")
|
|
|
|
record.Name = ""
|
2020-04-08 18:00:27 +02:00
|
|
|
|
2020-04-09 11:13:19 +02:00
|
|
|
const validationResult = validateRecord(schema, record)
|
2020-04-08 18:00:27 +02:00
|
|
|
|
|
|
|
expect(validationResult.isValid).toBe(false)
|
|
|
|
expect(validationResult.errors.length).toBe(1)
|
|
|
|
})
|
|
|
|
|
2020-04-09 11:13:19 +02:00
|
|
|
it("should return error when string field is beyond maxLength", () => {
|
|
|
|
const schema = testSchema()
|
|
|
|
schema.findField("Contact", "Name").typeOptions.maxLength = 5
|
|
|
|
const record = getNewRecord(schema, "Contact")
|
2020-04-09 17:42:55 +02:00
|
|
|
record.Name = "more than 5 characters"
|
2020-04-08 18:00:27 +02:00
|
|
|
|
2020-04-09 11:13:19 +02:00
|
|
|
const validationResult = validateRecord(schema, record)
|
2020-04-08 18:00:27 +02:00
|
|
|
expect(validationResult.isValid).toBe(false)
|
|
|
|
expect(validationResult.errors.length).toBe(1)
|
|
|
|
})
|
|
|
|
|
2020-04-09 11:13:19 +02:00
|
|
|
it("should return error when number field is > maxValue", () => {
|
|
|
|
const schema = testSchema()
|
|
|
|
schema.findField("Deal", "Estimated Value").typeOptions.maxValue = 5
|
|
|
|
const record = getNewRecord(schema, "Deal")
|
|
|
|
record["Estimated Value"] = 10
|
2020-04-08 18:00:27 +02:00
|
|
|
|
2020-04-09 17:42:55 +02:00
|
|
|
const validationResult = validateRecord(schema, record)
|
2020-04-09 11:13:19 +02:00
|
|
|
expect(validationResult.isValid).toBe(false)
|
|
|
|
expect(validationResult.errors.length).toBe(1)
|
2020-04-08 18:00:27 +02:00
|
|
|
})
|
|
|
|
|
2020-04-09 17:42:55 +02:00
|
|
|
it("should return error when number field is < minValue", () => {
|
|
|
|
const schema = testSchema()
|
|
|
|
schema.findField("Deal", "Estimated Value").typeOptions.minValue = 5
|
|
|
|
const record = getNewRecord(schema, "Deal")
|
|
|
|
record["Estimated Value"] = 1
|
2020-04-08 18:00:27 +02:00
|
|
|
|
2020-04-09 17:42:55 +02:00
|
|
|
const result = validateRecord(schema, record)
|
|
|
|
expect(result.isValid).toBe(false)
|
|
|
|
expect(result.errors.length).toBe(1)
|
2020-04-08 18:00:27 +02:00
|
|
|
})
|
|
|
|
|
2020-04-09 17:42:55 +02:00
|
|
|
it("should return error when number has too many decimal places", () => {
|
|
|
|
const schema = testSchema()
|
|
|
|
schema.findField("Deal", "Estimated Value").typeOptions.decimalPlaces = 2
|
|
|
|
const record = getNewRecord(schema, "Deal")
|
|
|
|
record["Estimated Value"] = 1.123
|
2020-04-08 18:00:27 +02:00
|
|
|
|
2020-04-09 17:42:55 +02:00
|
|
|
const result = validateRecord(schema, record)
|
|
|
|
expect(result.isValid).toBe(false)
|
|
|
|
expect(result.errors.length).toBe(1)
|
2020-04-08 18:00:27 +02:00
|
|
|
})
|
|
|
|
|
2020-04-09 17:42:55 +02:00
|
|
|
it("should return error when datetime field is > maxValue", () => {
|
|
|
|
const schema = testSchema()
|
|
|
|
schema.findField("Contact", "Created").typeOptions.maxValue = new Date(2020, 1, 1)
|
|
|
|
const record = getNewRecord(schema, "Contact")
|
|
|
|
record.Name = "Bob"
|
|
|
|
record.Created = new Date(2020, 1, 2)
|
2020-04-08 18:00:27 +02:00
|
|
|
|
2020-04-09 17:42:55 +02:00
|
|
|
const result = validateRecord(schema, record)
|
2020-04-08 18:00:27 +02:00
|
|
|
expect(result.isValid).toBe(false)
|
|
|
|
expect(result.errors.length).toBe(1)
|
|
|
|
})
|
|
|
|
|
2020-04-09 17:42:55 +02:00
|
|
|
it("should return error when number field is < minValue", () => {
|
|
|
|
const schema = testSchema()
|
|
|
|
schema.findField("Contact", "Created").typeOptions.minValue = new Date(2020, 1, 2)
|
|
|
|
const record = getNewRecord(schema, "Contact")
|
|
|
|
record.Name = "Bob"
|
|
|
|
record.Created = new Date(2020, 1, 1)
|
2020-04-08 18:00:27 +02:00
|
|
|
|
2020-04-09 17:42:55 +02:00
|
|
|
const result = validateRecord(schema, record)
|
2020-04-08 18:00:27 +02:00
|
|
|
expect(result.isValid).toBe(false)
|
|
|
|
expect(result.errors.length).toBe(1)
|
|
|
|
})
|
|
|
|
|
2020-04-09 17:42:55 +02:00
|
|
|
it("should return error when string IS NOT one of declared values, and only declared values are allowed", () => {
|
|
|
|
const schema = testSchema()
|
|
|
|
schema.findField("Contact", "Status").typeOptions.allowDeclaredValuesOnly = true
|
|
|
|
schema.findField("Contact", "Status").typeOptions.values = ["thedog"]
|
|
|
|
const record = getNewRecord(schema, "Contact")
|
|
|
|
record.Status = "not allowed"
|
|
|
|
record.Name = "Bob"
|
2020-04-08 18:00:27 +02:00
|
|
|
|
2020-04-09 17:42:55 +02:00
|
|
|
const result = validateRecord(schema, record)
|
2020-04-08 18:00:27 +02:00
|
|
|
expect(result.isValid).toBe(false)
|
|
|
|
expect(result.errors.length).toBe(1)
|
|
|
|
})
|
|
|
|
|
2020-04-09 17:42:55 +02:00
|
|
|
it("should not return error when string IS one of declared values, and only declared values are allowed", () => {
|
|
|
|
const schema = testSchema()
|
|
|
|
schema.findField("Contact", "Status").typeOptions.allowDeclaredValuesOnly = true
|
|
|
|
schema.findField("Contact", "Status").typeOptions.values = ["thedog"]
|
|
|
|
const record = getNewRecord(schema, "Contact")
|
|
|
|
record.Status = "thedog"
|
|
|
|
record.Name = "Bob"
|
2020-04-08 18:00:27 +02:00
|
|
|
|
2020-04-09 17:42:55 +02:00
|
|
|
const result = validateRecord(schema, record)
|
2020-04-08 18:00:27 +02:00
|
|
|
expect(result.isValid).toBe(true)
|
|
|
|
expect(result.errors.length).toBe(0)
|
|
|
|
})
|
|
|
|
|
2020-04-09 17:42:55 +02:00
|
|
|
it("should not return error when string IS NOT one of declared values, but any values are allowed", () => {
|
|
|
|
const schema = testSchema()
|
|
|
|
schema.findField("Contact", "Status").typeOptions.allowDeclaredValuesOnly = false
|
|
|
|
schema.findField("Contact", "Status").typeOptions.values = ["thedog"]
|
|
|
|
const record = getNewRecord(schema, "Contact")
|
|
|
|
record.Status = "not one of the values"
|
|
|
|
record.Name = "Bob"
|
2020-04-08 18:00:27 +02:00
|
|
|
|
2020-04-09 17:42:55 +02:00
|
|
|
const result = validateRecord(schema, record)
|
2020-04-08 18:00:27 +02:00
|
|
|
expect(result.isValid).toBe(true)
|
|
|
|
expect(result.errors.length).toBe(0)
|
|
|
|
})
|
|
|
|
})
|