Fix: fix validation of question input; remove some obsolete logging; stabilize output of generate:translations wrt newlines

This commit is contained in:
Pieter Vander Vennet 2023-05-03 00:57:15 +02:00
parent 755f905c36
commit 1f39ba9ab5
26 changed files with 7328 additions and 71 deletions

View file

@ -6,29 +6,43 @@
import { ExclamationIcon } from "@rgossiaux/svelte-heroicons/solid";
import { Translation } from "../i18n/Translation";
import { createEventDispatcher, onDestroy } from "svelte";
import {Validator} from "./Validator";
export let value: UIEventSource<string>;
// Internal state, only copied to 'value' so that no invalid values leak outside
let _value = new UIEventSource(value.data ?? "");
onDestroy(value.addCallbackAndRunD(v => _value.setData(v ?? "")));
export let type: ValidatorType;
let validator = Validators.get(type);
export let feedback: UIEventSource<Translation> | undefined = undefined;
export let getCountry: () => string | undefined
let validator : Validator = Validators.get(type)
$: {
// The type changed -> reset some values
validator = Validators.get(type)
_value.setData("")
feedback = feedback?.setData(validator?.getFeedback(_value.data, getCountry));
}
onDestroy(value.addCallbackAndRun(v => {
if(v === undefined || v === ""){
_value.setData("")
}
}))
onDestroy(_value.addCallbackAndRun(v => {
if (validator.isValid(v)) {
if (validator.isValid(v, getCountry)) {
feedback?.setData(undefined);
value.setData(v);
return;
}
value.setData(undefined);
feedback?.setData(validator.getFeedback(v));
feedback?.setData(validator.getFeedback(v, getCountry));
}))
if (validator === undefined) {
throw "Not a valid type for a validator:" + type;
}
const isValid = _value.map(v => validator.isValid(v));
const isValid = _value.map(v => validator.isValid(v, getCountry));
let htmlElem: HTMLInputElement;

View file

@ -44,9 +44,8 @@ export abstract class Validator {
/**
* Gets a piece of feedback. By default, validation.<type> will be used, resulting in a generic 'not a valid <type>'.
* However, inheritors might overwrite this to give more specific feedback
* @param s
*/
public getFeedback(s: string): Translation {
public getFeedback(s: string, requestCountry?: () => string): Translation {
const tr = Translations.t.validation[this.name]
if (tr !== undefined) {
return tr["feedback"]

View file

@ -1,7 +1,8 @@
import { Translation } from "../../i18n/Translation.js"
import {Translation} from "../../i18n/Translation.js"
import Translations from "../../i18n/Translations.js"
import * as emailValidatorLibrary from "email-validator"
import { Validator } from "../Validator"
import {Validator} from "../Validator"
export default class EmailValidator extends Validator {
constructor() {
super("email", "An email adress", "email")

View file

@ -1,12 +1,28 @@
import { parsePhoneNumberFromString } from "libphonenumber-js"
import { Validator } from "../Validator"
import {parsePhoneNumberFromString} from "libphonenumber-js"
import {Validator} from "../Validator"
import {Translation} from "../../i18n/Translation";
import Translations from "../../i18n/Translations";
export default class PhoneValidator extends Validator {
constructor() {
super("phone", "A phone number", "tel")
}
isValid(str, country: () => string): boolean {
getFeedback(s: string, requestCountry?: () => string): Translation {
const tr = Translations.t.validation.phone
const generic = tr.feedback
if(requestCountry){
const country = requestCountry()
if(country){
return tr.feedbackCountry.Subs({country})
}
}
return generic
}
public isValid(str, country: () => string): boolean {
if (str === undefined) {
return false
}
@ -20,13 +36,17 @@ export default class PhoneValidator extends Validator {
return parsePhoneNumberFromString(str, countryCode)?.isValid() ?? false
}
reformat = (str, country: () => string) => {
public reformat(str, country: () => string) {
if (str.startsWith("tel:")) {
str = str.substring("tel:".length)
}
let countryCode = undefined
if(country){
countryCode = country()
}
return parsePhoneNumberFromString(
str,
country()?.toUpperCase() as any
countryCode?.toUpperCase() as any
)?.formatInternational()
}
}