Skip to content
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

Add support for MaxSizeInBytes constraint #71

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
5 changes: 5 additions & 0 deletions .changeset/big-starfishes-dream.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@croz/nrich-form-configuration-core": patch
---

Add validator converter for MaxSizeInBytes constraint
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ export class FormConfigurationValidationConverter {
supports: (configuration) => ["InList"].includes(configuration.name),
convert: (configuration, validator) => validator.test("inList", configuration.errorMessage, (value) => (configuration.argumentMap.value as string[]).includes(value)),
},
{
supports: (configuration) => ["MaxSizeInBytes"].includes(configuration.name),
convert: (configuration, validator) => validator.test("maxSizeInBytes", configuration.errorMessage, (value: string) => new Blob([value]).size <= (configuration.argumentMap.value as number)),
},
{
supports: () => true,
convert: (configuration, validator) => validator[configuration.name.toLowerCase()](configuration.errorMessage),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,13 @@ import * as yup from "yup";

import { FormConfigurationValidationConverter } from "../../src/converter";
import {
createComplexValidationList, createCustomValidationList, createNestedValidationList, createSimpleNullableValidationList, createSimpleValidationList, invalidValidationConfiguration,
createComplexValidationList,
createCustomValidationList,
createMaxSizeInBytesValidationList,
createNestedValidationList,
createSimpleNullableValidationList,
createSimpleValidationList,
invalidValidationConfiguration,
} from "../testutil/form-configuration-generating-util";

describe("@croz/nrich-form-configuration-core/FormConfigurationValidationConverter", () => {
Expand Down Expand Up @@ -51,6 +57,21 @@ describe("@croz/nrich-form-configuration-core/FormConfigurationValidationConvert
expect(result.isValidSync({ username: "username" })).toBe(true);
});

it("should convert max size in bytes configuration to yup schema", () => {
// given
const converter = new FormConfigurationValidationConverter();
const simpleValidationList = createMaxSizeInBytesValidationList();

// when
const result = converter.convertFormConfigurationToYupSchema(simpleValidationList);

// then
expect(result).toBeDefined();
expect(() => result.validateSync({ username: "username" })).toThrowError("Too many diacritics");
expect(() => result.validateSync({ username: "ččč" })).toThrowError("Too many diacritics");
expect(result.isValidSync({ username: "user" })).toBe(true);
});

it("should use custom converter", () => {
// given
const additionalConverter = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,23 @@ export const createSimpleValidationList = () => [
},
];

export const createMaxSizeInBytesValidationList = () => [
{
path: "username",
propertyType: "java.lang.String",
javascriptType: "string",
validatorList: [
{
name: "MaxSizeInBytes",
argumentMap: {
value: 5,
},
errorMessage: "Too many diacritics",
},
],
},
];

export const createSimpleNullableValidationList = () => [
{
path: "username",
Expand Down
Loading